#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 "defines.h"            // for options.game.height, options.game.width
#include "global_events.h"      // for check
#include "global_inputs.h"      // for check
#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 "s_sprite.h"           // for SSprite
#include "text.h"               // for Text, TEXT_STROKE
#include "surface.h"            // for Surface
#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("black"));

    // Crea la textura para cubrir el texto
    cover_surface_ = std::make_shared<Surface>(options.game.width, options.game.height + 8);

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

// 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()->clearSurface(stringToColor("black"));

    // Dibuja las imagenes de la escena
    sprite_pics_.at(current_scene_).image_sprite->render();
    sprite_pics_.at(current_scene_).cover_sprite->render();

    // Dibuja los textos de la escena
    for (const auto &ti : scenes_.at(current_scene_).text_index)
    {
        if (counter_ > ti.trigger)
        {
            sprite_texts_.at(ti.index).image_sprite->render();
            sprite_texts_.at(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 &txt : texts)
    {
        auto text = Resource::get()->getText("smb2");

        const int WIDTH = text->lenght(txt.caption, 1) + 2 + 2;
        const int HEIGHT = text->getCharacterSize() + 2 + 2;
        auto text_color = stringToColor("white");
        auto shadow_color = stringToColor("black");

        EndingSurface st;

        // Crea la textura
        st.image_surface = std::make_shared<Surface>(WIDTH, HEIGHT);
        Screen::get()->setRendererSurface(st.image_surface);
        text->writeDX(TEXT_STROKE, 2, 2, txt.caption, 1, text_color, 2, shadow_color);

        // Crea el sprite
        st.image_sprite = std::make_shared<SSprite>(st.image_surface, 0, 0, st.image_surface->getWidth(), st.image_surface->getHeight());
        st.image_sprite->setPosition((options.game.width - st.image_surface->getWidth()) / 2, txt.pos);

        // Crea la cover_surface
        st.cover_surface = std::make_shared<Surface>(WIDTH, HEIGHT + 8);
        Screen::get()->setRendererSurface(st.cover_surface);

        // Rellena la cover_surface con color transparente
        st.cover_surface->clear(stringToColor("transparent"));

        // Crea una malla de 8 pixels de alto
        auto surface = Screen::get()->getRendererSurface();
        auto color = stringToColor("black");
        for (int i = 0; i < WIDTH; i += 2)
        {
            surface->putPixel(i, 0, color);
            surface->putPixel(i, 2, color);
            surface->putPixel(i, 4, color);
            surface->putPixel(i, 6, color);

            surface->putPixel(i + 1, 5, color);
            surface->putPixel(i + 1, 7, color);
        }

        // El resto se rellena de color sólido
        SDL_Rect rect = {0, 8, WIDTH, HEIGHT};
        surface->fillRect(&rect, color);

        // Crea el sprite
        st.cover_sprite = std::make_shared<SSprite>(st.cover_surface, 0, 0, st.cover_surface->getWidth(), st.cover_surface->getHeight() - 8);
        st.cover_sprite->setPosition((options.game.width - st.cover_surface->getWidth()) / 2, txt.pos);
        st.cover_sprite->setClip(0, 8, st.cover_surface->getWidth(), st.cover_surface->getHeight());

        // Inicializa variables
        st.cover_clip_desp = 8;
        st.cover_clip_height = HEIGHT;

        sprite_texts_.push_back(st);
        Screen::get()->setRendererSurface(nullptr);
    }
}

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

    pics.push_back({"ending1.gif", 48});
    pics.push_back({"ending2.gif", 26});
    pics.push_back({"ending3.gif", 29});
    pics.push_back({"ending4.gif", 63});
    pics.push_back({"ending5.gif", 53});

    // Crea los sprites
    sprite_pics_.clear();

    for (const auto &pic : pics)
    {
        EndingSurface sp;

        // Crea la texture
        sp.image_surface = Resource::get()->getSurface(pic.caption);
        const int WIDTH = sp.image_surface->getWidth();
        const int HEIGHT = sp.image_surface->getHeight();

        // Crea el sprite
        sp.image_sprite = std::make_shared<SSprite>(sp.image_surface, 0, 0, WIDTH, HEIGHT);
        sp.image_sprite->setPosition((options.game.width - WIDTH) / 2, pic.pos);

        // Crea la cover_surface
        sp.cover_surface = std::make_shared<Surface>(WIDTH, HEIGHT + 8);
        Screen::get()->setRendererSurface(sp.cover_surface);

        // Rellena la cover_surface con color transparente
        sp.cover_surface->clear(stringToColor("transparent"));

        // Crea una malla en los primeros 8 pixels
        auto surface = Screen::get()->getRendererSurface();
        auto color = stringToColor("black");
        for (int i = 0; i < WIDTH; i += 2)
        {
            surface->putPixel(i, 0, color);
            surface->putPixel(i, 2, color);
            surface->putPixel(i, 4, color);
            surface->putPixel(i, 6, color);

            surface->putPixel(i + 1, 5, color);
            surface->putPixel(i + 1, 7, color);
        }

        // El resto se rellena de color sólido
        SDL_Rect rect = {0, 8, WIDTH, HEIGHT};
        surface->fillRect(&rect, color);

        // Crea el sprite
        sp.cover_sprite = std::make_shared<SSprite>(sp.cover_surface, 0, 0, sp.cover_surface->getWidth(), sp.cover_surface->getHeight() - 8);
        sp.cover_sprite->setPosition((options.game.width - sp.cover_surface->getWidth()) / 2, pic.pos);
        sp.cover_sprite->setClip(0, 8, sp.cover_surface->getWidth(), sp.cover_surface->getHeight());

        // Inicializa variables
        sp.cover_clip_desp = 8;
        sp.cover_clip_height = HEIGHT;

        sprite_pics_.push_back(sp);
        Screen::get()->setRendererSurface(nullptr);
    }
}

