#include "game/scenes/game.hpp"

#include <SDL3/SDL.h>

#include <cmath>    // Para std::sqrt, std::min
#include <numeric>  // Para std::accumulate
#include <utility>
#include <vector>  // Para vector

#include "core/audio/audio.hpp"               // Para Audio
#include "core/input/global_inputs.hpp"       // Para check
#include "core/input/input.hpp"               // Para Input, InputAction, Input::DO_NOT_ALLOW_REPEAT
#include "core/locale/locale.hpp"             // Para Locale
#include "core/rendering/screen.hpp"          // Para Screen
#include "core/rendering/surface.hpp"         // Para Surface
#include "core/rendering/text.hpp"            // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
#include "core/resources/resource_cache.hpp"  // Para ResourceRoom, Resource
#include "core/resources/resource_list.hpp"   // Para Asset
#include "core/system/global_events.hpp"      // Para check
#include "game/defaults.hpp"                  // Para Defaults::Game
#include "game/game_control.hpp"              // Para GameControl
#include "game/gameplay/cheevos.hpp"          // Para Cheevos
#include "game/gameplay/item_tracker.hpp"     // Para ItemTracker
#include "game/gameplay/room.hpp"             // Para Room, RoomData
#include "game/gameplay/room_tracker.hpp"     // Para RoomTracker
#include "game/gameplay/scoreboard.hpp"       // Para Scoreboard::Data, Scoreboard
#include "game/gameplay/stats.hpp"            // Para Stats
#include "game/options.hpp"                   // Para Options, options, Cheat, SectionState
#include "game/scene_manager.hpp"             // Para SceneManager
#include "game/ui/console.hpp"                // Para Console
#include "game/ui/notifier.hpp"               // Para Notifier, NotificationText, CHEEVO_NO...
#include "utils/defines.hpp"                  // Para Tile::SIZE, PlayArea::HEIGHT, RoomBorder::BOTTOM
#include "utils/utils.hpp"                    // Para PaletteColor, stringToColor

#ifdef _DEBUG
#include "core/system/debug.hpp"       // Para Debug
#include "game/editor/map_editor.hpp"  // Para MapEditor
#endif

// Constructor
Game::Game(Mode mode)
    : scoreboard_data_(std::make_shared<Scoreboard::Data>(0, 9, 0, true, 0, SDL_GetTicks(), Options::cheats.jail_is_open == Options::Cheat::State::ENABLED)),
      scoreboard_(std::make_shared<Scoreboard>(scoreboard_data_)),
      room_tracker_(std::make_shared<RoomTracker>()),
      stats_(std::make_shared<Stats>(Resource::List::get()->get("stats.csv"), Resource::List::get()->get("stats_buffer.csv"))),
      mode_(mode),
