#pragma once

#include <string>
#include <vector>

#include "game/defaults.hpp"
#include "game/defines.hpp"

namespace Options {

    // Tecles GUI (finestra, zoom, shaders)
    struct KeysGUI {
        SDL_Scancode dec_zoom{Defaults::KeysGUI::DEC_ZOOM};
        SDL_Scancode inc_zoom{Defaults::KeysGUI::INC_ZOOM};
        SDL_Scancode fullscreen{Defaults::KeysGUI::FULLSCREEN};
        SDL_Scancode toggle_shader{Defaults::KeysGUI::TOGGLE_SHADER};
        SDL_Scancode toggle_aspect_ratio{Defaults::KeysGUI::TOGGLE_ASPECT_RATIO};
        SDL_Scancode toggle_supersampling{Defaults::KeysGUI::TOGGLE_SUPERSAMPLING};
        SDL_Scancode next_shader{Defaults::KeysGUI::NEXT_SHADER};
        SDL_Scancode next_shader_preset{Defaults::KeysGUI::NEXT_SHADER_PRESET};
        SDL_Scancode toggle_stretch_filter{Defaults::KeysGUI::TOGGLE_STRETCH_FILTER};
        SDL_Scancode toggle_render_info{Defaults::KeysGUI::TOGGLE_RENDER_INFO};
        SDL_Scancode menu_toggle{Defaults::KeysGUI::MENU_TOGGLE};
    };

    // Tecles de joc (moviment, accions)
    struct KeysGame {
        SDL_Scancode up{Defaults::KeysGame::UP};
        SDL_Scancode down{Defaults::KeysGame::DOWN};
        SDL_Scancode left{Defaults::KeysGame::LEFT};
        SDL_Scancode right{Defaults::KeysGame::RIGHT};
        SDL_Scancode exit{Defaults::KeysGame::EXIT};
    };

    // Posició del render info
    enum class RenderInfoPosition { OFF = 0,
        TOP = 1,
        BOTTOM = 2 };

    // Opcions de vídeo
    struct Video {
        bool gpu_acceleration{Defaults::Video::GPU_ACCELERATION};
        bool shader_enabled{Defaults::Video::SHADER_ENABLED};
        bool supersampling{Defaults::Video::SUPERSAMPLING};
        bool integer_scale{Defaults::Video::INTEGER_SCALE};
        bool aspect_ratio_4_3{Defaults::Video::ASPECT_RATIO_4_3};
        bool stretch_filter_linear{Defaults::Video::STRETCH_FILTER_LINEAR};
        int downscale_algo{Defaults::Video::DOWNSCALE_ALGO};
        bool linear_upscale{Defaults::Video::LINEAR_UPSCALE};
        std::string current_shader{"postfx"};         // "postfx" o "crtpi"
        std::string current_postfx_preset{"CRT"};     // Nom del preset PostFX actiu
        std::string current_crtpi_preset{"DEFAULT"};  // Nom del preset CrtPi actiu
    };

    // Opcions del render info
    struct RenderInfo {
        RenderInfoPosition position{RenderInfoPosition::OFF};
        Uint32 text_color{0xFF00D7FF};    // Groc daurat (ABGR)
        Uint32 shadow_color{0xFF005A6B};  // Ombra daurada fosca (ABGR)
    };

    // Opcions d'àudio
    struct Audio {
        bool music_enabled{Defaults::Audio::MUSIC_ENABLED};
        float music_volume{Defaults::Audio::MUSIC_VOLUME};
        bool sound_enabled{Defaults::Audio::SOUND_ENABLED};
        float sound_volume{Defaults::Audio::SOUND_VOLUME};
        float volume{Defaults::Audio::VOLUME};
    };

    // Opcions de finestra
    struct Window {
        int zoom{Defaults::Window::ZOOM};
        bool fullscreen{Defaults::Window::FULLSCREEN};
    };

    // Opcions de joc
    struct Game {
        int habitacio_inicial{Defaults::Game::HABITACIO_INICIAL};
        int piramide_inicial{Defaults::Game::PIRAMIDE_INICIAL};
        int vides{Defaults::Game::VIDES};
    };

    // Preset PostFX
    struct PostFXPreset {
        std::string name;
        float vignette{0.6F};
        float scanlines{0.7F};
        float chroma{0.15F};
        float mask{0.0F};
        float gamma{0.0F};
        float curvature{0.0F};
        float bleeding{0.0F};
        float flicker{0.0F};
    };

    // Preset CrtPi
    struct CrtPiPreset {
        std::string name;
        float scanline_weight{6.0F};
        float scanline_gap_brightness{0.12F};
        float bloom_factor{3.5F};
        float input_gamma{2.4F};
        float output_gamma{2.2F};
        float mask_brightness{0.80F};
        float curvature_x{0.05F};
        float curvature_y{0.10F};
        int mask_type{2};
        bool enable_scanlines{true};
        bool enable_multisample{true};
        bool enable_gamma{true};
        bool enable_curvature{false};
        bool enable_sharper{false};
    };

    // --- Variables globals ---
    inline std::string version{};
    inline KeysGUI keys_gui{};
    inline KeysGame keys_game{};
    inline Video video{};
    inline RenderInfo render_info{};
    inline Audio audio{};
    inline Window window{};
    inline Game game{};
    inline std::string config_file_path{};

    // Presets de shaders
    inline std::vector<PostFXPreset> postfx_presets{};
    inline std::string postfx_file_path{};
    inline int current_postfx_preset{0};

    inline std::vector<CrtPiPreset> crtpi_presets{};
    inline std::string crtpi_file_path{};
    inline int current_crtpi_preset{0};

    // --- API ---
    void setConfigFile(const std::string& path);
    auto loadFromFile() -> bool;
    auto saveToFile() -> bool;

    void setPostFXFile(const std::string& path);
    auto loadPostFXFromFile() -> bool;

    void setCrtPiFile(const std::string& path);
    auto loadCrtPiFromFile() -> bool;

}  // namespace Options
