#include "audio.h"
#include "external/jail_audio.h"
#include "options.h"
#include "resource.h"
#include <SDL3/SDL.h>

// Singleton
Audio *Audio::instance_ = nullptr;

// Inicializa la instancia única del singleton
void Audio::init() { Audio::instance_ = new Audio(); }

// Libera la instancia
void Audio::destroy() { delete Audio::instance_; }

// Obtiene la instancia
Audio *Audio::get() { return Audio::instance_; }

// Constructor
Audio::Audio() { initSDLAudio(); }

// Destructor
Audio::~Audio() { JA_Quit(); }

// Reproduce la música
void Audio::playMusic(const std::string &name, const int loop)
{
    music_.name = name;
    music_.loop = loop;

    if (music_enabled_ && music_.state != MusicState::PLAYING)
    {
        JA_PlayMusic(Resource::get()->getMusic(name), loop);
        music_.state = MusicState::PLAYING;
    }
}

// Pausa la música
void Audio::pauseMusic()
{
    if (music_enabled_ && music_.state == MusicState::PLAYING)
    {
        JA_PauseMusic();
        music_.state = MusicState::PAUSED;
    }
}

// Detiene la música
void Audio::stopMusic()
{
    if (music_enabled_)
    {
        JA_StopMusic();
        music_.state = MusicState::STOPPED;
    }
}

// Reproduce un sonido
void Audio::playSound(const std::string &name)
{
    if (sound_enabled_)
    {
        JA_PlaySound(Resource::get()->getSound(name));
    }
}

// Detiene todos los sonidos
void Audio::stopAllSounds()
{
    if (sound_enabled_)
    {
        JA_StopChannel(-1);
    }
}

// Realiza un fundido de salida de la música
void Audio::fadeOutMusic(int milliseconds)
{
    if (music_enabled_)
    {
        JA_FadeOutMusic(milliseconds);
    }
}

// Establece el volumen de los sonidos
void Audio::setSoundVolume(int sound_volume)
{
    if (sound_enabled_)
    {
        sound_volume = std::clamp(sound_volume, 0, 100);
        const float CONVERTED_VOLUME = (sound_volume / 100.0f) * (Options::audio.volume / 100.0f);
        JA_SetSoundVolume(CONVERTED_VOLUME);
    }
}

// Establece el volumen de la música
void Audio::setMusicVolume(int music_volume)
{
    if (music_enabled_)
    {
        music_volume = std::clamp(music_volume, 0, 100);
        const float CONVERTED_VOLUME = (music_volume / 100.0f) * (Options::audio.volume / 100.0f);
        JA_SetMusicVolume(CONVERTED_VOLUME);
    }
}

// Aplica la configuración
void Audio::applySettings()
{
    enable(Options::audio.enabled);
}

// Establecer estado general
void Audio::enable(bool value)
{
    enabled_ = value;

    setSoundVolume(enabled_ ? Options::audio.sound.volume : 0);
    setMusicVolume(enabled_ ? Options::audio.music.volume : 0);
}

// Inicializa SDL Audio
void Audio::initSDLAudio()
{
    if (!SDL_Init(SDL_INIT_AUDIO))
    {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO could not initialize! SDL Error: %s", SDL_GetError());
    }
    else
    {
        SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "\n** SDL_AUDIO: INITIALIZING\n");

        JA_Init(48000, SDL_AUDIO_S16LE, 2);
        enable(Options::audio.enabled);

        SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "** SDL_AUDIO: INITIALIZATION COMPLETE\n");
    }
}