// IWYU pragma: no_include <bits/std_abs.h>
#include "game/scenes/credits.hpp"

#include <SDL3/SDL.h>  // Para SDL_RenderFillRect, SDL_RenderTexture, SDL_SetRenderTarget, SDL_SetRenderDrawColor, SDL_CreateTexture, SDL_DestroyTexture, SDL_GetTicks, SDL_GetRenderTarget, SDL_PixelFormat, SDL_PollEvent, SDL_RenderClear, SDL_RenderRect, SDL_SetTextureBlendMode, SDL_TextureAccess, SDL_BLENDMODE_BLEND, SDL_Event, Uint64

#include <algorithm>    // Para max, min, clamp
#include <array>        // Para array
#include <cmath>        // Para abs
#include <stdexcept>    // Para runtime_error
#include <string>       // Para basic_string, string
#include <string_view>  // Para string_view
#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
#include "core/locale/lang.hpp"               // Para getText
#include "core/rendering/fade.hpp"            // Para Fade
#include "core/rendering/screen.hpp"          // Para Screen
#include "core/rendering/sprite/sprite.hpp"   // Para Sprite
#include "core/rendering/text.hpp"            // Para Text
#include "core/rendering/texture.hpp"         // Para Texture
#include "core/rendering/tiled_bg.hpp"        // Para TiledBG, TiledBGMode
#include "core/resources/resource.hpp"        // Para Resource
#include "core/system/global_events.hpp"      // Para handle
#include "core/system/section.hpp"            // Para Name, name
#include "game/entities/player.hpp"           // Para Player
#include "game/gameplay/balloon_manager.hpp"  // Para BalloonManager
#include "game/ui/service_menu.hpp"           // Para ServiceMenu
#include "utils/color.hpp"                    // Para Color, SHADOW_TEXT, NO_COLOR_MOD
#include "utils/param.hpp"                    // Para Param, param, ParamGame, ParamFade
#include "utils/utils.hpp"                    // Para Zone

// Textos
constexpr std::string_view TEXT_COPYRIGHT = "@2020,2025 JailDesigner";

// Constructor
Credits::Credits()
    : balloon_manager_(std::make_unique<BalloonManager>(nullptr)),
      tiled_bg_(std::make_unique<TiledBG>(param.game.game_area.rect, TiledBGMode::DIAGONAL)),
      fade_in_(std::make_unique<Fade>()),
      fade_out_(std::make_unique<Fade>()),
      text_texture_(SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, static_cast<int>(param.game.width), static_cast<int>(param.game.height))),
      canvas_(SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, static_cast<int>(param.game.width), static_cast<int>(param.game.height))) {
    if (text_texture_ == nullptr) {
        throw std::runtime_error("Failed to create SDL texture for text.");
    }
    initVars();
    startCredits();

    // Inicializa el timer de delta time para el primer frame del callback
    last_time_ = SDL_GetTicks();
}

// Destructor
Credits::~Credits() {
    SDL_DestroyTexture(text_texture_);
    SDL_DestroyTexture(canvas_);
    resetVolume();
    Audio::get()->stopMusic();

    // Desregistra los jugadores de Options
    Options::keyboard.clearPlayers();
    Options::gamepad_manager.clearPlayers();
}

// Calcula el deltatime
auto Credits::calculateDeltaTime() -> float {
    const Uint64 CURRENT_TIME = SDL_GetTicks();
    const float DELTA_TIME = static_cast<float>(CURRENT_TIME - last_time_) / 1000.0F;  // Convertir ms a segundos
    last_time_ = CURRENT_TIME;
    return DELTA_TIME;
}

// Avanza un frame (llamado desde Director::iterate)
void Credits::iterate() {
    checkInput();
    const float DELTA_TIME = calculateDeltaTime();
    update(DELTA_TIME);
    render();
}

// Procesa un evento (llamado desde Director::handleEvent)
void Credits::handleEvent(const SDL_Event& /*event*/) {
    // Eventos globales ya gestionados por Director::handleEvent
}

// Bucle principal legacy (fallback)
void Credits::run() {
    last_time_ = SDL_GetTicks();

    while (Section::name == Section::Name::CREDITS) {
        checkInput();
        const float DELTA_TIME = calculateDeltaTime();
        update(DELTA_TIME);
        checkEvents();  // Tiene que ir antes del render
        render();
    }
}

