#include "logo.h"
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <string>			// for basic_string, string
#include "const.h"			// for SECTION_LOGO, SECTION_TITLE, SUBSECTION_...
#include "input.h"			// for Input, REPEAT_FALSE, inputs_e
#include "jail_audio.h"		// for JA_StopMusic
#include "resource.h"		// for Resource
#include "screen.h"			// for Screen
#include "sprite.h"			// for Sprite
#include "texture.h"		// for Texture
#include "utils.h"			// for color_t, section_t, options_t, stringToC...
#include "asset.h"
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
class Asset; // lines 11-11

// Constructor
Logo::Logo()
	: screen_(Screen::get()),
	  renderer_(Screen::get()->getRenderer()),
	  resource_(Resource::get()),
	  asset_(Asset::get()),
	  input_(Input::get())
{
	// Reserva memoria para los punteros
	jailgames_texture_ = resource_->getTexture("jailgames.png");
	since_1998_texture_ = resource_->getTexture("since_1998.png");
	since_1998_sprite_ = new Sprite((256 - since_1998_texture_->getWidth()) / 2, 83 + jailgames_texture_->getHeight() + 5, since_1998_texture_->getWidth(), since_1998_texture_->getHeight(), since_1998_texture_, renderer_);
	since_1998_sprite_->setSpriteClip(0, 0, since_1998_texture_->getWidth(), since_1998_texture_->getHeight());
	since_1998_texture_->setColor(0, 0, 0);

	// Crea los sprites de cada linea
	for (int i = 0; i < jailgames_texture_->getHeight(); ++i)
	{
		jailgames_sprite_.push_back(new Sprite(0, i, jailgames_texture_->getWidth(), 1, jailgames_texture_, renderer_));
		jailgames_sprite_.back()->setSpriteClip(0, i, jailgames_texture_->getWidth(), 1);
		if (i % 2 == 0)
		{
			jailgames_sprite_[i]->setPosX(256 + (i * 3));
		}
		else
		{
			jailgames_sprite_[i]->setPosX(-181 - (i * 3));
		}
		jailgames_sprite_[i]->setPosY(83 + i);
	}

	// Inicializa variables
	options.section.section = Section::LOGO;

	// Inicializa el vector de colores
	const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
	for (auto v : vColors)
	{
		color_.push_back(stringToColor(options.video.palette, v));
	}

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

// Destructor
Logo::~Logo()
{
	for (auto s : jailgames_sprite_)
	{
		delete s;
	}

	delete since_1998_sprite_;
}

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

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

// Gestiona el logo de JAILGAME
void Logo::updateJAILGAMES()
{
	if (counter_ > 30)
	{
		for (int i = 1; i < (int)jailgames_sprite_.size(); ++i)
		{
			const int speed = 8;
			const int dest = 37;
			if (jailgames_sprite_[i]->getPosX() != 37)
			{
				if (i % 2 == 0)
				{
					jailgames_sprite_[i]->incPosX(-speed);
					if (jailgames_sprite_[i]->getPosX() < dest)
					{
						jailgames_sprite_[i]->setPosX(dest);
					}
				}
				else
				{
					jailgames_sprite_[i]->incPosX(speed);
					if (jailgames_sprite_[i]->getPosX() > dest)
					{
						jailgames_sprite_[i]->setPosX(dest);
					}
				}
			}
		}
	}
}

// Gestiona el color de las texturas
void Logo::updateTextureColors()
{
	const int ini = 70;
	const int inc = 4;

	if (counter_ == ini + inc * 0)
	{
		since_1998_texture_->setColor(color_[0].r, color_[0].g, color_[0].b);
	}

	else if (counter_ == ini + inc * 1)
	{
		since_1998_texture_->setColor(color_[1].r, color_[1].g, color_[1].b);
	}

	else if (counter_ == ini + inc * 2)
	{
		since_1998_texture_->setColor(color_[2].r, color_[2].g, color_[2].b);
	}

	else if (counter_ == ini + inc * 3)
	{
		since_1998_texture_->setColor(color_[3].r, color_[3].g, color_[3].b);
	}

	else if (counter_ == ini + inc * 4)
	{
		since_1998_texture_->setColor(color_[4].r, color_[4].g, color_[4].b);
	}

	else if (counter_ == ini + inc * 5)
	{
		since_1998_texture_->setColor(color_[5].r, color_[5].g, color_[5].b);
	}

	else if (counter_ == ini + inc * 6)
	{
		since_1998_texture_->setColor(color_[6].r, color_[6].g, color_[6].b);
	}

	else if (counter_ == ini + inc * 7)
	{
		since_1998_texture_->setColor(color_[7].r, color_[7].g, color_[7].b);
	}

	else if (counter_ == init_fade_ + inc * 0)
	{
		jailgames_texture_->setColor(color_[6].r, color_[6].g, color_[6].b);
		since_1998_texture_->setColor(color_[6].r, color_[6].g, color_[6].b);
	}

	else if (counter_ == init_fade_ + inc * 1)
	{
		jailgames_texture_->setColor(color_[5].r, color_[5].g, color_[5].b);
		since_1998_texture_->setColor(color_[5].r, color_[5].g, color_[5].b);
	}

	else if (counter_ == init_fade_ + inc * 2)
	{
		jailgames_texture_->setColor(color_[4].r, color_[4].g, color_[4].b);
		since_1998_texture_->setColor(color_[4].r, color_[4].g, color_[4].b);
	}

	else if (counter_ == init_fade_ + inc * 3)
	{
		jailgames_texture_->setColor(color_[3].r, color_[3].g, color_[3].b);
		since_1998_texture_->setColor(color_[3].r, color_[3].g, color_[3].b);
	}

	else if (counter_ == init_fade_ + inc * 4)
	{
		jailgames_texture_->setColor(color_[2].r, color_[2].g, color_[2].b);
		since_1998_texture_->setColor(color_[2].r, color_[2].g, color_[2].b);
	}

	else if (counter_ == init_fade_ + inc * 5)
	{
		jailgames_texture_->setColor(color_[1].r, color_[1].g, color_[1].b);
		since_1998_texture_->setColor(color_[1].r, color_[1].g, color_[1].b);
	}

	else if (counter_ == init_fade_ + inc * 6)
	{
		jailgames_texture_->setColor(color_[0].r, color_[0].g, color_[0].b);
		since_1998_texture_->setColor(color_[0].r, color_[0].g, color_[0].b);
	}
}

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

		// Comprueba las entradas
		checkInput();

		// Incrementa el contador
		counter_++;

		// Gestiona el logo de JAILGAME
		updateJAILGAMES();

		// Gestiona el color de las texturas
		updateTextureColors();

		screen_->update();

		// Comprueba si ha terminado el logo
		if (counter_ == end_logo_ + post_logo_)
		{
			endSection();
		}
	}
}

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

	// Limpia la pantalla
	screen_->clean();

	// Dibuja los objetos
	for (auto s : jailgames_sprite_)
	{
		s->render();
	}
	since_1998_sprite_->render();

	// Vuelca el contenido del renderizador en pantalla
	screen_->render();
}

// Bucle para el logo del juego
void Logo::run()
{
	// Detiene la música
	JA_StopMusic();

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

// Termina la sección
void Logo::endSection()
{
	if (options.section.subsection == Subsection::LOGO_TO_TITLE)
	{
		options.section.section = Section::TITLE;
	}

	else if (options.section.subsection == Subsection::LOGO_TO_INTRO)
	{
		options.section.section = Section::LOADING_SCREEN;
	}
}