// IWYU pragma: no_include <bits/std_abs.h>
#include "credits.h"
#include <SDL3/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <SDL3/SDL_events.h>    // Para SDL_PollEvent, SDL_Event
#include <SDL3/SDL_pixels.h>    // Para SDL_PixelFormat
#include <SDL3/SDL_timer.h>     // Para SDL_GetTicks
#include <algorithm>            // Para max, min, clamp
#include <array>                // Para array
#include <stdexcept>            // Para runtime_error
#include <string>               // Para basic_string, string
#include <vector>               // Para vector
#include "balloon_manager.h"    // Para BalloonManager
#include "fade.h"               // Para Fade, FadeType, FadeMode
#include "global_events.h"      // Para check
#include "global_inputs.h"      // Para check, update
#include "input.h"              // Para Input, INPUT_ALLOW_REPEAT
#include "audio.h"              // Para JA_GetMusicState, JA_SetMusicVolume
#include "lang.h"               // Para getText
#include "param.h"              // Para Param, ParamGame, param
#include "player.h"             // Para Player, PlayerState
#include "resource.h"           // Para Resource
#include "screen.h"             // Para Screen
#include "section.h"            // Para Name, name
#include "sprite.h"             // Para Sprite
#include "text.h"               // Para Text, TEXT_CENTER, TEXT_SHADOW
#include "texture.h"            // Para Texture
#include "tiled_bg.h"           // Para TiledBG, TiledBGMode
#include "utils.h"              // Para Color, Zone, shdw_txt_color, no_color

// Textos
constexpr const char TEXT_COPYRIGHT[] = "@2020,2025 JailDesigner";

// Constructor
Credits::Credits()
    : balloon_manager_(std::make_unique<BalloonManager>()),
      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, param.game.width, param.game.height)),
      canvas_(SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height))
{
    if (!text_texture_)
    {
        throw std::runtime_error("Failed to create SDL texture for text.");
    }
    section::name = section::Name::CREDITS;
    balloon_manager_->setPlayArea(play_area_);

    fade_in_->setColor(FADE_COLOR.r, FADE_COLOR.g, FADE_COLOR.b);
    fade_in_->setType(FadeType::FULLSCREEN);
    fade_in_->setPostDuration(50);
    fade_in_->setMode(FadeMode::IN);
    fade_in_->activate();

    fade_out_->setColor(0, 0, 0);
    fade_out_->setType(FadeType::FULLSCREEN);
    fade_out_->setPostDuration(400);

    updateRedRect();
    tiled_bg_->setColor(Color(255, 96, 96));

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

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

// Bucle principal
void Credits::run()
{
    while (section::name == section::Name::CREDITS)
    {
        checkInput();
        update();
        checkEvents(); // Tiene que ir antes del render
        render();
    }
}

// Actualiza las variables
void Credits::update()
{
    if (SDL_GetTicks() - ticks_ > param.game.speed)
    {
        ticks_ = SDL_GetTicks();
        const int REPEAT = want_to_pass_ ? 4 : 1;
        for (int i = 0; i < REPEAT; ++i)
        {
            tiled_bg_->update();
            cycleColors();
            balloon_manager_->update();
            updateTextureDstRects();
            throwBalloons();
            for (auto &player : players_)
            {
                player->update();
            }
            updateAllFades();
            ++counter_;
        }

        Screen::get()->update();

        fillCanvas();
    }
}

// Dibuja Credits::en patalla
void Credits::render()
{
    // Prepara para empezar a dibujar en la textura de juego
    Screen::get()->start();

    // Copia la textura con la zona de juego a la pantalla
    SDL_RenderTexture(Screen::get()->getRenderer(), canvas_, nullptr, nullptr);

    // Vuelca el contenido del renderizador en pantalla
    Screen::get()->render();
}

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

// Comprueba las entradas
void Credits::checkInput()
{
    // Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
    if (Input::get()->checkAnyButtonPressed(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");
    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, 10> TEXTS = {
        lang::getText(121),
        lang::getText(122),
        lang::getText(123),
        lang::getText(124),
        "JAILDESIGNER",
        "JAILDOCTOR (INTRO)",
        "ERIC MATYAS (SOUNDIMAGE.ORG)",
        "WWW.THEMOTIONMONKEY.CO.UK",
        "WWW.KENNEY.NL",
        "JAILDOCTOR"};

    const int SPACE_POST_TITLE = 3 + text->getCharacterSize();
    const int SPACE_PRE_TITLE = text->getCharacterSize() * 4;
    const int TEXTS_HEIGHT = 1 * text->getCharacterSize() + 7 * SPACE_POST_TITLE + 3 * SPACE_PRE_TITLE;
    credits_rect_dst_.h = credits_rect_src_.h = TEXTS_HEIGHT;

    int y = (param.game.height - TEXTS_HEIGHT) / 2;
    y = 0;
    text->setPalette(1);
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(0), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);

    text->setPalette(0);
    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(4), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);

    y += SPACE_PRE_TITLE;
    text->setPalette(1);
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(1), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);
    text->setPalette(0);
    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(4), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);

    y += SPACE_PRE_TITLE;
    text->setPalette(1);
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(2), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);
    text->setPalette(0);

    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(5), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);
    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(6), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);

    y += SPACE_PRE_TITLE;
    text->setPalette(1);
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(3), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);
    text->setPalette(0);
    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(7), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);
    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(8), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);
    y += SPACE_POST_TITLE;
    text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXTS.at(9), 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);

    // Mini logo
    y += SPACE_PRE_TITLE;
    mini_logo_rect_src_.y = y;
    auto mini_logo_sprite = std::make_unique<Sprite>(Resource::get()->getTexture("logo_jailgames_mini.png"));
    mini_logo_sprite->setPosition(1 + param.game.game_area.center_x - mini_logo_sprite->getWidth() / 2, 1 + y);
    Resource::get()->getTexture("logo_jailgames_mini.png")->setColor(SHADOW_TEXT_COLOR.r, SHADOW_TEXT_COLOR.g, SHADOW_TEXT_COLOR.b);
    mini_logo_sprite->render();

    mini_logo_sprite->setPosition(param.game.game_area.center_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, param.game.game_area.center_x, y, TEXT_COPYRIGHT, 1, NO_COLOR, 1, SHADOW_TEXT_COLOR);

    // 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();
    for (auto const &player : players_)
    {
        player->render();
    }

    // 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
    SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0xFF, 0, 0, 0xFF);
    SDL_RenderRect(Screen::get()->getRenderer(), &red_rect);

    // 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