// Actualiza las variables (time-based puro - sin conversión frame-based)
void Credits::update(float delta_time) {
    const float MULTIPLIER = want_to_pass_ ? FAST_FORWARD_MULTIPLIER : 1.0F;
    const float ADJUSTED_DELTA_TIME = delta_time * MULTIPLIER;

    static auto* const SCREEN = Screen::get();
    SCREEN->update(delta_time);  // Actualiza el objeto screen
    Audio::update();             // Actualiza el objeto audio

    tiled_bg_->update(ADJUSTED_DELTA_TIME);
    cycleColors(ADJUSTED_DELTA_TIME);
    balloon_manager_->update(ADJUSTED_DELTA_TIME);
    updateTextureDstRects(ADJUSTED_DELTA_TIME);
    throwBalloons(ADJUSTED_DELTA_TIME);
    updatePlayers(ADJUSTED_DELTA_TIME);
    updateAllFades(ADJUSTED_DELTA_TIME);

    fillCanvas();
}

// Dibuja Credits::en patalla
void Credits::render() {
    static auto* const SCREEN = Screen::get();

    SCREEN->start();                                                      // Prepara para empezar a dibujar en la textura de juego
    SDL_RenderTexture(SCREEN->getRenderer(), canvas_, nullptr, nullptr);  // Copia la textura con la zona de juego a la pantalla
    SCREEN->render();                                                     // Vuelca el contenido del renderizador en pantalla
}

// Comprueba el manejador de eventos
void Credits::checkEvents() {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        GlobalEvents::handle(event);
    }
}

// Comprueba las entradas
void Credits::checkInput() {
    Input::get()->update();

    if (!ServiceMenu::get()->isEnabled()) {
        // Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
        if (Input::get()->checkAnyButton(Input::ALLOW_REPEAT)) {
            want_to_pass_ = true;
            fading_ = mini_logo_on_position_;
        } else {
            want_to_pass_ = false;
        }
    }

    // Comprueba los inputs que se pueden introducir en cualquier sección del juego
    GlobalInputs::check();
}