#ifdef _DEBUG
      current_room_(Debug::get()->getSpawnSettings().room),
      spawn_data_(Player::SpawnData(Debug::get()->getSpawnSettings().spawn_x, Debug::get()->getSpawnSettings().spawn_y, 0, 0, 0, Player::State::ON_GROUND, Debug::get()->getSpawnSettings().flip)) {
#else
      current_room_(Defaults::Game::Room::INITIAL),
      spawn_data_(Player::SpawnData(Defaults::Game::Player::SPAWN_X, Defaults::Game::Player::SPAWN_Y, 0, 0, 0, Player::State::ON_GROUND, Defaults::Game::Player::SPAWN_FLIP)) {
#endif

#ifdef _DEBUG
    // Validar que la room de debug existe; si no, fallback a la default
    if (Resource::List::get()->get(current_room_).empty()) {
        SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Debug room %s not found, using default", current_room_.c_str());
        current_room_ = Defaults::Game::Room::INITIAL;
        spawn_data_ = Player::SpawnData(Defaults::Game::Player::SPAWN_X, Defaults::Game::Player::SPAWN_Y, 0, 0, 0, Player::State::ON_GROUND, Defaults::Game::Player::SPAWN_FLIP);
        auto ss = Debug::get()->getSpawnSettings();
        ss.room = current_room_;
        Debug::get()->setSpawnSettings(ss);
        Debug::get()->saveToFile();
    }
#endif

    // Crea objetos e inicializa variables
    ItemTracker::init();
    demoInit();
    room_ = std::make_shared<Room>(current_room_, scoreboard_data_);
    initPlayer(spawn_data_, room_);
    initStats();
    total_items_ = getTotalItems();

    createRoomNameTexture();
    game_backbuffer_surface_ = std::make_shared<Surface>(Options::game.width, Options::game.height);
    changeRoom(current_room_);

    Cheevos::get()->enable(!Options::cheats.enabled());  // Deshabilita los logros si hay trucos activados
    Cheevos::get()->clearUnobtainableState();

#ifdef _DEBUG
    Console::get()->setScope("debug");
#else
    Console::get()->setScope("game");
#endif
    Console::get()->on_toggle = [this](bool open) { player_->setIgnoreInput(open); };
    if (Console::get()->isActive()) { player_->setIgnoreInput(true); }
    GameControl::change_player_skin = [this](const std::string& skin_name) -> void {
        Options::game.player_skin = skin_name;
        player_->setSkin(skin_name);
        scoreboard_->refreshPlayerSkin();
    };
    GameControl::change_player_color = [this](int color) -> void {
        Options::game.player_color = color;
        player_->setColor();
    };

#ifdef _DEBUG
    GameControl::change_room = [this](const std::string& r) -> bool { return this->changeRoom(r); };
    GameControl::get_current_room = [this]() -> std::string { return current_room_; };
    GameControl::set_items = [this](int count) -> void {
        scoreboard_data_->items = count;
        Options::stats.items = count;
    };
    GameControl::toggle_debug_mode = [this]() -> void {
        const bool ENTERING_DEBUG = !Debug::get()->isEnabled();
        if (ENTERING_DEBUG) {
            invincible_before_debug_ = (Options::cheats.invincible == Options::Cheat::State::ENABLED);
        }
        Debug::get()->toggleEnabled();
        room_->redrawMap();
        if (Debug::get()->isEnabled()) {
            Options::cheats.invincible = Options::Cheat::State::ENABLED;
        } else {
            Options::cheats.invincible = invincible_before_debug_ ? Options::Cheat::State::ENABLED : Options::Cheat::State::DISABLED;
        }
        player_->setColor();
        scoreboard_data_->music = !Debug::get()->isEnabled();
        scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
    };
    GameControl::set_initial_room = [this]() -> std::string {
        auto ss = Debug::get()->getSpawnSettings();
        ss.room = current_room_;
        Debug::get()->setSpawnSettings(ss);
        Debug::get()->saveToFile();
        const std::string ROOM_NUM = ss.room.substr(0, ss.room.find('.'));
        return "Room:" + ROOM_NUM;
    };
    GameControl::set_initial_pos = [this]() -> std::string {
        auto rect = player_->getRect();
        int tile_x = static_cast<int>((rect.x + (rect.w / 2.0F)) / Tile::SIZE);
        int tile_y = static_cast<int>(rect.y / Tile::SIZE);
        auto ss = Debug::get()->getSpawnSettings();
        ss.spawn_x = tile_x * Tile::SIZE;
        ss.spawn_y = tile_y * Tile::SIZE;
        ss.flip = player_->getSpawnParams().flip;
        Debug::get()->setSpawnSettings(ss);
        Debug::get()->saveToFile();
        return "Pos:" + std::to_string(tile_x) + "," + std::to_string(tile_y);
    };
    GameControl::enter_editor = [this]() -> void {
        MapEditor::get()->enter(room_, player_, current_room_, scoreboard_data_);
    };
    GameControl::exit_editor = [this]() -> void {
        MapEditor::get()->exit();
        // Recargar la habitación desde disco (con los cambios del editor)
        Resource::Cache::get()->reloadRoom(current_room_);
        changeRoom(current_room_);
        player_->setRoom(room_);
    };
    GameControl::revert_editor = []() -> std::string {
        return MapEditor::get()->revert();
    };
    GameControl::get_adjacent_room = [this](const std::string& direction) -> std::string {
        if (direction == "UP") { return room_->getRoom(Room::Border::TOP); }
        if (direction == "DOWN") { return room_->getRoom(Room::Border::BOTTOM); }
        if (direction == "LEFT") { return room_->getRoom(Room::Border::LEFT); }
        if (direction == "RIGHT") { return room_->getRoom(Room::Border::RIGHT); }
        return "0";
    };
#endif

    SceneManager::current = (mode_ == Mode::GAME) ? SceneManager::Scene::GAME : SceneManager::Scene::DEMO;
    SceneManager::options = SceneManager::Options::NONE;

    // Arranca la música del juego (abans a run())
    keepMusicPlaying();
    if (!scoreboard_data_->music && mode_ == Mode::GAME) {
        Audio::get()->pauseMusic();
    }
}

Game::~Game() {
    if (mode_ == Mode::GAME) {
        Audio::get()->stopMusic();
    }

    ItemTracker::destroy();

    GameControl::change_player_skin = nullptr;
    GameControl::change_player_color = nullptr;
    Console::get()->on_toggle = nullptr;

#ifdef _DEBUG
    GameControl::change_room = nullptr;
    GameControl::get_current_room = nullptr;
    GameControl::set_items = nullptr;
    GameControl::toggle_debug_mode = nullptr;
    GameControl::set_initial_room = nullptr;
    GameControl::set_initial_pos = nullptr;
    if (MapEditor::get()->isActive()) { MapEditor::get()->exit(); }
    GameControl::enter_editor = nullptr;
    GameControl::exit_editor = nullptr;
    GameControl::revert_editor = nullptr;
    GameControl::reload_current_room = nullptr;
    GameControl::get_adjacent_room = nullptr;
#endif
}

// Despatx d'un event (SDL3 Callback API)
void Game::handleEvent(const SDL_Event& event) {
    GlobalEvents::handle(event);
#ifdef _DEBUG
    // En modo editor: click del ratón cierra la consola
    if (Console::get()->isActive() && MapEditor::get()->isActive() &&
        event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
        Console::get()->toggle();
    }

    if (!Console::get()->isActive()) {
        // Tecla 9: toggle editor (funciona tanto dentro como fuera del editor)
        if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_9 && static_cast<int>(event.key.repeat) == 0) {
            if (MapEditor::get()->isActive()) {
                GameControl::exit_editor();
                Notifier::get()->show({Locale::get()->get("game.editor_disabled")});  // NOLINT(readability-static-accessed-through-instance)
            } else {
                GameControl::enter_editor();
                Notifier::get()->show({Locale::get()->get("game.editor_enabled")});  // NOLINT(readability-static-accessed-through-instance)
            }
        } else if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_8 && static_cast<int>(event.key.repeat) == 0 && MapEditor::get()->isActive()) {
            MapEditor::get()->showGrid(!MapEditor::get()->isGridEnabled());
        } else if (MapEditor::get()->isActive()) {
            MapEditor::get()->handleEvent(event);
        } else {
            handleDebugEvents(event);
        }
    }
