#include "logo.h"
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event
#include <SDL2/SDL_timer.h>	 // for SDL_GetTicks
#include <string>			 // for basic_string, string
#include "defines.h"		 // for GAME_SPEED
#include "global_events.h"	 // for check
#include "global_inputs.h"	 // for check
#include "jail_audio.h"		 // for JA_StopMusic
#include "options.h"		 // for Options, options, SectionState, Section
#include "resource.h"		 // for Resource
#include "screen.h"			 // for Screen
#include "s_sprite.h"		 // for SSprite
#include "surface.h"		 // for Texture
#include "utils.h"			 // for Color, stringToColor

// Constructor
Logo::Logo()
	: jailgames_surface_(Resource::get()->getSurface("jailgames.gif")),
	  since_1998_surface_(Resource::get()->getSurface("since_1998.gif")),
	  since_1998_sprite_(std::make_shared<SSprite>(since_1998_surface_, (256 - since_1998_surface_->getWidth()) / 2, 83 + jailgames_surface_->getHeight() + 5, since_1998_surface_->getWidth(), since_1998_surface_->getHeight()))
{
	since_1998_sprite_->setClip(0, 0, since_1998_surface_->getWidth(), since_1998_surface_->getHeight());
	// since_1998_surface_->setColor(0, 0, 0);

	// Crea los sprites de cada linea
	for (int i = 0; i < jailgames_surface_->getHeight(); ++i)
	{
		jailgames_sprite_.push_back(std::make_shared<SSprite>(jailgames_surface_, 0, i, jailgames_surface_->getWidth(), 1));
		jailgames_sprite_.back()->setClip(0, i, jailgames_surface_->getWidth(), 1);
		jailgames_sprite_.at(i)->setX((i % 2 == 0) ? (256 + (i * 3)) : (-181 - (i * 3)));
		jailgames_sprite_.at(i)->setY(83 + i);
	}

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

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

	// Cambia el color del borde
	Screen::get()->setBorderColor(stringToColor("black"));
}

// 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)
		{
			constexpr int SPEED = 8;
			constexpr int DEST = 37;
			if (jailgames_sprite_.at(i)->getX() != 37)
			{
				if (i % 2 == 0)
				{
					jailgames_sprite_.at(i)->incX(-SPEED);
					if (jailgames_sprite_.at(i)->getX() < DEST)
					{
						jailgames_sprite_.at(i)->setX(DEST);
					}
				}
				else
				{
					jailgames_sprite_.at(i)->incX(SPEED);
					if (jailgames_sprite_.at(i)->getX() > DEST)
					{
						jailgames_sprite_.at(i)->setX(DEST);
					}
				}
			}
		}
	}
}

// Gestiona el color de las texturas
void Logo::updateTextureColors()
{
	/*constexpr int INI = 70;
	constexpr int INC = 4;

		if (counter_ == INI + INC * 0)
		{
			since_1998_surface_->setColor(color_.at(0).r, color_.at(0).g, color_.at(0).b);
		}

		else if (counter_ == INI + INC * 1)
		{
			since_1998_surface_->setColor(color_.at(1).r, color_.at(1).g, color_.at(1).b);
		}

		else if (counter_ == INI + INC * 2)
		{
			since_1998_surface_->setColor(color_.at(2).r, color_.at(2).g, color_.at(2).b);
		}

		else if (counter_ == INI + INC * 3)
		{
			since_1998_surface_->setColor(color_.at(3).r, color_.at(3).g, color_.at(3).b);
		}

		else if (counter_ == INI + INC * 4)
		{
			since_1998_surface_->setColor(color_.at(4).r, color_.at(4).g, color_.at(4).b);
		}

		else if (counter_ == INI + INC * 5)
		{
			since_1998_surface_->setColor(color_.at(5).r, color_.at(5).g, color_.at(5).b);
		}

		else if (counter_ == INI + INC * 6)
		{
			since_1998_surface_->setColor(color_.at(6).r, color_.at(6).g, color_.at(6).b);
		}

		else if (counter_ == INI + INC * 7)
		{
			since_1998_surface_->setColor(color_.at(7).r, color_.at(7).g, color_.at(7).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 0)
		{
			jailgames_surface_->setColor(color_.at(6).r, color_.at(6).g, color_.at(6).b);
			since_1998_surface_->setColor(color_.at(6).r, color_.at(6).g, color_.at(6).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 1)
		{
			jailgames_surface_->setColor(color_.at(5).r, color_.at(5).g, color_.at(5).b);
			since_1998_surface_->setColor(color_.at(5).r, color_.at(5).g, color_.at(5).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 2)
		{
			jailgames_surface_->setColor(color_.at(4).r, color_.at(4).g, color_.at(4).b);
			since_1998_surface_->setColor(color_.at(4).r, color_.at(4).g, color_.at(4).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 3)
		{
			jailgames_surface_->setColor(color_.at(3).r, color_.at(3).g, color_.at(3).b);
			since_1998_surface_->setColor(color_.at(3).r, color_.at(3).g, color_.at(3).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 4)
		{
			jailgames_surface_->setColor(color_.at(2).r, color_.at(2).g, color_.at(2).b);
			since_1998_surface_->setColor(color_.at(2).r, color_.at(2).g, color_.at(2).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 5)
		{
			jailgames_surface_->setColor(color_.at(1).r, color_.at(1).g, color_.at(1).b);
			since_1998_surface_->setColor(color_.at(1).r, color_.at(1).g, color_.at(1).b);
		}

		else if (counter_ == INIT_FADE_ + INC * 6)
		{
			jailgames_surface_->setColor(color_.at(0).r, color_.at(0).g, color_.at(0).b);
			since_1998_surface_->setColor(color_.at(0).r, color_.at(0).g, color_.at(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_ > GAME_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::get()->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::get()->start();

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

	// Dibuja los objetos
	for (const auto &s : jailgames_sprite_)
	{
		s->render(1, stringToColor("white"));
	}
	since_1998_sprite_->render(1, stringToColor("white"));

	// Vuelca el contenido del renderizador en pantalla
	Screen::get()->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;
	}
}