#include "core/input/global_inputs.hpp"

#include <cstdio>
#include <functional>
#include <string>

#include "core/input/key_config.hpp"
#include "core/jail/jinput.hpp"
#include "core/locale/locale.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 texture_filter_prev = false;
    static bool render_info_prev = false;

    // Patró comú: lectura amb detecció de flanc + acumulació al flag "consumed".
    // `on_press` només s'executa al flanc puja; `prev` es manté actualitzat.
    static auto edgeTrigger(const char* key_id, bool& prev, const std::function<void()>& on_press) -> bool {
        const bool PRESSED = Ji::keyPressed(KeyConfig::scancode(key_id));
        if (PRESSED && !prev) {
            on_press();
        }
        prev = PRESSED;
        return PRESSED;
    }

    auto handle() -> bool {
        bool consumed = false;
        consumed |= edgeTrigger("dec_zoom", dec_zoom_prev, [] {
            Screen::get()->decZoom();
            char msg[32];
            snprintf(msg, sizeof(msg), Locale::get("notifications.zoom_fmt"), Screen::get()->getZoom());
            Overlay::showNotification(msg);
        });
        consumed |= edgeTrigger("inc_zoom", inc_zoom_prev, [] {
            Screen::get()->incZoom();
            char msg[32];
            snprintf(msg, sizeof(msg), Locale::get("notifications.zoom_fmt"), Screen::get()->getZoom());
            Overlay::showNotification(msg);
        });
        consumed |= edgeTrigger("fullscreen", fullscreen_prev, [] {
            Screen::get()->toggleFullscreen();
            Overlay::showNotification(Screen::get()->isFullscreen() ? Locale::get("notifications.fullscreen") : Locale::get("notifications.windowed"));
        });
        consumed |= edgeTrigger("toggle_shader", shader_prev, [] {
            Screen::get()->toggleShaders();
            Overlay::showNotification(Options::video.shader_enabled ? Locale::get("notifications.shader_on") : Locale::get("notifications.shader_off"));
        });
        consumed |= edgeTrigger("toggle_aspect_ratio", aspect_prev, [] {
            Screen::get()->toggleAspectRatio();
            Overlay::showNotification(Options::video.aspect_ratio_4_3 ? Locale::get("notifications.aspect_43") : Locale::get("notifications.aspect_square"));
        });
        consumed |= edgeTrigger("toggle_supersampling", ss_prev, [] {
            if (Screen::get()->toggleSupersampling()) {
                Overlay::showNotification(Options::video.supersampling ? Locale::get("notifications.ss_on") : Locale::get("notifications.ss_off"));
            }
        });
        consumed |= edgeTrigger("next_shader", next_shader_prev, [] {
            if (Screen::get()->nextShaderType()) {
                char msg[64];
                snprintf(msg, sizeof(msg), "%s: %s", Screen::get()->getActiveShaderName(), Screen::get()->getCurrentPresetName());
                Overlay::showNotification(msg);
            }
        });
        consumed |= edgeTrigger("next_shader_preset", next_preset_prev, [] {
            if (Screen::get()->nextPreset()) {
                char msg[64];
                snprintf(msg, sizeof(msg), Locale::get("notifications.preset_fmt"), Screen::get()->getCurrentPresetName());
                Overlay::showNotification(msg);
            }
        });
        consumed |= edgeTrigger("cycle_texture_filter", texture_filter_prev, [] {
            Screen::get()->cycleTextureFilter(+1);
            Overlay::showNotification(Options::video.texture_filter == Options::TextureFilter::LINEAR
                    ? Locale::get("notifications.filter_linear")
                    : Locale::get("notifications.filter_nearest"));
        });
        consumed |= edgeTrigger("toggle_render_info", render_info_prev, [] {
            Overlay::toggleRenderInfo();
        });
        return consumed;
    }

}  // namespace GlobalInputs