#endif
}

// Comprueba el teclado
void Game::handleInput() {
    Input::get()->update();

    // Inputs globales siempre funcionan
    if (Input::get()->checkAction(InputAction::TOGGLE_IN_GAME_MUSIC, Input::DO_NOT_ALLOW_REPEAT)) {
        scoreboard_data_->music = !scoreboard_data_->music;
        scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
        Notifier::get()->show({scoreboard_data_->music ? Locale::get()->get("game.music_enabled") : Locale::get()->get("game.music_disabled")});
    }

    // Si la consola está activa, no procesar inputs del juego
    if (Console::get()->isActive()) {
        GlobalInputs::handle();
        return;
    }

#ifdef _DEBUG
    // Si el editor de mapas está activo, no procesar inputs del juego
    if (MapEditor::get()->isActive()) {
        GlobalInputs::handle();
        return;
    }
#endif

    // Durante fade/postfade, solo procesar inputs globales
    if (state_ != State::PLAYING) {
        GlobalInputs::handle();
        return;
    }

    // Input de pausa solo en estado PLAYING
    if (Input::get()->checkAction(InputAction::PAUSE, Input::DO_NOT_ALLOW_REPEAT)) {  // NOLINT(readability-static-accessed-through-instance)
        togglePause();
        Notifier::get()->show({paused_ ? Locale::get()->get("game.paused") : Locale::get()->get("game.running")});  // NOLINT(readability-static-accessed-through-instance)
    }

    GlobalInputs::handle();
}

// Un frame de l'escena (SDL3 Callback API)
void Game::iterate() {
    update();
    render();
}

// Actualiza el juego, las variables, comprueba la entrada, etc.
void Game::update() {
    const float DELTA_TIME = delta_timer_.tick();

    handleInput();  // Comprueba las entradas

#ifdef _DEBUG
    Debug::get()->clear();
#endif

    // Dispatch por estado
    switch (state_) {
        case State::PLAYING:
            updatePlaying(DELTA_TIME);
            break;
        case State::BLACK_SCREEN:
            updateBlackScreen(DELTA_TIME);
            break;
        case State::GAME_OVER:
            updateGameOver(DELTA_TIME);
            break;
        case State::FADE_TO_ENDING:
            updateFadeToEnding(DELTA_TIME);
            break;
        case State::POST_FADE_ENDING:
            updatePostFadeEnding(DELTA_TIME);
            break;
    }

    Audio::update();                    // Actualiza el objeto Audio
    Screen::get()->update(DELTA_TIME);  // Actualiza el objeto Screen
}

// Actualiza el juego en estado PLAYING
void Game::updatePlaying(float delta_time) {
#ifdef _DEBUG
    // Si el editor de mapas está activo, delegar en él y no ejecutar gameplay
    if (MapEditor::get()->isActive()) {
        MapEditor::get()->update(delta_time);
        return;
    }
#endif

    // Actualiza los objetos
    room_->update(delta_time);
    switch (mode_) {
        case Mode::GAME:
#ifdef _DEBUG
            // Maneja el arrastre del jugador con el ratón (debug)
            handleDebugMouseDrag(delta_time);

            // Si estamos arrastrando, no ejecutar la física normal del jugador
            if (!debug_dragging_player_) {
                player_->update(delta_time);
            }
#else
            player_->update(delta_time);
#endif
            checkPlayerIsOnBorder();
            checkPlayerAndItems();
            checkPlayerAndEnemies();
            checkIfPlayerIsAlive();
            checkEndGame();
            checkRestoringJail(delta_time);
            checkSomeCheevos();
            break;

        case Mode::DEMO:
            demoCheckRoomChange(delta_time);
            break;
    }
    scoreboard_->update(delta_time);
    keepMusicPlaying();
}