// Crea la textura con el texto
void Credits::fillTextTexture() {
    auto text = Resource::get()->getText("smb2");
    auto text_grad = Resource::get()->getText("smb2_grad");
    SDL_SetRenderTarget(Screen::get()->getRenderer(), text_texture_);

    SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0);
    SDL_RenderClear(Screen::get()->getRenderer());

    const std::array<std::string, 11> TEXTS = {
        Lang::getText("[CREDITS] PROGRAMMED_AND_DESIGNED_BY"),
        Lang::getText("[CREDITS] PIXELART_DRAWN_BY"),
        Lang::getText("[CREDITS] MUSIC_COMPOSED_BY"),
        Lang::getText("[CREDITS] SOUND_EFFECTS"),
        "JAILDESIGNER",
        "JAILDOCTOR",
        "ERIC MATYAS (SOUNDIMAGE.ORG)",
        "WWW.THEMOTIONMONKEY.CO.UK",
        "WWW.KENNEY.NL",
        "JAILDOCTOR",
        "JAILDESIGNER"};

    const int SPACE_POST_TITLE = 3 + text->getCharacterSize();
    const int SPACE_PRE_TITLE = text->getCharacterSize() * 4;
    const int TEXTS_HEIGHT = (1 * text->getCharacterSize()) + (8 * SPACE_POST_TITLE) + (3 * SPACE_PRE_TITLE);
    const int POS_X = static_cast<int>(param.game.game_area.center_x);
    credits_rect_dst_.h = credits_rect_src_.h = static_cast<float>(TEXTS_HEIGHT);
    auto text_style = Text::Style(Text::CENTER | Text::SHADOW, Colors::NO_COLOR_MOD, Colors::SHADOW_TEXT);

    // PROGRAMMED_AND_DESIGNED_BY
    int y = 0;
    text_grad->writeStyle(POS_X, y, TEXTS.at(0), text_style);

    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(4), text_style);

    // PIXELART_DRAWN_BY
    y += SPACE_PRE_TITLE;
    text_grad->writeStyle(POS_X, y, TEXTS.at(1), text_style);
    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(4), text_style);

    // MUSIC_COMPOSED_BY
    y += SPACE_PRE_TITLE;
    text_grad->writeStyle(POS_X, y, TEXTS.at(2), text_style);

    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(5), text_style);
    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(6), text_style);

    // SOUND_EFFECTS
    y += SPACE_PRE_TITLE;
    text_grad->writeStyle(POS_X, y, TEXTS.at(3), text_style);
    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(7), text_style);
    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(8), text_style);
    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(9), text_style);
    y += SPACE_POST_TITLE;
    text->writeStyle(POS_X, y, TEXTS.at(10), text_style);

    // Mini logo
    y += SPACE_PRE_TITLE;
    mini_logo_rect_src_.y = static_cast<float>(y);
    auto mini_logo_sprite = std::make_unique<Sprite>(Resource::get()->getTexture("logo_jailgames_mini.png"));
    mini_logo_sprite->setPosition(1 + POS_X - (mini_logo_sprite->getWidth() / 2), 1 + y);
    Resource::get()->getTexture("logo_jailgames_mini.png")->setColor(Colors::SHADOW_TEXT.r, Colors::SHADOW_TEXT.g, Colors::SHADOW_TEXT.b);
    mini_logo_sprite->render();

    mini_logo_sprite->setPosition(POS_X - (mini_logo_sprite->getWidth() / 2), y);
    Resource::get()->getTexture("logo_jailgames_mini.png")->setColor(255, 255, 255);
    mini_logo_sprite->render();

    // Texto con el copyright
    y += mini_logo_sprite->getHeight() + 3;
    text->writeDX(Text::CENTER | Text::SHADOW, POS_X, y, std::string(TEXT_COPYRIGHT), 1, Colors::NO_COLOR_MOD, 1, Colors::SHADOW_TEXT);

    // Resetea el renderizador
    SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr);

    // Actualiza las variables
    mini_logo_rect_dst_.h = mini_logo_rect_src_.h = mini_logo_sprite->getHeight() + 3 + text->getCharacterSize();
    credits_rect_dst_.y = param.game.game_area.rect.h;
    mini_logo_rect_dst_.y = credits_rect_dst_.y + credits_rect_dst_.h + 30;
    mini_logo_final_pos_ = param.game.game_area.center_y - (mini_logo_rect_src_.h / 2);
}

// Dibuja todos los sprites en la textura
void Credits::fillCanvas() {
    // Cambia el destino del renderizador
    auto* temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
    SDL_SetRenderTarget(Screen::get()->getRenderer(), canvas_);

    // Dibuja el fondo, los globos y los jugadores
    tiled_bg_->render();
    balloon_manager_->render();
    renderPlayers();

    // Dibuja los titulos de credito
    SDL_RenderTexture(Screen::get()->getRenderer(), text_texture_, &credits_rect_src_, &credits_rect_dst_);

    // Dibuja el mini_logo
    SDL_RenderTexture(Screen::get()->getRenderer(), text_texture_, &mini_logo_rect_src_, &mini_logo_rect_dst_);

    // Dibuja los rectangulos negros
    SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0xFF);
    SDL_RenderFillRect(Screen::get()->getRenderer(), &top_black_rect_);
    SDL_RenderFillRect(Screen::get()->getRenderer(), &bottom_black_rect_);
    SDL_RenderFillRect(Screen::get()->getRenderer(), &left_black_rect_);
    SDL_RenderFillRect(Screen::get()->getRenderer(), &right_black_rect_);

    // Dibuja el rectangulo rojo
    drawBorderRect();

    // Si el mini_logo está en su destino, lo dibuja encima de lo anterior
    if (mini_logo_on_position_) {
        SDL_RenderTexture(Screen::get()->getRenderer(), text_texture_, &mini_logo_rect_src_, &mini_logo_rect_dst_);
    }

    // Dibuja el fade sobre el resto de elementos
    fade_in_->render();
    fade_out_->render();

    // Deja el renderizador apuntando donde estaba
    SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
}

