#include "ending.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_error.h>     // for SDL_GetError
#include <SDL2/SDL_events.h>    // for SDL_PollEvent, SDL_Event
#include <SDL2/SDL_pixels.h>    // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_rect.h>      // for SDL_Rect
#include <SDL2/SDL_timer.h>     // for SDL_GetTicks
#include <algorithm>            // for min
#include <iostream>             // for basic_ostream, operator<<, cout, endl
#include "asset.h"              // for Asset
#include "defines.h"            // for GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH
#include "global_events.h"      // for check
#include "global_inputs.h"      // for check
#include "input.h"              // for Input
#include "jail_audio.h"         // for JA_SetVolume, JA_PlayMusic, JA_StopM...
#include "options.h"            // for Options, options, OptionsVideo, Sect...
#include "resource.h"           // for Resource
#include "screen.h"             // for Screen
#include "sprite.h"             // for Sprite
#include "text.h"               // for Text, TEXT_STROKE
#include "texture.h"            // for Texture
#include "utils.h"              // for Color, stringToColor, Palette

// Constructor
Ending::Ending()
    : counter_(-1),
      pre_counter_(0),
      cover_counter_(0),
      ticks_(0),
      current_scene_(0)
{
    options.section.section = Section::ENDING;
    options.section.subsection = Subsection::NONE;

    // Inicializa los textos
    iniTexts();

    // Inicializa las imagenes
    iniPics();

    // Inicializa las escenas
    iniScenes();

    // Cambia el color del borde
    Screen::get()->setBorderColor(stringToColor(options.video.palette, "black"));

    // Crea la textura para cubrir el rexto
    cover_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT + 8);
    if (cover_texture_ == nullptr)
    {
        if (options.console)
        {
            std::cout << "Error: canvasTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
        }
    }
    SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND);

    // Rellena la textura para la cortinilla
    fillCoverTexture();
}

// Destructor
Ending::~Ending()
{
    // Libera la memoria de los objetos
    SDL_DestroyTexture(cover_texture_);
}