// Actualiza el juego en estado BLACK_SCREEN
void Game::updateBlackScreen(float delta_time) {
    state_time_ += delta_time;

    // Si se acabaron las vidas Y pasó el threshold → GAME_OVER
    if (scoreboard_data_->lives < 0 && state_time_ > GAME_OVER_THRESHOLD) {
        transitionToState(State::GAME_OVER);
        return;
    }

    // Si pasó la duración completa → volver a PLAYING
    if (state_time_ > BLACK_SCREEN_DURATION) {
        // Despausar al salir
        player_->setPaused(false);
        room_->setPaused(false);
        Screen::get()->setBorderColor(room_->getBorderColor());
        transitionToState(State::PLAYING);
    }
}

// Actualiza el juego en estado GAME_OVER
void Game::updateGameOver(float delta_time) {
    // Pequeño delay antes de cambiar escena
    state_time_ += delta_time;
    if (state_time_ > 0.1F) {  // 100ms de delay mínimo
        SceneManager::current = SceneManager::Scene::GAME_OVER;
    }
}

// Actualiza el juego en estado FADE_TO_ENDING
void Game::updateFadeToEnding(float delta_time) {
    // Actualiza room, enemies, items (todo sigue funcionando)
    room_->update(delta_time);

    // NO actualizar player (congelar movimiento)
    // player_->update(delta_time); -- COMENTADO INTENCIONALMENTE

    // Actualiza scoreboard
    scoreboard_->update(delta_time);
    keepMusicPlaying();

    // Aplica el fade progresivo al BACKBUFFER (no al renderer de pantalla)
    fade_accumulator_ += delta_time;
    if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
        fade_accumulator_ = 0.0F;
        if (game_backbuffer_surface_->fadeSubPalette()) {
            // Fade completado, transicionar a POST_FADE
            transitionToState(State::POST_FADE_ENDING);
        }
    }
}

// Actualiza el juego en estado POST_FADE_ENDING
void Game::updatePostFadeEnding(float delta_time) {
    // Pantalla negra estática, acumular tiempo
    state_time_ += delta_time;

    // Después del delay, cambiar a la escena de ending
    if (state_time_ >= POST_FADE_DELAY) {
        SceneManager::current = SceneManager::Scene::ENDING;
    }
}

// Cambia al estado especificado y resetea los timers
void Game::transitionToState(State new_state) {
    // Lógica de ENTRADA según el nuevo estado
    if (new_state == State::BLACK_SCREEN) {
        // Respawn room y player
        room_ = std::make_shared<Room>(current_room_, scoreboard_data_);
        initPlayer(spawn_data_, room_);
        // Pausar ambos
        room_->setPaused(true);
        player_->setPaused(true);
    }

    state_ = new_state;
    state_time_ = 0.0F;
    fade_accumulator_ = 0.0F;
}

// Pinta los objetos en pantalla
void Game::render() {
    // Dispatch por estado
    switch (state_) {
        case State::PLAYING:
            renderPlaying();
            break;
        case State::BLACK_SCREEN:
            renderBlackScreen();
            break;
        case State::GAME_OVER:
            renderGameOver();
            break;
        case State::FADE_TO_ENDING:
            renderFadeToEnding();
            break;
        case State::POST_FADE_ENDING:
            renderPostFadeEnding();
            break;
    }
}

// Renderiza el juego en estado PLAYING (directo a pantalla)
void Game::renderPlaying() {
    // Prepara para dibujar el frame
    Screen::get()->start();

    // Dibuja el mapa de tiles (siempre)
    room_->renderMap();

#ifdef _DEBUG
    // Si el editor está activo, delegar el renderizado de entidades y statusbar
    if (MapEditor::get()->isActive()) {
        MapEditor::get()->render();
        renderRoomName();
        Screen::get()->render();
        return;
    }
#endif

    // Dibuja los elementos del juego en orden
    room_->renderEnemies();
    room_->renderItems();
    if (mode_ == Mode::GAME) {
        player_->render();
    }
    renderRoomName();
    scoreboard_->render();

#ifdef _DEBUG
    // Debug info
    renderDebugInfo();
#endif

    // Actualiza la pantalla
    Screen::get()->render();
}

// Renderiza el juego en estado BLACK_SCREEN (pantalla negra)
void Game::renderBlackScreen() {
    Screen::get()->start();
    auto const COLOR = static_cast<Uint8>(PaletteColor::BLACK);
    Screen::get()->clearSurface(COLOR);
    Screen::get()->setBorderColor(COLOR);
    Screen::get()->render();
}

// Renderiza el juego en estado GAME_OVER (pantalla negra)
void Game::renderGameOver() {
    Screen::get()->start();
    Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
    Screen::get()->render();
}

// Renderiza el juego en estado FADE_TO_ENDING (via backbuffer)
void Game::renderFadeToEnding() {
    // 1. Guardar renderer actual
    auto previous_renderer = Screen::get()->getRendererSurface();

    // 2. Cambiar target a backbuffer
    Screen::get()->setRendererSurface(game_backbuffer_surface_);

    // 3. Renderizar todo a backbuffer
    game_backbuffer_surface_->clear(static_cast<Uint8>(PaletteColor::BLACK));
    room_->renderMap();
    room_->renderEnemies();
    room_->renderItems();
    player_->render();  // Player congelado pero visible
    renderRoomName();
    scoreboard_->render();

    // 4. Restaurar renderer original
    Screen::get()->setRendererSurface(previous_renderer);

    // 5. Preparar pantalla y volcar backbuffer (fade YA aplicado en update)
    Screen::get()->start();
    game_backbuffer_surface_->render();
    Screen::get()->render();
}

