#include <audio/audio.hpp>

namespace Game::Audio {
    Audio::~Audio() {
        if (mDevice != 0) {
            SDL_CloseAudioDevice(mDevice);
            LOG("Audio device closed successfully");
        }
        SDL_QuitSubSystem(SDL_INIT_AUDIO);
        LOG("SDL audio subsystem quit successfully");
    }

    Audio& Audio::getInstance() {
        static Audio instance;
        return instance;
    }

    bool Audio::init() {
        if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) {
            ERROR("Failed to initialize SDL audio subsystem: " << SDL_GetError());
        } else {
            LOG("SDL audio subsystem initialized successfully");
        }
        
        mAudioSpec.channels = 2; // Stereo
        mAudioSpec.freq = 44100; // CD quality
        mAudioSpec.format = SDL_AUDIO_F32; // 32-bit float

        mDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &mAudioSpec);
        if (mDevice == 0) {
            ERROR("Failed to open audio device: " << SDL_GetError());
            return false;
        }

        return true;
    }
}