// Actualiza el objeto
void Ending::update()
{
    // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
    if (SDL_GetTicks() - ticks_ > GAME_SPEED)
    {
        // Actualiza el contador de ticks
        ticks_ = SDL_GetTicks();

        // Comprueba las entradas
        checkInput();

        // Actualiza el contador
        updateCounters();

        // Actualiza las cortinillas de los elementos
        updateSpriteCovers();

        // Comprueba si se ha de cambiar de escena
        checkChangeScene();

        // Actualiza el volumen de la musica
        updateMusicVolume();

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

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

    // Limpia la pantalla
    Screen::get()->clean(stringToColor(options.video.palette, "black"));

    // Dibuja las imagenes de la escena
    sprite_pics_[current_scene_].sprite->render();
    sprite_pics_[current_scene_].cover_sprite->render();

    // Dibuja los textos de la escena
    for (auto ti : scenes_[current_scene_].text_index)
    {
        if (counter_ > ti.trigger)
        {
            sprite_texts_[ti.index].sprite->render();
            sprite_texts_[ti.index].cover_sprite->render();
        }
    }

    // Dibuja la cortinilla de cambio de escena
    renderCoverTexture();

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

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

// Comprueba las entradas
void Ending::checkInput()
{
    globalInputs::check();
}

// Inicializa los textos
void Ending::iniTexts()
{
    // Vector con los textos
    std::vector<TextAndPosition> texts;

    // Escena #0
    texts.push_back({"HE FINALLY MANAGED", 32});
    texts.push_back({"TO GET TO THE JAIL", 42});
    texts.push_back({"WITH ALL HIS PROJECTS", 142});
    texts.push_back({"READY TO BE FREED", 152});

    // Escena #1
    texts.push_back({"ALL THE JAILERS WERE THERE", 1});
    texts.push_back({"WAITING FOR THE JAILGAMES", 11});
    texts.push_back({"TO BE RELEASED", 21});

    texts.push_back({"THERE WERE EVEN BARRULLS AND", 161});
    texts.push_back({"BEGINNERS AMONG THE CROWD", 171});

    texts.push_back({"BRY WAS CRYING...", 181});

    // Escena #2
    texts.push_back({"BUT SUDDENLY SOMETHING", 19});
    texts.push_back({"CAUGHT HIS ATTENTION", 29});

    // Escena #3
    texts.push_back({"A PILE OF JUNK!", 36});
    texts.push_back({"FULL OF NON WORKING TRASH!!", 46});

    // Escena #4
    texts.push_back({"AND THEN,", 36});
    texts.push_back({"FOURTY NEW PROJECTS", 46});
    texts.push_back({"WERE BORN...", 158});

    // Crea los sprites
    sprite_texts_.clear();

    for (const auto &t : texts)
    {
        auto text = Resource::get()->getText("smb2");

        const int WIDTH = text->lenght(t.caption, 1) + 2 + 2;
        const int HEIGHT = text->getCharacterSize() + 2 + 2;
        Color color = stringToColor(options.video.palette, "black");
        
        EndingTexture st;
        
        // Crea la texture
        st.texture = std::make_shared<Texture>(Screen::get()->getRenderer());
        st.texture->createBlank(WIDTH, HEIGHT);
        st.texture->setAsRenderTarget(Screen::get()->getRenderer());
        st.texture->setBlendMode(SDL_BLENDMODE_BLEND);
        text->writeDX(TEXT_STROKE, 2, 2, t.caption, 1, color, 2, color);

        // Crea el sprite
        st.sprite = std::make_shared<Sprite>(st.texture, 0, 0, st.texture->getWidth(), st.texture->getHeight());
        st.sprite->setPosition((GAMECANVAS_WIDTH - st.texture->getWidth()) / 2, t.pos);

        // Crea la coverTexture
        st.cover_texture = std::make_shared<Texture>(Screen::get()->getRenderer());
        st.cover_texture->createBlank(WIDTH, HEIGHT + 8);
        st.cover_texture->setAsRenderTarget(Screen::get()->getRenderer());
        st.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND);

        // Rellena la coverTexture con color transparente
        SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0);
        SDL_RenderClear(Screen::get()->getRenderer());

        // Los primeros 8 pixels crea una malla
        color = stringToColor(options.video.palette, "black");
        SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
        for (int i = 0; i < WIDTH; i += 2)
        {
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 4);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 6);

            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 5);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 7);
        }

        // El resto se rellena de color sólido
        SDL_Rect rect = {0, 8, WIDTH, HEIGHT};
        color = stringToColor(options.video.palette, "black");
        SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
        SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);

        // Crea el sprite
        st.cover_sprite = std::make_shared<Sprite>(st.cover_texture, 0, 0, st.cover_texture->getWidth(), st.cover_texture->getHeight() - 8);
        st.cover_sprite->setPosition((GAMECANVAS_WIDTH - st.cover_texture->getWidth()) / 2, t.pos);
        st.cover_sprite->setClip(0, 8, -1, -1);

        // Inicializa variables
        st.clip_desp = 8;
        st.clip_height = HEIGHT;

        sprite_texts_.push_back(st);
    }
}

// Inicializa las imagenes
void Ending::iniPics()
{
    // Vector con las rutas y la posición
    std::vector<TextAndPosition> pics;

    if (options.video.palette == Palette::ZXSPECTRUM)
    {
        pics.push_back({"ending1.png", 48});
        pics.push_back({"ending2.png", 26});
        pics.push_back({"ending3.png", 29});
        pics.push_back({"ending4.png", 63});
        pics.push_back({"ending5.png", 53});
    }
    else
    {
        pics.push_back({"ending1_zxarne.png", 48});
        pics.push_back({"ending2_zxarne.png", 26});
        pics.push_back({"ending3_zxarne.png", 29});
        pics.push_back({"ending4_zxarne.png", 63});
        pics.push_back({"ending5_zxarne.png", 53});
    }

    // Crea los sprites
    sprite_pics_.clear();

    for (const auto &p : pics)
    {
        EndingTexture sp;

        // Crea la texture
        sp.texture = Resource::get()->getTexture(p.caption);
        const int width = sp.texture->getWidth();
        const int height = sp.texture->getHeight();

        // Crea el sprite
        sp.sprite = std::make_shared<Sprite>(sp.texture, 0, 0, width, height);
        sp.sprite->setPosition((GAMECANVAS_WIDTH - width) / 2, p.pos);

        // Crea la coverTexture
        sp.cover_texture = std::make_shared<Texture>(Screen::get()->getRenderer());
        sp.cover_texture->createBlank(width, height + 8);
        sp.cover_texture->setAsRenderTarget(Screen::get()->getRenderer());
        sp.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND);

        // Rellena la coverTexture con color transparente
        SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0);
        SDL_RenderClear(Screen::get()->getRenderer());

        // Los primeros 8 pixels crea una malla
        Color c = stringToColor(options.video.palette, "black");
        SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF);
        for (int i = 0; i < width; i += 2)
        {
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 4);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 6);

            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 5);
            SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 7);
        }

        // El resto se rellena de color sólido
        SDL_Rect rect = {0, 8, width, height};
        c = stringToColor(options.video.palette, "black");
        SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF);
        SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);

        // Crea el sprite
        sp.cover_sprite = std::make_shared<Sprite>(sp.cover_texture, 0, 0, sp.cover_texture->getWidth(), sp.cover_texture->getHeight() - 8);
        sp.cover_sprite->setPosition((GAMECANVAS_WIDTH - sp.cover_texture->getWidth()) / 2, p.pos);
        sp.cover_sprite->setClip(0, 8, -1, -1);

        // Inicializa variables
        sp.clip_desp = 8;
        sp.clip_height = height;

        sprite_pics_.push_back(sp);
    }
}