// Renderiza el juego en estado POST_FADE_ENDING (pantalla negra)
void Game::renderPostFadeEnding() {
    Screen::get()->start();
    Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
    Screen::get()->render();
}

#ifdef _DEBUG
// Helper: alterna un cheat y muestra notificación con su estado
static void toggleCheat(Options::Cheat::State& cheat, const std::string& label) {
    cheat = (cheat == Options::Cheat::State::ENABLED) ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
    const bool ENABLED = (cheat == Options::Cheat::State::ENABLED);
    Notifier::get()->show({label + (ENABLED ? Locale::get()->get("game.enabled") : Locale::get()->get("game.disabled"))}, Notifier::Style::DEFAULT, -1, true);  // NOLINT(readability-static-accessed-through-instance)
}

// Pone la información de debug en pantalla
void Game::renderDebugInfo() {
    if (!Debug::get()->isEnabled()) { return; }

    auto surface = Screen::get()->getRendererSurface();

    // Borra el marcador
    SDL_FRect rect = {.x = 0, .y = 18 * Tile::SIZE, .w = PlayArea::WIDTH, .h = GameCanvas::HEIGHT - PlayArea::HEIGHT};
    surface->fillRect(&rect, static_cast<Uint8>(PaletteColor::BLACK));

    // Pinta la rejilla
    /*for (int i = 0; i < PlayArea::BOTTOM; i += 8)
    {
            // Lineas horizontales
            surface->drawLine(0, i, PlayArea::RIGHT, i, static_cast<Uint8>(PaletteColor::BRIGHT_BLACK));
    }
    for (int i = 0; i < PlayArea::RIGHT; i += 8)
    {
            // Lineas verticales
            surface->drawLine(i, 0, i, PlayArea::BOTTOM - 1, static_cast<Uint8>(PaletteColor::BRIGHT_BLACK));
    }*/

    // Pinta el texto
    Debug::get()->setPos({.x = 1, .y = 18 * 8});
    Debug::get()->render();
}

// Comprueba los eventos
void Game::handleDebugEvents(const SDL_Event& event) {  // NOLINT(readability-convert-member-functions-to-static)
    if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
        switch (event.key.key) {
            case SDLK_R:
                Resource::Cache::get()->reload();
                break;

            case SDLK_W:
                changeRoom(room_->getRoom(Room::Border::TOP));
                break;

            case SDLK_A:
                changeRoom(room_->getRoom(Room::Border::LEFT));
                break;

            case SDLK_S:
                changeRoom(room_->getRoom(Room::Border::BOTTOM));
                break;

            case SDLK_D:
                changeRoom(room_->getRoom(Room::Border::RIGHT));
                break;

            case SDLK_1:
                toggleCheat(Options::cheats.infinite_lives, Locale::get()->get("game.cheat_infinite_lives"));  // NOLINT(readability-static-accessed-through-instance)
                player_->setColor();
                break;

            case SDLK_2:
                toggleCheat(Options::cheats.invincible, Locale::get()->get("game.cheat_invincible"));  // NOLINT(readability-static-accessed-through-instance)
                player_->setColor();
                break;

            case SDLK_3:
                toggleCheat(Options::cheats.jail_is_open, Locale::get()->get("game.cheat_jail_open"));  // NOLINT(readability-static-accessed-through-instance)
                break;

            case SDLK_7:
                Notifier::get()->show({Locale::get()->get("achievements.header"), Locale::get()->get("achievements.c11")}, Notifier::Style::CHEEVO, -1, false, "F7");  // NOLINT(readability-static-accessed-through-instance)
                break;

            case SDLK_0: {
                const bool ENTERING_DEBUG = !Debug::get()->isEnabled();
                if (ENTERING_DEBUG) {
                    invincible_before_debug_ = (Options::cheats.invincible == Options::Cheat::State::ENABLED);
                }
                Debug::get()->toggleEnabled();
                Notifier::get()->show({Debug::get()->isEnabled() ? Locale::get()->get("game.debug_enabled") : Locale::get()->get("game.debug_disabled")});  // NOLINT(readability-static-accessed-through-instance)
                room_->redrawMap();
                if (Debug::get()->isEnabled()) {
                    Options::cheats.invincible = Options::Cheat::State::ENABLED;
                } else {
                    Options::cheats.invincible = invincible_before_debug_ ? Options::Cheat::State::ENABLED : Options::Cheat::State::DISABLED;
                }
                player_->setColor();
                scoreboard_data_->music = !Debug::get()->isEnabled();
                scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
                break;
            }

            default:
                break;
        }
    }
}