// Actualiza el destino de los rectangulos de las texturas (time-based puro)
void Credits::updateTextureDstRects(float delta_time) {
    constexpr float TEXTURE_UPDATE_INTERVAL_S = 10.0F / 60.0F;  // ~0.167s (cada 10 frames)
    credits_state_.texture_accumulator += delta_time;

    if (credits_state_.texture_accumulator >= TEXTURE_UPDATE_INTERVAL_S) {
        credits_state_.texture_accumulator -= TEXTURE_UPDATE_INTERVAL_S;

        // Comprueba la posición de la textura con los titulos de credito
        if (credits_rect_dst_.y + credits_rect_dst_.h > play_area_.y) {
            --credits_rect_dst_.y;
        }

        // Comprueba la posición de la textura con el mini_logo
        if (mini_logo_rect_dst_.y <= static_cast<float>(mini_logo_final_pos_)) {
            // Forzar posición exacta para evitar problemas de comparación float
            mini_logo_rect_dst_.y = static_cast<float>(mini_logo_final_pos_);
            mini_logo_on_position_ = true;
        } else {
            --mini_logo_rect_dst_.y;
        }
    }

    // Acumular tiempo desde que el logo llegó a su posición (fuera del if para que se ejecute cada frame)
    if (mini_logo_on_position_) {
        time_since_logo_positioned_ += delta_time;

        // Timeout para evitar que la sección sea infinita
        if (time_since_logo_positioned_ >= MAX_TIME_AFTER_LOGO_S) {
            fading_ = true;
        }

        // Si el jugador quiere pasar los titulos de credito, el fade se inicia solo
        if (want_to_pass_) {
            fading_ = true;
        }
    }
}

// Tira globos al escenario (time-based puro)
void Credits::throwBalloons(float delta_time) {
    constexpr int SPEED = 200;
    constexpr size_t NUM_SETS = 8;  // Tamaño del vector SETS
    const std::vector<int> SETS = {0, 63, 25, 67, 17, 75, 13, 50};
    constexpr float BALLOON_INTERVAL_S = SPEED / 60.0F;                         // ~3.33s (cada 200 frames)
    constexpr float POWERBALL_INTERVAL_S = (SPEED * 4) / 60.0F;                 // ~13.33s (cada 800 frames)
    constexpr float MAX_BALLOON_TIME_S = ((NUM_SETS - 1) * SPEED * 3) / 60.0F;  // Tiempo máximo para lanzar globos

    // Acumular tiempo total de globos
    elapsed_time_balloons_ += delta_time;

    // Detener lanzamiento después del tiempo límite
    if (elapsed_time_balloons_ > MAX_BALLOON_TIME_S) {
        return;
    }

    credits_state_.balloon_accumulator += delta_time;
    credits_state_.powerball_accumulator += delta_time;

    if (credits_state_.balloon_accumulator >= BALLOON_INTERVAL_S) {
        credits_state_.balloon_accumulator -= BALLOON_INTERVAL_S;
        const int INDEX = (static_cast<int>(elapsed_time_balloons_ * 60.0F / SPEED)) % SETS.size();
        balloon_manager_->deployFormation(SETS.at(INDEX), -60);
    }

    if (credits_state_.powerball_accumulator >= POWERBALL_INTERVAL_S && elapsed_time_balloons_ > 0.0F) {
        credits_state_.powerball_accumulator -= POWERBALL_INTERVAL_S;
        balloon_manager_->createPowerBall();
    }
}

