#include "audio.h"
#include "jail_audio.h"
#include "mini/file/file.h"

namespace mini
{
    namespace audio
    {
        JA_Music_t *current_music = NULL;
        #define MAX_SOUNDS 50
        JA_Sound_t *sounds[MAX_SOUNDS];

        void init() {
            JA_Init(48000, SDL_AUDIO_S16, 1);
            for (int i=0;i<MAX_SOUNDS;++i) sounds[i] = NULL;
        }

        void quit() {
            if (current_music != NULL) JA_DeleteMusic(current_music);
            for (int i=0;i<MAX_SOUNDS;++i) if (sounds[i]!=NULL) JA_DeleteSound(sounds[i]);
            JA_Quit();
        }

        namespace music
        {
            void play(const char *filename, const int loop) {
                int size;
                char *buffer = file::getfilebuffer(filename, size);
                if (current_music != NULL) JA_DeleteMusic(current_music);
                current_music = JA_LoadMusic((Uint8*)buffer, size);
                JA_PlayMusic(current_music, loop);
            }

            void pause() {
                JA_PauseMusic();
            }

            void resume() {
                JA_ResumeMusic();
            }

            void stop(const int t) {
                JA_StopMusic();
            }

            namespace pos {
                void set(float value)
                {
                    JA_SetMusicPosition(value);
                }

                float get()
                {
                    return JA_GetMusicPosition();
                }
            }

            namespace enable {
                void set(const bool value)
                {
                    JA_EnableMusic(value);
                    file::setconfigvalue("music", value?"true":"false");
                }

                const bool get()
                {
                    return JA_IsMusicEnabled();
                }
            }
        }

        namespace sound
        {
            int load(const char *filename) {
                int size;
                char *buffer = file::getfilebuffer(filename, size);
                int i=0;
                while (i<MAX_SOUNDS && sounds[i]!=NULL) i++;
                if (i==MAX_SOUNDS) { i=0; JA_DeleteSound(sounds[i]); }
                sounds[i]=JA_LoadSound((Uint8*)buffer, size);
                return i;
            }

            void free(int soundfile) {
                JA_DeleteSound(sounds[soundfile]);
                sounds[soundfile] = NULL;
            }

            int play(int soundfile, const int volume) {
                const int channel = JA_PlaySound(sounds[soundfile], 0);
                JA_SetSoundVolume(128);
                //Mix_Volume(channel, volume!=-1?volume:MIX_MAX_VOLUME);
                return channel;
            }

            void stop(int soundchannel) {
                JA_StopChannel(soundchannel);
                //Mix_HaltChannel(soundchannel);
            }

            namespace enable {
                void set(const bool value)
                {
                    JA_EnableSound(value);
                    file::setconfigvalue("sound", value?"true":"false");
                }

                const bool get()
                {
                    return JA_IsSoundEnabled();
                }
            }
        }
    }
}