#pragma once

#include <SDL3/SDL.h>

#include <string>
#include <vector>

#include "core/rendering/shader_backend.hpp"  // for Rendering::ShaderType
#include "game/defaults.hpp"
#include "utils/utils.h"  // for input_t, palette_e

// =============================================================================
// Opciones del programa, alineades amb coffee_crisis_arcade_edition.
// L'estat viu en globals dins el namespace Options:: (window, video, audio,
// loading, settings, inputs). La persistència usa fkyaml i es guarda a
// config.yaml dins la carpeta de configuració del sistema.
// =============================================================================

namespace Options {

    struct Window {
        std::string caption = Defaults::Window::CAPTION;
        int zoom = Defaults::Window::ZOOM;
        int max_zoom = Defaults::Window::MAX_ZOOM;
    };

    struct GPU {
        bool acceleration = Defaults::Video::GPU_ACCELERATION;
        std::string preferred_driver = Defaults::Video::GPU_PREFERRED_DRIVER;
    };

    struct Supersampling {
        bool enabled = Defaults::Video::SUPERSAMPLING;
        bool linear_upscale = Defaults::Video::LINEAR_UPSCALE;
        int downscale_algo = Defaults::Video::DOWNSCALE_ALGO;
    };

    struct ShaderConfig {
        bool enabled = Defaults::Video::SHADER_ENABLED;
        Rendering::ShaderType current_shader = Rendering::ShaderType::POSTFX;
        std::string current_postfx_preset_name = "CRT";
        std::string current_crtpi_preset_name = "Default";
        int current_postfx_preset = 0;
        int current_crtpi_preset = 0;
    };

    struct Video {
        SDL_ScaleMode scale_mode = Defaults::Video::SCALE_MODE;
        bool fullscreen = Defaults::Video::FULLSCREEN;
        bool vsync = Defaults::Video::VSYNC;
        bool integer_scale = Defaults::Video::INTEGER_SCALE;
        GPU gpu;
        Supersampling supersampling;
        ShaderConfig shader;
    };

    struct Music {
        bool enabled = Defaults::Music::ENABLED;
        int volume = Defaults::Music::VOLUME;
    };

    struct Sound {
        bool enabled = Defaults::Sound::ENABLED;
        int volume = Defaults::Sound::VOLUME;
    };

    struct Audio {
        bool enabled = Defaults::Audio::ENABLED;
        int volume = Defaults::Audio::VOLUME;
        Music music;
        Sound sound;
    };

    struct Loading {
        bool show = Defaults::Loading::SHOW;
        bool show_resource_name = Defaults::Loading::SHOW_RESOURCE_NAME;
        bool wait_for_input = Defaults::Loading::WAIT_FOR_INPUT;
    };

    struct Settings {
        static constexpr int CURRENT_CONFIG_VERSION = 1;
        int config_version = CURRENT_CONFIG_VERSION;
        int difficulty = Defaults::Settings::DIFFICULTY;
        int language = Defaults::Settings::LANGUAGE;
        palette_e palette = Defaults::Settings::PALETTE;
        bool console = false;
        int player_selected = 0;
        std::string config_file;
    };

    // --- Variables globales ---
    extern Window window;
    extern Video video;
    extern Audio audio;
    extern Loading loading;
    extern Settings settings;
    extern std::vector<input_t> inputs;  // [0]=KEYBOARD, [1]=GAMECONTROLLER per defecte

    // --- Funciones ---
    void init();                                       // Reinicia a defaults i omple `inputs`
    void setConfigFile(const std::string &file_path);  // Ruta del config.yaml
    auto loadFromFile() -> bool;                       // Carrega el YAML; si no existeix, crea'l amb defaults
    auto saveToFile() -> bool;                         // Guarda el YAML

}  // namespace Options