// Inicializa los jugadores
void Credits::initPlayers() {
    std::vector<std::vector<std::shared_ptr<Texture>>> player_textures;  // Vector con todas las texturas de los jugadores;
    std::vector<std::vector<std::string>> player1_animations;            // Vector con las animaciones del jugador 1
    std::vector<std::vector<std::string>> player2_animations;            // Vector con las animaciones del jugador 2

    // Texturas - Player1
    std::vector<std::shared_ptr<Texture>> player1_textures;
    player1_textures.emplace_back(Resource::get()->getTexture("player1_pal0"));
    player1_textures.emplace_back(Resource::get()->getTexture("player1_pal1"));
    player1_textures.emplace_back(Resource::get()->getTexture("player1_pal2"));
    player1_textures.emplace_back(Resource::get()->getTexture("player1_pal3"));
    player1_textures.emplace_back(Resource::get()->getTexture("player1_power.png"));
    player_textures.push_back(player1_textures);

    // Texturas - Player2
    std::vector<std::shared_ptr<Texture>> player2_textures;
    player2_textures.emplace_back(Resource::get()->getTexture("player2_pal0"));
    player2_textures.emplace_back(Resource::get()->getTexture("player2_pal1"));
    player2_textures.emplace_back(Resource::get()->getTexture("player2_pal2"));
    player2_textures.emplace_back(Resource::get()->getTexture("player2_pal3"));
    player2_textures.emplace_back(Resource::get()->getTexture("player2_power.png"));
    player_textures.push_back(player2_textures);

    // Animaciones -- Jugador
    player1_animations.emplace_back(Resource::get()->getAnimation("player1.ani"));
    player1_animations.emplace_back(Resource::get()->getAnimation("player_power.ani"));
    player2_animations.emplace_back(Resource::get()->getAnimation("player2.ani"));
    player2_animations.emplace_back(Resource::get()->getAnimation("player_power.ani"));

    // Crea los dos jugadores
    const int Y = play_area_.y + play_area_.h - Player::WIDTH;
    constexpr bool DEMO = false;
    constexpr int AWAY_DISTANCE = 700;

    Player::Config config_player1;
    config_player1.id = Player::Id::PLAYER1;
    config_player1.x = play_area_.x - AWAY_DISTANCE - Player::WIDTH;
    config_player1.y = Y;
    config_player1.demo = DEMO;
    config_player1.play_area = &play_area_;
    config_player1.texture = player_textures.at(0);
    config_player1.animations = player1_animations;
    config_player1.hi_score_table = &Options::settings.hi_score_table;
    config_player1.glowing_entry = &Options::settings.glowing_entries.at(static_cast<int>(Player::Id::PLAYER1) - 1);
    players_.emplace_back(std::make_unique<Player>(config_player1));
    players_.back()->setWalkingState(Player::State::WALKING_RIGHT);
    players_.back()->setPlayingState(Player::State::CREDITS);

    Player::Config config_player2;
    config_player2.id = Player::Id::PLAYER2;
    config_player2.x = play_area_.x + play_area_.w + AWAY_DISTANCE;
    config_player2.y = Y;
    config_player2.demo = DEMO;
    config_player2.play_area = &play_area_;
    config_player2.texture = player_textures.at(1);
    config_player2.animations = player2_animations;
    config_player2.hi_score_table = &Options::settings.hi_score_table;
    config_player2.glowing_entry = &Options::settings.glowing_entries.at(static_cast<int>(Player::Id::PLAYER2) - 1);
    players_.emplace_back(std::make_unique<Player>(config_player2));
    players_.back()->setWalkingState(Player::State::WALKING_LEFT);
    players_.back()->setPlayingState(Player::State::CREDITS);

    // Registra los jugadores en Options
    for (const auto& player : players_) {
        Options::keyboard.addPlayer(player);
        Options::gamepad_manager.addPlayer(player);
    }
}

