// SoundEffect.h - Single sound effect player using SDL3 audio
#pragma once
#include <SDL3/SDL.h>
#include <string>
#include <vector>
#include <cstdint>
#include <memory>

class SoundEffect {
public:
    SoundEffect() = default;
    ~SoundEffect() = default;
    
    // Load a sound effect from file (WAV or MP3)
    bool load(const std::string& filePath);
    
    // Play the sound effect
    void play(float volume = 1.0f);
    
    // Set default volume for this sound effect
    void setVolume(float volume);
    
    // Get PCM data for mixing
    const std::vector<int16_t>& getPCMData() const { return pcmData; }
    int getChannels() const { return channels; }
    int getSampleRate() const { return sampleRate; }
    bool isLoaded() const { return loaded; }
    
private:
    std::vector<int16_t> pcmData;
    int channels = 2;
    int sampleRate = 44100;
    bool loaded = false;
    float defaultVolume = 1.0f;
    
    bool loadWAV(const std::string& filePath);
    bool loadMP3(const std::string& filePath);
};

// Simple audio player for immediate playback
class SimpleAudioPlayer {
public:
    static SimpleAudioPlayer& instance();
    
    bool init();
    void shutdown();
    
    // Play a sound effect immediately
    void playSound(const std::vector<int16_t>& pcmData, int channels, int sampleRate, float volume = 1.0f);
    
private:
    SimpleAudioPlayer() = default;
    ~SimpleAudioPlayer() = default;
    SimpleAudioPlayer(const SimpleAudioPlayer&) = delete;
    SimpleAudioPlayer& operator=(const SimpleAudioPlayer&) = delete;
    
    bool initialized = false;
};

// Helper class to manage multiple sound effects
class SoundEffectManager {
public:
    static SoundEffectManager& instance();
    
    bool init();
    void shutdown();
    
    // Load a sound effect and assign it an ID
    bool loadSound(const std::string& id, const std::string& filePath);
    
    // Play a sound effect by ID
    void playSound(const std::string& id, float volume = 1.0f);
    
    // Play a random sound from a group
    void playRandomSound(const std::vector<std::string>& soundIds, float volume = 1.0f);
    
    // Set master volume for all sound effects
    void setMasterVolume(float volume);
    
    // Enable/disable sound effects
    void setEnabled(bool enabled);
    bool isEnabled() const;
    
private:
    SoundEffectManager() = default;
    ~SoundEffectManager() = default;
    SoundEffectManager(const SoundEffectManager&) = delete;
    SoundEffectManager& operator=(const SoundEffectManager&) = delete;
    
    std::vector<std::pair<std::string, std::unique_ptr<SoundEffect>>> soundEffects;
    float masterVolume = 1.0f;
    bool enabled = true;
    bool initialized = false;
};