void Credits::updateTextureDstRects()
{
    if (counter_ % 10 == 0)
    {
        // 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 == mini_logo_final_pos_)
        {
            mini_logo_on_position_ = true;

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

            // Se activa el contador para evitar que la sección sea infinita
            if (counter_prevent_endless_ == 1000)
            {
                fading_ = true;
            }
            else
            {
                ++counter_prevent_endless_;
            }
        }
        else
        {
            --mini_logo_rect_dst_.y;
        }
    }
}

// Tira globos al escenario
void Credits::throwBalloons()
{
    constexpr int speed = 200;
    const std::vector<int> sets = {0, 63, 25, 67, 17, 75, 13, 50};

    if (counter_ > ((sets.size() - 1) * speed) * 3)
    {
        return;
    }

    if (counter_ % speed == 0)
    {
        const int index = (counter_ / speed) % sets.size();
        balloon_manager_->deploySet(sets.at(index), -60);
    }

    if (counter_ % (speed * 4) == 0 && counter_ > 0)
    {
        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>> player_animations;            // Vector con las animaciones del jugador

    // Texturas - Player1
    {
        std::vector<std::shared_ptr<Texture>> player_texture;
        player_texture.emplace_back(Resource::get()->getTexture("player1.gif"));
        player_texture.emplace_back(Resource::get()->getTexture("player1_power.png"));
        player_textures.push_back(player_texture);
    }

    // Texturas - Player2
    {
        std::vector<std::shared_ptr<Texture>> player_texture;
        player_texture.emplace_back(Resource::get()->getTexture("player2.gif"));
        player_texture.emplace_back(Resource::get()->getTexture("player2_power.png"));
        player_textures.push_back(player_texture);
    }

    // Animaciones -- Jugador
    {
        player_animations.emplace_back(Resource::get()->getAnimation("player.ani"));
        player_animations.emplace_back(Resource::get()->getAnimation("player_power.ani"));
    }

    // Crea los dos jugadores
    constexpr int player_width = 30;
    const int y = play_area_.y + play_area_.h - player_width;
    constexpr bool demo = false;
    constexpr int away_distance = 700;
    players_.emplace_back(std::make_unique<Player>(1, play_area_.x - away_distance - player_width, y, demo, play_area_, player_textures.at(0), player_animations));
    players_.back()->setWalkingState(PlayerState::WALKING_RIGHT);
    players_.back()->setPlayingState(PlayerState::CREDITS);
    players_.back()->setInvulnerable(false);

    players_.emplace_back(std::make_unique<Player>(2, play_area_.x + play_area_.w + away_distance, y, demo, play_area_, player_textures.at(1), player_animations));
    players_.back()->setWalkingState(PlayerState::WALKING_LEFT);
    players_.back()->setPlayingState(PlayerState::CREDITS);
    players_.back()->setInvulnerable(false);
}

// Actualiza los rectangulos negros
void Credits::updateBlackRects()
{
    static int current_step = steps_;
    if (top_black_rect_.h != param.game.game_area.center_y - 1 && bottom_black_rect_.y != param.game.game_area.center_y + 1)
    {
        // Si los rectangulos superior e inferior no han llegado al centro
        if (counter_ % 4 == 0)
        {
            // Incrementa la altura del rectangulo superior
            top_black_rect_.h = std::min(top_black_rect_.h + 1, param.game.game_area.center_y - 1);

            // Incrementa la altura y modifica la posición del rectangulo inferior
            ++bottom_black_rect_.h;
            bottom_black_rect_.y = std::max(bottom_black_rect_.y - 1, param.game.game_area.center_y + 1);

            --current_step;
            setVolume(static_cast<int>(initial_volume_ * current_step / steps_));
        }
    }
    else
    {
        // Si los rectangulos superior e inferior han llegado al centro
        if (left_black_rect_.w != param.game.game_area.center_x && right_black_rect_.x != param.game.game_area.center_x)
        {
            constexpr int SPEED = 2;
            // Si los rectangulos izquierdo y derecho no han llegado al centro
            // Incrementa la anchura del rectangulo situado a la izquierda
            left_black_rect_.w = std::min(left_black_rect_.w + SPEED, param.game.game_area.center_x);

            // Incrementa la anchura y modifica la posición del rectangulo situado a la derecha
            right_black_rect_.w += SPEED;
            right_black_rect_.x = std::max(right_black_rect_.x - SPEED, param.game.game_area.center_x);

            --current_step;
            setVolume(static_cast<int>(initial_volume_ * current_step / steps_));
        }
        else
        {
            // Si los rectangulos izquierdo y derecho han llegado al centro
            setVolume(0);
            Audio::get().stopMusic();
            if (counter_pre_fade_ == 400)
            {
                fade_out_->activate();
            }
            else
            {
                ++counter_pre_fade_;
            }
        }
    }
}

// Actualiza el rectangulo rojo
void Credits::updateRedRect()
{
    red_rect.x = left_black_rect_.x + left_black_rect_.w;
    red_rect.y = top_black_rect_.y + top_black_rect_.h - 1;
    red_rect.w = right_black_rect_.x - red_rect.x;
    red_rect.h = bottom_black_rect_.y - red_rect.y + 1;
}

// Actualiza el estado de fade
void Credits::updateAllFades()
{
    if (fading_)
    {
        updateBlackRects();
        updateRedRect();
    }

    fade_in_->update();
    if (fade_in_->hasEnded())
    {
        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()
{
    options.audio.music.volume = initial_volume_;
    Audio::get().setMusicVolume(options.audio.music.volume);
}

// Cambia el color del fondo
void Credits::cycleColors()
{
    constexpr int UPPER_LIMIT = 255; // Límite superior
    constexpr int LOWER_LIMIT = 80;  // Límite inferior

    static float r = static_cast<float>(UPPER_LIMIT);
    static float g = static_cast<float>(LOWER_LIMIT);
    static float b = static_cast<float>(LOWER_LIMIT);
    static float stepR = -0.5f; // Paso flotante para transiciones suaves
    static float stepG = 0.3f;
    static float stepB = 0.7f;

    // Ajustar valores de R
    r += stepR;
    if (r >= UPPER_LIMIT || r <= LOWER_LIMIT)
    {
        stepR = -stepR; // Cambia de dirección al alcanzar los límites
    }

    // Ajustar valores de G
    g += stepG;
    if (g >= UPPER_LIMIT || g <= LOWER_LIMIT)
    {
        stepG = -stepG; // Cambia de dirección al alcanzar los límites
    }

    // Ajustar valores de B
    b += stepB;
    if (b >= UPPER_LIMIT || b <= LOWER_LIMIT)
    {
        stepB = -stepB; // Cambia de dirección al alcanzar los límites
    }

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