// Actualiza los rectangulos negros (time-based)
void Credits::updateBlackRects(float delta_time) {
    if (!initialized_) { return; }
    delta_time = std::max(delta_time, 0.0F);

    // Fase vertical: hasta que ambos rects verticales estén exactos en su target
    if (!vertical_done_) {
        credits_state_.black_rect_accumulator += delta_time;
        if (credits_state_.black_rect_accumulator >= BLACK_RECT_INTERVAL_S) {
            credits_state_.black_rect_accumulator -= BLACK_RECT_INTERVAL_S;

            // top
            int prev_top_h = static_cast<int>(top_black_rect_.h);
            top_black_rect_.h = std::min(top_black_rect_.h + 1.0F,
                static_cast<float>(param.game.game_area.center_y - 1));
            int top_delta = static_cast<int>(top_black_rect_.h) - prev_top_h;

            // bottom
            int prev_bottom_h = static_cast<int>(bottom_black_rect_.h);
            int prev_bottom_y = static_cast<int>(bottom_black_rect_.y);
            bottom_black_rect_.h = bottom_black_rect_.h + 1.0F;
            bottom_black_rect_.y = std::max(bottom_black_rect_.y - 1.0F,
                static_cast<float>(param.game.game_area.center_y + 1));
            int bottom_steps_by_h = static_cast<int>(bottom_black_rect_.h) - prev_bottom_h;
            int bottom_steps_by_y = prev_bottom_y - static_cast<int>(bottom_black_rect_.y);
            int bottom_steps = std::max({0, bottom_steps_by_h, bottom_steps_by_y});

            int steps_done = top_delta + bottom_steps;
            if (steps_done > 0) {
                current_step_ = std::max(0.0F, current_step_ - static_cast<float>(steps_done));
                float vol_f = initial_volume_ * (current_step_ / static_cast<float>(total_steps_));
                int vol_i = static_cast<int>(std::clamp(vol_f, 0.0F, static_cast<float>(initial_volume_)));
                Audio::get()->setMusicVolume(vol_i);  // usa tu API de audio aquí
            }

            // Si han alcanzado los objetivos, fijarlos exactamente y marcar done
            bool top_at_target = static_cast<int>(top_black_rect_.h) == param.game.game_area.center_y - 1.0F;
            bool bottom_at_target = static_cast<int>(bottom_black_rect_.y) == param.game.game_area.center_y + 1.0F;
            if (top_at_target && bottom_at_target) {
                top_black_rect_.h = param.game.game_area.center_y - 1.0F;
                bottom_black_rect_.y = param.game.game_area.center_y + 1.0F;
                vertical_done_ = true;
            }
        }
        // actualizar border_rect cada frame aunque todavía en fase vertical
        updateBorderRect();
        return;
    }

    // Fase horizontal
    if (!horizontal_done_) {
        int prev_left_w = static_cast<int>(left_black_rect_.w);
        left_black_rect_.w = std::min(left_black_rect_.w + static_cast<float>(HORIZONTAL_SPEED),
            param.game.game_area.center_x);
        int left_gain = static_cast<int>(left_black_rect_.w) - prev_left_w;

        int prev_right_x = static_cast<int>(right_black_rect_.x);
        right_black_rect_.w = right_black_rect_.w + static_cast<float>(HORIZONTAL_SPEED);
        right_black_rect_.x = std::max(right_black_rect_.x - static_cast<float>(HORIZONTAL_SPEED),
            param.game.game_area.center_x);
        int right_move = prev_right_x - static_cast<int>(right_black_rect_.x);

        int steps_done = left_gain + right_move;
        if (steps_done > 0) {
            current_step_ = std::max(0.0F, current_step_ - static_cast<float>(steps_done));
            float vol_f = initial_volume_ * (current_step_ / static_cast<float>(total_steps_));
            int vol_i = static_cast<int>(std::clamp(vol_f, 0.0F, static_cast<float>(initial_volume_)));
            Audio::get()->setMusicVolume(vol_i);  // usa tu API de audio aquí
        }

        bool left_at_target = static_cast<int>(left_black_rect_.w) == param.game.game_area.center_x;
        bool right_at_target = static_cast<int>(right_black_rect_.x) == param.game.game_area.center_x;
        if (left_at_target && right_at_target) {
            left_black_rect_.w = param.game.game_area.center_x;
            right_black_rect_.x = param.game.game_area.center_x;
            horizontal_done_ = true;
        }

        updateBorderRect();
        return;
    }

    // Fase final: ya completado el movimiento de rects
    Audio::get()->setMusicVolume(0);
    // Audio::get()->stopMusic(); // opcional, si quieres parar la reproducción

    // Usar segundos puros en lugar de frames equivalentes
    if (counter_pre_fade_ >= PRE_FADE_DELAY_S) {
        if (fade_out_) {
            fade_out_->activate();
        }
    } else {
        counter_pre_fade_ += delta_time;
    }
}

// Actualiza el rectangulo del borde
void Credits::updateBorderRect() {
    border_rect_.x = left_black_rect_.x + left_black_rect_.w;
    border_rect_.y = top_black_rect_.y + top_black_rect_.h - 1.0F;

    float raw_w = right_black_rect_.x - border_rect_.x;
    float raw_h = bottom_black_rect_.y - border_rect_.y + 1.0F;

    border_rect_.w = std::max(0.0F, raw_w);
    border_rect_.h = std::max(0.0F, raw_h);
}

// Actualiza el estado de fade (time-based)
void Credits::updateAllFades(float delta_time) {
    if (fading_) {
        updateBlackRects(delta_time);
        updateBorderRect();
    }

    fade_in_->update();
    if (fade_in_->hasEnded() && Audio::get()->getMusicState() != Audio::MusicState::PLAYING) {
        Audio::get()->playMusic("credits.ogg");
    }

    fade_out_->update();
    if (fade_out_->hasEnded()) {
        Section::name = Section::Name::HI_SCORE_TABLE;
    }
}