// Inicializa las escenas
void Ending::iniScenes()
{
    // Variable para los tiempos
    int trigger;
    constexpr 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_.at(current_scene_).text_index)
        {
            if (counter_ > ti.trigger)
            {
                if (sprite_texts_.at(ti.index).cover_clip_desp > 0)
                {
                    sprite_texts_.at(ti.index).cover_clip_desp -= 2;
                }
                else if (sprite_texts_.at(ti.index).cover_clip_height > 0)
                {
                    sprite_texts_.at(ti.index).cover_clip_height -= 2;
                    sprite_texts_.at(ti.index).cover_sprite->setY(sprite_texts_.at(ti.index).cover_sprite->getY() + 2);
                }
                sprite_texts_.at(ti.index).cover_sprite->setClip(0, sprite_texts_.at(ti.index).cover_clip_desp, sprite_texts_.at(ti.index).cover_sprite->getWidth(), sprite_texts_.at(ti.index).cover_clip_height);
            }
        }
    }

    // Actualiza la cortinilla de las imágenes
    if (counter_ % 2 == 0)
    {
        if (sprite_pics_.at(current_scene_).cover_clip_desp > 0)
        {
            sprite_pics_.at(current_scene_).cover_clip_desp -= 2;
        }
        else if (sprite_pics_.at(current_scene_).cover_clip_height > 0)
        {
            sprite_pics_.at(current_scene_).cover_clip_height -= 2;
            if (sprite_pics_.at(current_scene_).cover_clip_height < 0)
            {
                sprite_pics_.at(current_scene_).cover_clip_height = 0;
            }
            sprite_pics_.at(current_scene_).cover_sprite->setY(sprite_pics_.at(current_scene_).cover_sprite->getY() + 2);
        }
        sprite_pics_.at(current_scene_).cover_sprite->setClip(0, sprite_pics_.at(current_scene_).cover_clip_desp, sprite_pics_.at(current_scene_).cover_sprite->getWidth(), sprite_pics_.at(current_scene_).cover_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
    Screen::get()->setRendererSurface(cover_surface_);
    cover_surface_->clear(stringToColor("transparent"));

    // Los primeros 8 pixels crea una malla
    const Uint8 color = stringToColor("black");
    auto surface = Screen::get()->getRendererSurface();
    for (int i = 0; i < 256; i += 2)
    {

        surface->putPixel(i + 0, options.game.height + 0, color);
        surface->putPixel(i + 1, options.game.height + 1, color);
        surface->putPixel(i + 0, options.game.height + 2, color);
        surface->putPixel(i + 1, options.game.height + 3, color);

        surface->putPixel(i, options.game.height + 4, color);
        surface->putPixel(i, options.game.height + 6, color);
    }

    // El resto se rellena de color sólido
    SDL_Rect rect = {0, 0, 256, options.game.height};
    surface->fillRect(&rect, color);

    Screen::get()->setRendererSurface(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};
        cover_surface_->render(&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);
    }
}