#include "title.h"

// Constructor
Title::Title(SDL_Renderer *renderer, Screen *screen, Asset *asset)
{
	// Copia la dirección de los objetos
	this->renderer = renderer;
	this->screen = screen;
	this->asset = asset;

	// Reserva memoria para los punteros
	eventHandler = new SDL_Event();
	texture = new LTexture(renderer, asset->get("loading_screen2.png"));
	sprite = new Sprite(0, 0, texture->getWidth(), texture->getHeight(), texture, renderer);
	text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer);
	music = JA_LoadMusic(asset->get("title.ogg").c_str());

	// Inicializa variables
	counter = 0;
	section.name = SECTION_PROG_TITLE;
	section.subsection = 0;
	ticks = 0;
	ticksSpeed = 15;
	longText = "HEY JAILERS!! IT'S 2022 AND WE'RE STILL ROCKING LIKE IT'S 1998!!! HAVE YOU HEARD IT? JAILGAMES ARE BACK!! YEEESSS BACK!! MORE THAN 10 TITLES ON JAILDOC'S KITCHEN!! THATS A LOOOOOOT OF JAILGAMES, BUT WHICH ONE WILL STRIKE FIRST? THERE IS ALSO A NEW DEVICE TO COME P.A.C.O. THAT WILL BLOW YOUR MIND WITH JAILGAMES ON THE GO. BUT WAIT! WHAT'S THAT BEAUTY I'M SEEING RIGHT OVER THERE?? OOOH THAT TINY MINIASCII IS PURE LOVE!! I WANT TO LICK EVERY BYTE OF IT!! OH SHIT! AND DON'T FORGET TO BRING BACK THOSE OLD AND FAT MS-DOS JAILGAMES TO GITHUB TO KEEP THEM ALIVE!! WHAT WILL BE THE NEXT JAILDOC RELEASE? WHAT WILL BE THE NEXT PROJECT TO COME ALIVE?? OH BABY WE DON'T KNOW BUT HERE YOU CAN FIND THE ANSWER, YOU JUST HAVE TO COMPLETE JAILDOCTOR'S DILEMMA ... COULD YOU?";
	for (int i = 0; i < longText.length(); i++)
	{
		letter_t l;
		l.letter = longText.substr(i, 1);
		l.x = 256;
		l.enabled = false;
		letters.push_back(l);
	}
	letters[0].enabled = true;

	// Cambia el color del borde
	screen->setBorderColor(stringToColor("bright_blue"));
}

// Destructor
Title::~Title()
{
	delete eventHandler;
	delete texture;
	delete sprite;
	delete text;
	JA_DeleteMusic(music);
}

// Comprueba el manejador de eventos
void Title::checkEventHandler()
{
	// Comprueba los eventos que hay en la cola
	while (SDL_PollEvent(eventHandler) != 0)
	{
		// Evento de salida de la aplicación
		if (eventHandler->type == SDL_QUIT)
		{
			section.name = SECTION_PROG_QUIT;
			break;
		}

		// Comprueba si se ha pulsado alguna tecla
		else if ((eventHandler->type == SDL_KEYDOWN) and (eventHandler->key.repeat == 0))
		{
			switch (eventHandler->key.keysym.scancode)
			{
			case SDL_SCANCODE_ESCAPE:
				section.name = SECTION_PROG_QUIT;
				break;

			case SDL_SCANCODE_RETURN:
				section.name = SECTION_PROG_GAME;
				section.subsection = 0;
				break;

			default:
				break;
			}
		}
	}
}

// Actualiza la marquesina
void Title::updateMarquee()
{
	for (int i = 0; i < letters.size(); i++)
	{
		if (letters[i].enabled)
		{
			letters[i].x -= 2;
			if (letters[i].x < -10)
			{
				letters[i].enabled = false;
			}
		}
		else
		{
			if (i > 0 && letters[i - 1].x < 250)
			{
				letters[i].enabled = true;
			}
		}
	}
}

// Dibuja la marquesina
void Title::renderMarquee()
{
	for (auto l : letters)
	{
		if (l.enabled)
		{
			// text->writeColored(l.x, 176, l.letter, {0, 0, 0});
			text->write(l.x, 176, l.letter);
		}
	}
}

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

		// Comprueba el manejador de eventos
		checkEventHandler();

		// Incrementa el contador
		counter++;

		// Actualiza la marquesina
		updateMarquee();

		// Comprueba si ha pasado mucho tiempo y acaba el titulo
		// if (counter == 1000)

		// Comprueba si ha terminado la marquesina y acaba con el titulo
		if (letters[letters.size() - 1].x < -10)
		{
			section.name = SECTION_PROG_LOGO;
			section.subsection = 0;
		}
	}
}

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

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

	// Dibuja el fondo del titulo
	sprite->render();

	// Dibuja el texto de PRESS ENTER TO PLAY
	SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
	SDL_Rect rect = {0, 192 / 5 * 4, 256, 8};
	SDL_RenderFillRect(renderer, &rect);
	if (counter % 80 < 60)
		text->writeCentered(256 / 2, 192 / 5 * 4, "PRESS ENTER TO PLAY");

	// Dibuja la marquesina
	renderMarquee();

	// Vuelca el contenido del renderizador en pantalla
	screen->blit();
}

// Bucle para el logo del juego
section_t Title::run()
{
	JA_PlayMusic(music);

	while (section.name == SECTION_PROG_TITLE)
	{
		update();
		render();
	}

	JA_StopMusic();

	return section;
}