// Establece el nivel de volumen
void Credits::setVolume(int amount) {
    Options::audio.music.volume = std::clamp(amount, 0, 100);
    Audio::get()->setMusicVolume(Options::audio.music.volume);
}

// Reestablece el nivel de volumen
void Credits::resetVolume() const {
    Options::audio.music.volume = initial_volume_;
    Audio::get()->setMusicVolume(Options::audio.music.volume);
}

// Cambia el color del fondo (time-based)
void Credits::cycleColors(float delta_time) {
    constexpr int UPPER_LIMIT = 140;  // Límite superior
    constexpr int LOWER_LIMIT = 30;   // Límite inferior

    // Factor para escalar los valores de incremento.
    // Asumimos que los valores originales estaban balanceados para 60 FPS.
    const float FRAME_ADJUSTMENT = delta_time * 60.0F;

    // Inicializar valores RGB si es la primera vez
    if (credits_state_.r == 255.0F && credits_state_.g == 0.0F && credits_state_.b == 0.0F && credits_state_.step_r == -0.5F) {
        credits_state_.r = static_cast<float>(UPPER_LIMIT);
        credits_state_.g = static_cast<float>(LOWER_LIMIT);
        credits_state_.b = static_cast<float>(LOWER_LIMIT);
    }

    // Ajustar valores de R
    credits_state_.r += credits_state_.step_r * FRAME_ADJUSTMENT;
    if (credits_state_.r >= UPPER_LIMIT) {
        credits_state_.r = UPPER_LIMIT;                  // Clamp para evitar que se pase
        credits_state_.step_r = -credits_state_.step_r;  // Cambia de dirección al alcanzar los límites
    } else if (credits_state_.r <= LOWER_LIMIT) {
        credits_state_.r = LOWER_LIMIT;  // Clamp para evitar que se pase
        credits_state_.step_r = -credits_state_.step_r;
    }

    // Ajustar valores de G
    credits_state_.g += credits_state_.step_g * FRAME_ADJUSTMENT;
    if (credits_state_.g >= UPPER_LIMIT) {
        credits_state_.g = UPPER_LIMIT;
        credits_state_.step_g = -credits_state_.step_g;  // Cambia de dirección al alcanzar los límites
    } else if (credits_state_.g <= LOWER_LIMIT) {
        credits_state_.g = LOWER_LIMIT;
        credits_state_.step_g = -credits_state_.step_g;
    }

    // Ajustar valores de B
    credits_state_.b += credits_state_.step_b * FRAME_ADJUSTMENT;
    if (credits_state_.b >= UPPER_LIMIT) {
        credits_state_.b = UPPER_LIMIT;
        credits_state_.step_b = -credits_state_.step_b;  // Cambia de dirección al alcanzar los límites
    } else if (credits_state_.b <= LOWER_LIMIT) {
        credits_state_.b = LOWER_LIMIT;
        credits_state_.step_b = -credits_state_.step_b;
    }

    // Aplicar el color, redondeando a enteros antes de usar
    color_ = Color(static_cast<int>(credits_state_.r), static_cast<int>(credits_state_.g), static_cast<int>(credits_state_.b));
    tiled_bg_->setColor(color_);
}

// Actualza los jugadores (time-based)
void Credits::updatePlayers(float delta_time) {
    for (auto& player : players_) {
        player->update(delta_time);
    }
}

// Renderiza los jugadores
void Credits::renderPlayers() {
    for (auto const& player : players_) {
        player->render();
    }
}