// Maneja el arrastre del jugador con el ratón (debug)
void Game::handleDebugMouseDrag(float delta_time) {
    // Solo funciona si Debug está habilitado
    if (!Debug::get()->isEnabled()) {
        return;
    }

    // Obtener estado del ratón (coordenadas de ventana física)
    float mouse_x = 0.0F;
    float mouse_y = 0.0F;
    SDL_MouseButtonFlags buttons = SDL_GetMouseState(&mouse_x, &mouse_y);

    // Convertir coordenadas de ventana a coordenadas lógicas del renderer
    float render_x = 0.0F;
    float render_y = 0.0F;
    SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);

    // Restar el offset del borde para obtener coordenadas del área de juego
    SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
    float game_x = render_x - dst_rect.x;
    float game_y = render_y - dst_rect.y;

    // Verificar si los botones están presionados
    bool left_button_pressed = (buttons & SDL_BUTTON_LMASK) != 0;
    bool right_button_pressed = (buttons & SDL_BUTTON_RMASK) != 0;

    // Botón derecho: teleport instantáneo a la posición del cursor
    if (right_button_pressed && !debug_dragging_player_) {
        player_->setDebugPosition(game_x, game_y);
        player_->finalizeDebugTeleport();
    } else if (left_button_pressed) {
        // Obtener posición actual del jugador
        SDL_FRect player_rect = player_->getRect();
        float player_x = player_rect.x;
        float player_y = player_rect.y;

        // Calcular distancia al objetivo
        float dx = game_x - player_x;
        float dy = game_y - player_y;
        float distance = std::sqrt((dx * dx) + (dy * dy));

        // Constantes de velocidad con ease-in (aceleración progresiva)
        constexpr float DRAG_SPEED_MIN = 30.0F;      // Velocidad inicial (pixels/segundo)
        constexpr float DRAG_SPEED_MAX = 600.0F;     // Velocidad máxima (pixels/segundo)
        constexpr float DRAG_ACCELERATION = 600.0F;  // Aceleración (pixels/segundo²)

        // Incrementar velocidad con el tiempo (ease-in)
        if (!debug_dragging_player_) {
            debug_drag_speed_ = DRAG_SPEED_MIN;  // Iniciar con velocidad mínima
        }
        debug_drag_speed_ = std::min(DRAG_SPEED_MAX, debug_drag_speed_ + (DRAG_ACCELERATION * delta_time));

        if (distance > 1.0F) {
            // Calcular el movimiento con la velocidad actual
            float move_factor = std::min(1.0F, debug_drag_speed_ * delta_time / distance);
            float new_x = player_x + (dx * move_factor);
            float new_y = player_y + (dy * move_factor);

            // Mover el jugador hacia la posición del cursor
            player_->setDebugPosition(new_x, new_y);
        }

        debug_dragging_player_ = true;
        Debug::get()->set("drag.x", std::to_string(static_cast<int>(player_rect.x)));
        Debug::get()->set("drag.y", std::to_string(static_cast<int>(player_rect.y)));
    } else if (debug_dragging_player_) {
        // Botón soltado después de arrastrar: finalizar teleport
        player_->finalizeDebugTeleport();
        debug_dragging_player_ = false;
        debug_drag_speed_ = 0.0F;  // Reset para el próximo arrastre
    }
}
#endif

// Escribe el nombre de la pantalla
void Game::renderRoomName() {
    // Dibuja la textura con el nombre de la habitación
    room_name_surface_->render(nullptr, &room_name_rect_);
}

// Cambia de habitación
auto Game::changeRoom(const std::string& room_path) -> bool {
    // En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
    if (room_path == "0") {
        return false;
    }

    // Verifica que exista el fichero que se va a cargar
    if (!Resource::List::get()->get(room_path).empty()) {  // NOLINT(readability-static-accessed-through-instance)
        // Crea un objeto habitación nuevo a partir del fichero
        room_ = std::make_shared<Room>(room_path, scoreboard_data_);

        // Pone el nombre de la habitación en la textura
        fillRoomNameTexture();

        // Pone el color del marcador en función del color del borde de la habitación
        setScoreBoardColor();

        if (room_tracker_->addRoom(room_path)) {
            // Incrementa el contador de habitaciones visitadas
            scoreboard_data_->rooms++;
            Options::stats.rooms = scoreboard_data_->rooms;

            // Actualiza las estadisticas
            stats_->addVisit(room_->getName());
        }

        // Pasa la nueva habitación al jugador
        player_->setRoom(room_);

        // Recalcula el color del jugador (evita coincidir con el fondo)
        player_->setColor();

        // Cambia la habitación actual
        current_room_ = room_path;

        return true;
    }

    return false;
}

// Comprueba si el jugador esta en el borde de la pantalla
void Game::checkPlayerIsOnBorder() {
    if (player_->isOnBorder()) {
        const auto BORDER = player_->getBorder();
        const auto ROOM_NAME = room_->getRoom(BORDER);

        // Si puede cambiar de habitación, cambia
        if (changeRoom(ROOM_NAME)) {
            player_->switchBorders();
            spawn_data_ = player_->getSpawnParams();
            return;
        }

        // Si ha llegado al fondo y no hay habitación, muere
        if (BORDER == Room::Border::BOTTOM) {
            killPlayer();
            return;
        }
    }
}