// Inicializa las escenas
void Ending::iniScenes()
{
    // Variable para los tiempos
    int trigger;
    const int lapse = 80;

    // Crea el contenedor
    SceneData sc;

    // Inicializa el vector
    scenes_.clear();

    // Crea la escena #0
    sc.counter_end = 1000;
    sc.picture_index = 0;
    sc.text_index.clear();
    trigger = 85 * 2;
    trigger += lapse;
    sc.text_index.push_back({0, trigger});
    trigger += lapse;
    sc.text_index.push_back({1, trigger});
    trigger += lapse * 3;
    sc.text_index.push_back({2, trigger});
    trigger += lapse;
    sc.text_index.push_back({3, trigger});
    scenes_.push_back(sc);

    // Crea la escena #1
    sc.counter_end = 1400;
    sc.picture_index = 1;
    sc.text_index.clear();
    trigger = 140 * 2;
    trigger += lapse;
    sc.text_index.push_back({4, trigger});
    trigger += lapse;
    sc.text_index.push_back({5, trigger});
    trigger += lapse;
    sc.text_index.push_back({6, trigger});
    trigger += lapse * 3;
    sc.text_index.push_back({7, trigger});
    trigger += lapse;
    sc.text_index.push_back({8, trigger});
    trigger += lapse * 3;
    sc.text_index.push_back({9, trigger});
    scenes_.push_back(sc);

    // Crea la escena #2
    sc.counter_end = 1000;
    sc.picture_index = 2;
    sc.text_index.clear();
    trigger = 148 / 2;
    trigger += lapse;
    sc.text_index.push_back({10, trigger});
    trigger += lapse;
    sc.text_index.push_back({11, trigger});
    scenes_.push_back(sc);

    // Crea la escena #3
    sc.counter_end = 800;
    sc.picture_index = 3;
    sc.text_index.clear();
    trigger = 87 / 2;
    trigger += lapse;
    sc.text_index.push_back({12, trigger});
    trigger += lapse / 2;
    sc.text_index.push_back({13, trigger});
    scenes_.push_back(sc);

    // Crea la escena #4
    sc.counter_end = 1000;
    sc.picture_index = 4;
    sc.text_index.clear();
    trigger = 91 * 2;
    trigger += lapse;
    sc.text_index.push_back({14, trigger});
    trigger += lapse * 2;
    sc.text_index.push_back({15, trigger});
    trigger += lapse * 3;
    sc.text_index.push_back({16, trigger});
    scenes_.push_back(sc);
}

// Bucle principal
void Ending::run()
{
    JA_PlayMusic(Resource::get()->getMusic("ending1.ogg"));

    while (options.section.section == Section::ENDING)
    {
        update();
        checkEvents();
        render();
    }

    JA_StopMusic();
    JA_SetVolume(128);
}

// Actualiza los contadores
void Ending::updateCounters()
{
    // Incrementa el contador
    if (pre_counter_ < 200)
    {
        pre_counter_++;
    }
    else
    {
        counter_++;
    }

    if (counter_ > scenes_[current_scene_].counter_end - 100)
    {
        cover_counter_++;
    }
}