// Inicializa variables
void Credits::initVars() {
    // Inicialización segura de rects tal y como los mostraste
    top_black_rect_ = {
        .x = play_area_.x,
        .y = param.game.game_area.rect.y,
        .w = play_area_.w,
        .h = black_bars_size_};
    bottom_black_rect_ = {
        .x = play_area_.x,
        .y = param.game.game_area.rect.h - black_bars_size_,
        .w = play_area_.w,
        .h = black_bars_size_};
    left_black_rect_ = {
        .x = play_area_.x,
        .y = param.game.game_area.center_y - 1.0F,
        .w = 0.0F,
        .h = 2.0F};
    right_black_rect_ = {
        .x = play_area_.x + play_area_.w,
        .y = param.game.game_area.center_y - 1.0F,
        .w = 0.0F,
        .h = 2.0F};

    initialized_ = false;

    Section::name = Section::Name::CREDITS;
    balloon_manager_->setPlayArea(play_area_);

    fade_in_->setColor(param.fade.color);
    fade_in_->setType(Fade::Type::FULLSCREEN);
    fade_in_->setPostDuration(800);
    fade_in_->setMode(Fade::Mode::IN);
    fade_in_->activate();

    fade_out_->setColor(0, 0, 0);
    fade_out_->setType(Fade::Type::FULLSCREEN);
    fade_out_->setPostDuration(7000);

    updateBorderRect();
    tiled_bg_->setColor(Color(255, 96, 96));
    tiled_bg_->setSpeed(60.0F);

    initPlayers();
    SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND);
    fillTextTexture();
    steps_ = static_cast<int>(std::abs((top_black_rect_.h - param.game.game_area.center_y - 1) + ((left_black_rect_.w - param.game.game_area.center_x) / 4)));
}

void Credits::startCredits() {
    // Guardar iniciales (enteros para contar "pasos" por píxel)
    init_top_h_ = static_cast<int>(top_black_rect_.h);
    init_bottom_y_ = static_cast<int>(bottom_black_rect_.y);
    init_left_w_ = static_cast<int>(left_black_rect_.w);
    init_right_x_ = static_cast<int>(right_black_rect_.x);

    // Objetivos
    int top_target_h = param.game.game_area.center_y - 1;
    int bottom_target_y = param.game.game_area.center_y + 1;
    int left_target_w = param.game.game_area.center_x;
    int right_target_x = param.game.game_area.center_x;

    // Pasos verticales
    int pasos_top = std::max(0, top_target_h - init_top_h_);
    int pasos_bottom = std::max(0, init_bottom_y_ - bottom_target_y);

    // Pasos horizontales. right se mueve a velocidad HORIZONTAL_SPEED, contamos pasos como unidades de movimiento equivalentes
    int pasos_left = std::max(0, left_target_w - init_left_w_);
    int dx_right = std::max(0, init_right_x_ - right_target_x);
    int pasos_right = (dx_right + (HORIZONTAL_SPEED - 1)) / HORIZONTAL_SPEED;  // ceil

    total_steps_ = pasos_top + pasos_bottom + pasos_left + pasos_right;
    if (total_steps_ <= 0) {
        total_steps_ = 1;
    }

    current_step_ = static_cast<float>(total_steps_);

    // Reiniciar contadores y estado
    credits_state_.black_rect_accumulator = 0.0F;
    counter_pre_fade_ = 0.0F;
    initialized_ = true;

    // Asegurar volumen inicial consistente
    if (steps_ <= 0) {
        steps_ = 1;
    }
    float vol_f = initial_volume_ * (current_step_ / static_cast<float>(total_steps_));
    setVolume(static_cast<int>(std::clamp(vol_f, 0.0F, static_cast<float>(initial_volume_))));
}

// Dibuja el rectángulo del borde si es visible
void Credits::drawBorderRect() {
    // Umbral: cualquier valor menor que 1 píxel no se considera visible
    constexpr float VISIBLE_THRESHOLD = 1.0F;
    if (border_rect_.w < VISIBLE_THRESHOLD || border_rect_.h < VISIBLE_THRESHOLD) {
        return;  // no dibujar
    }

    const Color COLOR = color_.LIGHTEN();
    SDL_Renderer* rdr = Screen::get()->getRenderer();
    SDL_SetRenderDrawColor(rdr, COLOR.r, COLOR.g, COLOR.b, 0xFF);

    // Convertir a enteros de forma conservadora para evitar líneas de 1px por redondeo extraño
    SDL_Rect r;
    r.x = static_cast<int>(std::floor(border_rect_.x + 0.5F));
    r.y = static_cast<int>(std::floor(border_rect_.y + 0.5F));
    r.w = static_cast<int>(std::max(0.0F, std::floor(border_rect_.w + 0.5F)));
    r.h = static_cast<int>(std::max(0.0F, std::floor(border_rect_.h + 0.5F)));

    if (r.w > 0 && r.h > 0) {
        SDL_RenderRect(Screen::get()->getRenderer(), &border_rect_);
    }
}