// Comprueba las colisiones del jugador con los enemigos
auto Game::checkPlayerAndEnemies() -> bool {
    const bool DEATH = room_->enemyCollision(player_->getCollider());
    if (DEATH) {
        killPlayer();
    }
    return DEATH;
}

// Comprueba las colisiones del jugador con los objetos
void Game::checkPlayerAndItems() {
    room_->itemCollision(player_->getCollider());
}

// Comprueba si el jugador esta vivo
void Game::checkIfPlayerIsAlive() {
    if (!player_->isAlive()) {
        killPlayer();
    }
}

// Mata al jugador
void Game::killPlayer() {
    if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
        return;
    }

    // Resta una vida al jugador
    if (Options::cheats.infinite_lives == Options::Cheat::State::DISABLED &&
        !(Options::kiosk.enabled && Options::kiosk.infinite_lives)) {
        --scoreboard_data_->lives;
    }

    // Actualiza las estadisticas
    stats_->addDeath(room_->getName());

    // Invalida el logro de pasarse el juego sin morir
    Cheevos::get()->setUnobtainable(11);

    // Sonido
    Audio::get()->playSound("death.wav", Audio::Group::GAME);

    // Transicionar al estado BLACK_SCREEN (el respawn ocurre en transitionToState)
    transitionToState(State::BLACK_SCREEN);
}

// Pone el color del marcador en función del color del borde de la habitación
void Game::setScoreBoardColor() {  // NOLINT(readability-convert-member-functions-to-static)
    // Obtiene el color del borde
    const Uint8 BORDER_COLOR = room_->getBorderColor();

    const bool IS_BLACK = BORDER_COLOR == static_cast<Uint8>(PaletteColor::BLACK);
    const bool IS_BRIGHT_BLACK = BORDER_COLOR == static_cast<Uint8>(PaletteColor::BRIGHT_BLACK);

    // Si el color del borde es negro o negro brillante cambia el texto del marcador a blanco
    scoreboard_data_->color = IS_BLACK || IS_BRIGHT_BLACK ? static_cast<Uint8>(PaletteColor::WHITE) : BORDER_COLOR;
}

// Comprueba si ha finalizado el juego
auto Game::checkEndGame() -> bool {
    const bool IS_ON_THE_ROOM = room_->getNumber() == Defaults::Game::Room::END_ROOM;  // Estar en la habitación que toca
    const bool HAVE_THE_ITEMS =
        scoreboard_data_->items >= int(total_items_ * Defaults::Game::Items::PERCENT_TO_OPEN_THE_JAIL) ||
        Options::cheats.jail_is_open == Options::Cheat::State::ENABLED;  // Con mas del 90% de los items recogidos
    const bool IS_ON_THE_DOOR = player_->getRect().x <= 128;             // Y en la ubicación que toca (En la puerta)

    scoreboard_data_->jail_is_open = HAVE_THE_ITEMS;

    if (HAVE_THE_ITEMS && IS_ON_THE_ROOM && IS_ON_THE_DOOR) {
        // Comprueba los logros de completar el juego
        checkEndGameCheevos();

        // Iniciar transición de fade en vez de cambio inmediato de escena
        transitionToState(State::FADE_TO_ENDING);
        Audio::get()->fadeOutMusic(1000);  // Fade out de música en 1 segundo
        return true;
    }

    return false;
}

// Obtiene la cantidad total de items que hay en el mapeado del juego
auto Game::getTotalItems() -> int {
    const auto& rooms = Resource::Cache::get()->getRooms();
    return static_cast<int>(std::accumulate(rooms.begin(), rooms.end(), size_t{0}, [](size_t acc, const auto& room) { return acc + room.room->items.size(); }));
}

// Pone el juego en pausa
void Game::togglePause() {
    paused_ = !paused_;

    player_->setPaused(paused_);
    room_->setPaused(paused_);
    scoreboard_->setPaused(paused_);
}

// Da vidas al jugador cuando está en la Jail
void Game::checkRestoringJail(float delta_time) {
    if (room_->getName() != "THE JAIL" || scoreboard_data_->lives == 9) {
        jail_restore_time_ = 0.0F;  // Reset timer cuando no está en la Jail
        return;
    }

    if (!paused_) {
        jail_restore_time_ += delta_time;
    }

    // Incrementa el numero de vidas
    if (jail_restore_time_ >= JAIL_RESTORE_INTERVAL) {
        jail_restore_time_ -= JAIL_RESTORE_INTERVAL;  // Mantiene el excedente para precisión
        scoreboard_data_->lives++;
        Audio::get()->playSound("death.wav", Audio::Group::GAME);

        // Invalida el logro de completar el juego sin entrar a la jail
        const bool HAVE_THE_ITEMS = scoreboard_data_->items >= int(total_items_ * 0.9F);
        if (!HAVE_THE_ITEMS) {
            Cheevos::get()->setUnobtainable(9);
        }
    }
}

// Inicializa las estadísticas
void Game::initStats() {  // NOLINT(readability-convert-member-functions-to-static)
    stats_->init();
}

