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

namespace mini
{
    namespace audio
    {
        int current_music = -1;
        //#define MAX_SOUNDS 50
        //JA_Sound_t *sounds[MAX_SOUNDS];

        void init() {
            jail::audio::init();
            //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]);
            jail::audio::quit();
        }

        namespace music
        {
            void play(const char *filename, const int loop) {
                int size;
                char *buffer = file::getfilebuffer(filename, size);
                if (current_music != -1) jail::audio::music::destroy(current_music);
                current_music = jail::audio::music::load((uint8_t*)buffer, size);
                jail::audio::music::play(current_music, loop);
            }

            void pause() {
                jail::audio::music::pause();
            }

            void resume() {
                jail::audio::music::resume();
            }

            void stop(const int t) {
                jail::audio::music::stop();
            }

            namespace pos {
                void set(float value)
                {
                    jail::audio::music::setPosition(value);
                }

                float get()
                {
                    return jail::audio::music::getPosition();
                }
            }

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

                const bool get()
                {
                    return jail::audio::music::isEnabled();
                }
            }
        }

        namespace sound
        {
            int load(const char *filename) {
                int size;
                char *buffer = file::getfilebuffer(filename, size);
                return jail::audio::sound::load((uint8_t*)buffer, size);
            }

            void free(int soundfile) {
                return jail::audio::sound::destroy(soundfile);
            }

            int play(int soundfile, const int volume) {
                // [TODO] Ficar el volumen
                return jail::audio::sound::play(soundfile, 0);
            }

            void stop(int soundchannel) {
                return jail::audio::sound::channel::stop(soundchannel);
            }

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

                const bool get()
                {
                    return jail::audio::sound::isEnabled();
                }
            }
        }
    }
}