#include "sound_commands.h"
#include <stdlib.h>
/* TODO (Ne0v001#1#): Add sound FX stuff <.< */
#include <iostream>


//main sound part
sounds Sound;

using std::cout;
using std::endl;

//this boots up the sound system
void PlayMusic(char * filename)
{
  Sound.channel = -1;

  strcpy(Sound.filename,filename);
  cout << "Starting up " << Sound.filename << endl;
    if (!FSOUND_Init(44100, 32, 0))
    {
        printf("Error!\n");
        printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));
        FSOUND_Close();
    }

    FSOUND_Stream_SetBufferSize(1000);

        FILE      *fp;
        int        length;
        char      *data;

        fp = fopen(Sound.filename, "rb");
        if (!fp)
        {
            printf("Error!\n");
            printf("File Not Found\n");
            FSOUND_Close();
        }
        fseek(fp, 0, SEEK_END);
        length = ftell(fp);
        fseek(fp, 0, SEEK_SET);

        data = (char *)malloc(length);
        fread(data, length, 1, fp);
        fclose(fp);

        Sound.stream = FSOUND_Stream_Open(data, FSOUND_NORMAL | FSOUND_MPEGACCURATE | FSOUND_LOADMEMORY, 0, length);
        
        if (Sound.channel < 0)
        {
            // ==========================================================================================
            // PLAY STREAM
            // ==========================================================================================
            Sound.channel = FSOUND_Stream_PlayEx(FSOUND_FREE, Sound.stream, NULL, TRUE);
            FSOUND_SetPaused(Sound.channel, FALSE);
        }
  cout << "Booted up " << Sound.filename << " correctly" << endl;
}

//shuts off all music files
void MusicOff()
{
    FSOUND_Stream_Close(Sound.stream);
    cout << "Closing " << Sound.filename << endl;
}