// Crea la textura con el nombre de la habitación
void Game::fillRoomNameTexture() {  // NOLINT(readability-convert-member-functions-to-static)
    // Pone la textura como destino de renderizado
    auto previuos_renderer = Screen::get()->getRendererSurface();
    Screen::get()->setRendererSurface(room_name_surface_);

    // Rellena la textura de color
    room_name_surface_->clear(stringToColor("white"));

    // Escribe el texto en la textura
    auto text = Resource::Cache::get()->getText("smb2");
    text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, text->getCharacterSize() / 2, room_->getName(), 1, room_->getBGColor());

    // Deja el renderizador por defecto
    Screen::get()->setRendererSurface(previuos_renderer);
}

// Comprueba algunos logros
void Game::checkSomeCheevos() {  // NOLINT(readability-convert-member-functions-to-static)
    auto* cheevos = Cheevos::get();

    // Logros sobre la cantidad de items
    if (scoreboard_data_->items == total_items_) {
        cheevos->unlock(4);
        cheevos->unlock(3);
        cheevos->unlock(2);
        cheevos->unlock(1);
    } else if (scoreboard_data_->items >= total_items_ * 0.75F) {
        cheevos->unlock(3);
        cheevos->unlock(2);
        cheevos->unlock(1);
    } else if (scoreboard_data_->items >= total_items_ * 0.5F) {
        cheevos->unlock(2);
        cheevos->unlock(1);
    } else if (scoreboard_data_->items >= total_items_ * 0.25F) {
        cheevos->unlock(1);
    }

    // Logros sobre las habitaciones visitadas
    if (scoreboard_data_->rooms >= 60) {
        cheevos->unlock(7);
        cheevos->unlock(6);
        cheevos->unlock(5);
    } else if (scoreboard_data_->rooms >= 40) {
        cheevos->unlock(6);
        cheevos->unlock(5);
    } else if (scoreboard_data_->rooms >= 20) {
        cheevos->unlock(5);
    }
}

// Comprueba los logros de completar el juego
void Game::checkEndGameCheevos() {  // NOLINT(readability-convert-member-functions-to-static)
    auto* cheevos = Cheevos::get();

    // "Complete the game"
    cheevos->unlock(8);

    // "Complete the game without entering the jail"
    cheevos->unlock(9);

    // "Complete the game with all items"
    if (scoreboard_data_->items == total_items_) {
        cheevos->unlock(10);
    }

    // "Complete the game without dying"
    cheevos->unlock(11);

    // "Complete the game in under 30 minutes"
    if (scoreboard_->getMinutes() < 30) {
        cheevos->unlock(12);
    }
}

// Inicializa al jugador
void Game::initPlayer(const Player::SpawnData& spawn_point, std::shared_ptr<Room> room) {  // NOLINT(readability-convert-member-functions-to-static)
    const bool IGNORE_INPUT = player_ != nullptr && player_->getIgnoreInput();
    std::string player_animations = Player::skinToAnimationPath(Options::game.player_skin);
    const Player::Data PLAYER{.spawn_data = spawn_point, .animations_path = player_animations, .room = std::move(room)};
    player_ = std::make_shared<Player>(PLAYER);
    if (IGNORE_INPUT) { player_->setIgnoreInput(true); }
}

// Crea la textura para poner el nombre de la habitación
void Game::createRoomNameTexture() {
    auto text = Resource::Cache::get()->getText("smb2");
    room_name_surface_ = std::make_shared<Surface>(Options::game.width, text->getCharacterSize() * 2);

    // Establece el destino de la textura
    room_name_rect_ = {.x = 0.0F, .y = PlayArea::HEIGHT, .w = Options::game.width, .h = text->getCharacterSize() * 2.0F};
}

// Hace sonar la música
void Game::keepMusicPlaying() {
    const std::string MUSIC_PATH = mode_ == Mode::GAME ? "game.ogg" : "title.ogg";

    // Si la música no está sonando
    if (Audio::get()->getMusicState() == Audio::MusicState::STOPPED) {
        Audio::get()->playMusic(MUSIC_PATH);
    }
}

// DEMO MODE: Inicializa las variables para el modo demo
void Game::demoInit() {
    if (mode_ == Mode::DEMO) {
        demo_ = DemoData(0.0F, 0, {"04.yaml", "54.yaml", "20.yaml", "09.yaml", "05.yaml", "11.yaml", "31.yaml", "44.yaml"});
        current_room_ = demo_.rooms.front();
    }
}

// DEMO MODE: Comprueba si se ha de cambiar de habitación
void Game::demoCheckRoomChange(float delta_time) {
    if (mode_ == Mode::DEMO) {
        demo_.time_accumulator += delta_time;
        if (demo_.time_accumulator >= DEMO_ROOM_DURATION) {
            demo_.time_accumulator = 0.0F;
            demo_.room_index++;
            if (demo_.room_index == (int)demo_.rooms.size()) {
                SceneManager::current = SceneManager::Scene::LOGO;
                SceneManager::options = SceneManager::Options::LOGO_TO_TITLE;
            } else {
                changeRoom(demo_.rooms[demo_.room_index]);
            }
        }
    }
}