#include "core/input/global_inputs.hpp"

#include <cstdio>
#include <string>

#include "core/jail/jinput.hpp"
#include "core/rendering/overlay.hpp"
#include "core/rendering/screen.hpp"
#include "game/options.hpp"

namespace GlobalInputs {

    static bool dec_zoom_prev = false;
    static bool inc_zoom_prev = false;
    static bool fullscreen_prev = false;
    static bool shader_prev = false;
    static bool aspect_prev = false;
    static bool ss_prev = false;
    static bool next_shader_prev = false;
    static bool next_preset_prev = false;
    static bool stretch_filter_prev = false;
    static bool render_info_prev = false;

    auto handle() -> bool {
        bool consumed = false;

        // F1 — Reduir zoom
        bool dec_zoom = JI_KeyPressed(Options::keys_gui.dec_zoom);
        if (dec_zoom && !dec_zoom_prev) {
            Screen::get()->decZoom();
            char msg[32];
            snprintf(msg, sizeof(msg), "ZOOM %dx", Screen::get()->getZoom());
            Overlay::showNotification(msg);
        }
        if (dec_zoom) consumed = true;
        dec_zoom_prev = dec_zoom;

        // F2 — Augmentar zoom
        bool inc_zoom = JI_KeyPressed(Options::keys_gui.inc_zoom);
        if (inc_zoom && !inc_zoom_prev) {
            Screen::get()->incZoom();
            char msg[32];
            snprintf(msg, sizeof(msg), "ZOOM %dx", Screen::get()->getZoom());
            Overlay::showNotification(msg);
        }
        if (inc_zoom) consumed = true;
        inc_zoom_prev = inc_zoom;

        // F3 — Toggle pantalla completa
        bool fullscreen = JI_KeyPressed(Options::keys_gui.fullscreen);
        if (fullscreen && !fullscreen_prev) {
            Screen::get()->toggleFullscreen();
            Overlay::showNotification(Screen::get()->isFullscreen() ? "PANTALLA COMPLETA" : "FINESTRA");
        }
        if (fullscreen) consumed = true;
        fullscreen_prev = fullscreen;

        // F4 — Toggle shaders
        bool shader = JI_KeyPressed(Options::keys_gui.toggle_shader);
        if (shader && !shader_prev) {
            Screen::get()->toggleShaders();
            Overlay::showNotification(Options::video.shader_enabled ? "SHADER ON" : "SHADER OFF");
        }
        if (shader) consumed = true;
        shader_prev = shader;

        // F5 — Toggle aspect ratio 4:3
        bool aspect = JI_KeyPressed(Options::keys_gui.toggle_aspect_ratio);
        if (aspect && !aspect_prev) {
            Screen::get()->toggleAspectRatio();
            Overlay::showNotification(Options::video.aspect_ratio_4_3 ? "4:3 CRT" : "PIXELS QUADRATS");
        }
        if (aspect) consumed = true;
        aspect_prev = aspect;

        // F6 — Toggle supersampling
        bool ss = JI_KeyPressed(Options::keys_gui.toggle_supersampling);
        if (ss && !ss_prev) {
            Screen::get()->toggleSupersampling();
            Overlay::showNotification(Options::video.supersampling ? "SUPERSAMPLING ON" : "SUPERSAMPLING OFF");
        }
        if (ss) consumed = true;
        ss_prev = ss;

        // F7 — Canviar tipus de shader (PostFX ↔ CrtPi)
        bool next_shader = JI_KeyPressed(Options::keys_gui.next_shader);
        if (next_shader && !next_shader_prev) {
            Screen::get()->nextShaderType();
            char msg[64];
            snprintf(msg, sizeof(msg), "%s: %s", Screen::get()->getActiveShaderName(), Screen::get()->getCurrentPresetName());
            Overlay::showNotification(msg);
        }
        if (next_shader) consumed = true;
        next_shader_prev = next_shader;

        // F8 — Pròxim preset del shader actiu
        bool next_preset = JI_KeyPressed(Options::keys_gui.next_shader_preset);
        if (next_preset && !next_preset_prev) {
            Screen::get()->nextPreset();
            char msg[64];
            snprintf(msg, sizeof(msg), "PRESET: %s", Screen::get()->getCurrentPresetName());
            Overlay::showNotification(msg);
        }
        if (next_preset) consumed = true;
        next_preset_prev = next_preset;

        // F9 — Toggle filtre d'estirament 4:3 (NEAREST ↔ LINEAR)
        bool stretch_filter = JI_KeyPressed(Options::keys_gui.toggle_stretch_filter);
        if (stretch_filter && !stretch_filter_prev) {
            Screen::get()->toggleStretchFilter();
            Overlay::showNotification(Options::video.stretch_filter_linear ? "FILTRE: LINEAR" : "FILTRE: NEAREST");
        }
        if (stretch_filter) consumed = true;
        stretch_filter_prev = stretch_filter;

        // F10 — Toggle render info (FPS, driver, shader)
        bool render_info = JI_KeyPressed(Options::keys_gui.toggle_render_info);
        if (render_info && !render_info_prev) {
            Overlay::toggleRenderInfo();
        }
        if (render_info) consumed = true;
        render_info_prev = render_info;

        return consumed;
    }

}  // namespace GlobalInputs