// Actualiza las cortinillas de los elementos
void Ending::updateSpriteCovers()
{ // Actualiza la cortinilla de los textos
    if (counter_ % 4 == 0)
    {
        for (auto ti : scenes_[current_scene_].text_index)
        {
            if (counter_ > ti.trigger)
            {
                if (sprite_texts_[ti.index].clip_desp > 0)
                {
                    sprite_texts_[ti.index].clip_desp -= 2;
                }
                else if (sprite_texts_[ti.index].clip_height > 0)
                {
                    sprite_texts_[ti.index].clip_height -= 2;
                    sprite_texts_[ti.index].cover_sprite->setY(sprite_texts_[ti.index].cover_sprite->getY() + 2);
                }
                sprite_texts_[ti.index].cover_sprite->setClip(0, sprite_texts_[ti.index].clip_desp, sprite_texts_[ti.index].cover_sprite->getWidth(), sprite_texts_[ti.index].clip_height);
            }
        }
    }

    // Actualiza la cortinilla de las imagenes
    if (counter_ % 2 == 0)
    {
        if (sprite_pics_[current_scene_].clip_desp > 0)
        {
            sprite_pics_[current_scene_].clip_desp -= 2;
        }
        else if (sprite_pics_[current_scene_].clip_height > 0)
        {
            sprite_pics_[current_scene_].clip_height -= 2;
            if (sprite_pics_[current_scene_].clip_height < 0)
            {
                sprite_pics_[current_scene_].clip_height = 0;
            }
            sprite_pics_[current_scene_].cover_sprite->setY(sprite_pics_[current_scene_].cover_sprite->getY() + 2);
        }
        sprite_pics_[current_scene_].cover_sprite->setClip(0, sprite_pics_[current_scene_].clip_desp, sprite_pics_[current_scene_].cover_sprite->getWidth(), sprite_pics_[current_scene_].clip_height);
    }
}

// Comprueba si se ha de cambiar de escena
void Ending::checkChangeScene()
{
    if (counter_ > scenes_[current_scene_].counter_end)
    {
        current_scene_++;
        counter_ = 0;
        cover_counter_ = 0;
        if (current_scene_ == 5)
        {
            // Termina el bucle
            options.section.section = Section::ENDING2;

            // Mantiene los valores anteriores
            current_scene_ = 4;
            cover_counter_ = 100;
        }
    }
}

// Rellena la textura para la cortinilla
void Ending::fillCoverTexture()
{
    // Rellena la textura que cubre el texto con color transparente
    SDL_SetRenderTarget(Screen::get()->getRenderer(), cover_texture_);
    SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0);
    SDL_RenderClear(Screen::get()->getRenderer());

    // Los primeros 8 pixels crea una malla
    const Color color = stringToColor(options.video.palette, "black");
    SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
    for (int i = 0; i < 256; i += 2)
    {
        SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 0, GAMECANVAS_HEIGHT + 0);
        SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, GAMECANVAS_HEIGHT + 1);
        SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 0, GAMECANVAS_HEIGHT + 2);
        SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, GAMECANVAS_HEIGHT + 3);

        SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, GAMECANVAS_HEIGHT + 4);
        SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, GAMECANVAS_HEIGHT + 6);
    }

    // El resto se rellena de color sólido
    SDL_Rect rect = {0, 0, 256, GAMECANVAS_HEIGHT};
    SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);

    SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr);
}

// Dibuja la cortinilla de cambio de escena
void Ending::renderCoverTexture()
{
    if (cover_counter_ > 0)
    { // Dibuja la textura que cubre el texto
        const int offset = std::min(cover_counter_, 100);
        SDL_Rect srcRect = {0, 200 - (cover_counter_ * 2), 256, offset * 2};
        SDL_Rect dstRect = {0, 0, 256, offset * 2};
        SDL_RenderCopy(Screen::get()->getRenderer(), cover_texture_, &srcRect, &dstRect);
    }
}

// Actualiza el volumen de la musica
void Ending::updateMusicVolume()
{
    if (current_scene_ == 4 && cover_counter_ > 0)
    {
        const float step = (100.0f - cover_counter_) / 100.0f;
        const int volume = 128 * step;
        JA_SetVolume(volume);
    }
}