#include "audio.h"
#include "backends/backend.h"
#include "mini/file/file.h"

namespace mini
{
    namespace audio
    {
        int current_music = -1;

        void init() {
            backend::audio::init();
        }

        void quit() {
            backend::audio::quit();
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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