#include "credits.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 "animated_sprite.h"	// for AnimatedSprite
#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 "options.h"			// for Options, options, OptionsVideo, Sect...
#include "resource.h"			// for Resource
#include "screen.h"				// for Screen
#include "text.h"				// for Text, TEXT_CENTER, TEXT_COLOR

// Constructor
Credits::Credits()
{
	// Reserva memoria para los punteros
	sprite_ = std::make_shared<AnimatedSprite>(Resource::get()->getTexture("shine.png"), Resource::get()->getAnimations("shine.ani"));

	// Inicializa variables
	options.section.section = Section::CREDITS;
	options.section.subsection = Subsection::NONE;
	sprite_->setPosition({194, 174, 8, 8});

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

	// Crea la textura para el texto que se escribe en pantalla
	text_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
	if (text_texture_ == nullptr)
	{
		if (options.console)
		{
			std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
		}
	}
	SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND);

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

	// Escribe el texto en la textura
	fillTexture();
}

// Destructor
Credits::~Credits()
{
	SDL_DestroyTexture(text_texture_);
	SDL_DestroyTexture(cover_texture_);
}

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

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

// Inicializa los textos
void Credits::iniTexts()
{
#ifndef GAME_CONSOLE
	std::string keys = "";

	switch (options.keys)
	{
	case ControlScheme::CURSOR:
		keys = "CURSORS";
		break;
	case ControlScheme::OPQA:
		keys = "O,P AND Q";
		break;
	case ControlScheme::WASD:
		keys = "A,D AND W";
		break;
	default:
		break;
	}

	texts_.clear();
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
	texts_.push_back({"INSTRUCTIONS:", stringToColor(options.video.palette, "yellow")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
	texts_.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options.video.palette, "white")});
	texts_.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options.video.palette, "white")});
	texts_.push_back({"JAIL TO FINISH THEM", stringToColor(options.video.palette, "white")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});

	texts_.push_back({"KEYS:", stringToColor(options.video.palette, "yellow")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
	texts_.push_back({keys + " TO MOVE AND JUMP", stringToColor(options.video.palette, "white")});
	texts_.push_back({"M TO SWITCH THE MUSIC", stringToColor(options.video.palette, "white")});
	texts_.push_back({"H TO PAUSE THE GAME", stringToColor(options.video.palette, "white")});
	texts_.push_back({"F1-F2 TO CHANGE WINDOWS SIZE", stringToColor(options.video.palette, "white")});
	texts_.push_back({"F3 TO SWITCH TO FULLSCREEN", stringToColor(options.video.palette, "white")});
	texts_.push_back({"B TO TOOGLE THE BORDER SCREEN", stringToColor(options.video.palette, "white")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});

	texts_.push_back({"A GAME BY JAILDESIGNER", stringToColor(options.video.palette, "yellow")});
	texts_.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options.video.palette, "yellow")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});

	texts_.push_back({"I LOVE JAILGAMES! ", stringToColor(options.video.palette, "white")});
	texts_.push_back({"", stringToColor(options.video.palette, "white")});
#else
	texts.clear();
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"INSTRUCTIONS:", stringToColor(options.video.palette, "yellow")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options.video.palette, "white")});
	texts.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options.video.palette, "white")});
	texts.push_back({"JAIL TO FINISH THEM", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});

	texts.push_back({"KEYS:", stringToColor(options.video.palette, "yellow")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"B TO JUMP", stringToColor(options.video.palette, "white")});
	texts.push_back({"R TO SWITCH THE MUSIC", stringToColor(options.video.palette, "white")});
	texts.push_back({"L TO SWAP THE COLOR PALETTE", stringToColor(options.video.palette, "white")});
	texts.push_back({"START TO PAUSE", stringToColor(options.video.palette, "white")});
	texts.push_back({"SELECT TO EXIT", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});

	texts.push_back({"A GAME BY JAILDESIGNER", stringToColor(options.video.palette, "yellow")});
	texts.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options.video.palette, "yellow")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});

	texts.push_back({"I LOVE JAILGAMES! ", stringToColor(options.video.palette, "white")});
	texts.push_back({"", stringToColor(options.video.palette, "white")});
#endif
}

// Escribe el texto en la textura
void Credits::fillTexture()
{
	// Inicializa los textos
	iniTexts();

	// Rellena la textura de texto
	SDL_SetRenderTarget(Screen::get()->getRenderer(), text_texture_);
	Color color = stringToColor(options.video.palette, "black");
	SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
	SDL_RenderClear(Screen::get()->getRenderer());

	auto text = Resource::get()->getText("smb2");

	// Escribe el texto en la textura
	const int SIZE = text->getCharacterSize();
	int pos_y = 0;

	for (const auto &t : texts_)
	{
		text->writeDX(TEXT_CENTER | TEXT_COLOR, PLAY_AREA_CENTER_X, pos_y * SIZE, t.label, 1, t.color);
		pos_y++;
	}

	// Escribe el corazón
	const int TEXT_LENGHT = text->lenght(texts_[22].label, 1) - text->lenght(" ", 1); // Se resta el ultimo caracter que es un espacio
	const int POS_X = ((PLAY_AREA_WIDTH - TEXT_LENGHT) / 2) + TEXT_LENGHT;
	text->writeColored(POS_X, 176, "}", stringToColor(options.video.palette, "bright_red"));

	// Recoloca el sprite del brillo
	sprite_->setPosX(POS_X + 2);

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

	// Rellena la textura que cubre el texto con color transparente
	SDL_SetRenderTarget(Screen::get()->getRenderer(), cover_texture_);
	SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0x00);
	SDL_RenderClear(Screen::get()->getRenderer());

	// Los primeros 8 pixels crea una malla
	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);
		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, 256, 192};
	SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);

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

// Actualiza el contador
void Credits::updateCounter()
{
	// Incrementa el contador
	if (counter_enabled_)
	{
		counter_++;
		if (counter_ == 224 || counter_ == 544 || counter_ == 672)
		{
			counter_enabled_ = false;
		}
	}
	else
	{
		sub_counter_++;
		if (sub_counter_ == 100)
		{
			counter_enabled_ = true;
			sub_counter_ = 0;
		}
	}

	// Comprueba si ha terminado la sección
	if (counter_ > 1200)
	{
		options.section.section = Section::DEMO;
	}
}

// Actualiza las variables
void Credits::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
		updateCounter();

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

		// Actualiza el sprite con el brillo
		if (counter_ > 770)
		{
			sprite_->update();
		}
	}
}

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

	// Limpia la pantalla
	Screen::get()->clean();

	if (counter_ < 1150)
	{
		// Dibuja la textura con el texto en pantalla
		SDL_RenderCopy(Screen::get()->getRenderer(), text_texture_, nullptr, nullptr);

		// Dibuja la textura que cubre el texto
		const int offset = std::min(counter_ / 8, 192 / 2);
		SDL_Rect srcRect = {0, 0, 256, 192 - (offset * 2)};
		SDL_Rect dstRect = {0, offset * 2, 256, 192 - (offset * 2)};
		SDL_RenderCopy(Screen::get()->getRenderer(), cover_texture_, &srcRect, &dstRect);

		// Dibuja el sprite con el brillo
		sprite_->render();
	}

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

// Bucle para el logo del juego
void Credits::run()
{
	while (options.section.section == Section::CREDITS)
	{
		update();
		checkEvents();
		render();
	}
}