#include "fade.h"
#include <SDL3/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND, SDL_BLENDMODE_NONE
#include <SDL3/SDL_pixels.h>	// Para SDL_PIXELFORMAT_RGBA8888
#include <stdlib.h>				// Para rand
#include <algorithm>			// Para min, max
#include "param.h"				// Para Param, param, ParamGame, ParamFade
#include "screen.h"				// Para Screen

// Constructor
Fade::Fade()
	: renderer_(Screen::get()->getRenderer())
{
	// Crea la textura donde dibujar el fade
	backbuffer_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height);
	SDL_SetTextureBlendMode(backbuffer_, SDL_BLENDMODE_BLEND);

	// Inicializa las variables
	init();
}

// Destructor
Fade::~Fade()
{
	SDL_DestroyTexture(backbuffer_);
}

// Inicializa las variables
void Fade::init()
{
	type_ = FadeType::CENTER;
	mode_ = FadeMode::OUT;
	counter_ = 0;
	r_ = 0;
	g_ = 0;
	b_ = 0;
	a_ = 0;
	post_duration_ = 0;
	post_counter_ = 0;
	pre_duration_ = 0;
	pre_counter_ = 0;
	num_squares_width_ = param.fade.num_squares_width;
	num_squares_height_ = param.fade.num_squares_height;
	fade_random_squares_delay_ = param.fade.random_squares_delay;
	fade_random_squares_mult_ = param.fade.random_squares_mult;
}

// Resetea algunas variables para volver a hacer el fade sin perder ciertos parametros
void Fade::reset()
{
	state_ = FadeState::NOT_ENABLED;
	counter_ = 0;
}

// Pinta una transición en pantalla
void Fade::render()
{
	if (state_ != FadeState::NOT_ENABLED)
	{
		SDL_RenderTexture(renderer_, backbuffer_, nullptr, nullptr);
	}
}

// Actualiza las variables internas
void Fade::update()
{
	if (state_ == FadeState::PRE)
	{
		// Actualiza el contador
		if (pre_counter_ == pre_duration_)
		{
			state_ = FadeState::FADING;
		}
		else
		{
			pre_counter_++;
		}
	}

	if (state_ == FadeState::FADING)
	{
		switch (type_)
		{
		case FadeType::FULLSCREEN:
		{
			// Modifica la transparencia
			a_ = mode_ == FadeMode::OUT ? std::min(counter_ * 4, 255) : 255 - std::min(counter_ * 4, 255);

			SDL_SetTextureAlphaMod(backbuffer_, a_);

			// Comprueba si ha terminado
			if (counter_ >= 255 / 4)
			{
				state_ = FadeState::POST;
			}

			break;
		}

		case FadeType::CENTER:
		{
			// Dibuja sobre el backbuffer_
			auto temp = SDL_GetRenderTarget(renderer_);
			SDL_SetRenderTarget(renderer_, backbuffer_);

			SDL_SetRenderDrawColor(renderer_, r_, g_, b_, a_);

			for (int i = 0; i < counter_; i++)
			{
				rect1_.h = rect2_.h = i * 4;
				rect2_.y = param.game.height - (i * 4);

				SDL_RenderFillRect(renderer_, &rect1_);
				SDL_RenderFillRect(renderer_, &rect2_);

				value_ = calculateValue(0, counter_, i);
			}

			// Deja el renderizador como estaba
			SDL_SetRenderTarget(renderer_, temp);

			// Comprueba si ha terminado
			if ((counter_ * 4) > param.game.height)
			{
				state_ = FadeState::POST;
				a_ = 255;
			}
			break;
		}

		case FadeType::RANDOM_SQUARE:
		{
			if (counter_ % fade_random_squares_delay_ == 0)
			{
				// Cambia el renderizador al backbuffer_ y modifica sus opciones
				auto temp = SDL_GetRenderTarget(renderer_);
				SDL_SetRenderTarget(renderer_, backbuffer_);
				SDL_BlendMode blend_mode;
				SDL_GetRenderDrawBlendMode(renderer_, &blend_mode);
				SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_NONE);
				SDL_SetRenderDrawColor(renderer_, r_, g_, b_, a_);

				// Dibuja el cuadrado correspondiente
				const int INDEX = std::min(counter_ / fade_random_squares_delay_, (num_squares_width_ * num_squares_height_) - 1);
				for (int i = 0; i < fade_random_squares_mult_; ++i)
				{
					const int INDEX2 = std::min(INDEX * fade_random_squares_mult_ + i, (int)square_.size() - 1);
					SDL_RenderFillRect(renderer_, &square_[INDEX2]);
				}

				// Deja el renderizador como estaba
				SDL_SetRenderDrawBlendMode(renderer_, blend_mode);
				SDL_SetRenderTarget(renderer_, temp);
			}

			value_ = calculateValue(0, static_cast<int>(num_squares_width_ * num_squares_height_), static_cast<int>(counter_ * fade_random_squares_mult_ / fade_random_squares_delay_));

			// Comprueba si ha terminado
			if (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_ >= num_squares_width_ * num_squares_height_)
			{
				state_ = FadeState::POST;
			}

			break;
		}

		case FadeType::VENETIAN:
		{
			// Counter debe ir de 0 a 150 <-- comprobar si esto es aún cierto
			if (square_.back().h < param.fade.venetian_size)
			{
				// Dibuja sobre el backbuffer_
				auto temp = SDL_GetRenderTarget(renderer_);
				SDL_SetRenderTarget(renderer_, backbuffer_);
				SDL_BlendMode blend_mode;
				SDL_GetRenderDrawBlendMode(renderer_, &blend_mode);
				SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_NONE);
				SDL_SetRenderDrawColor(renderer_, r_, g_, b_, a_);

				// Dibuja el cuadrado correspondiente
				for (const auto rect : square_)
				{
					SDL_RenderFillRect(renderer_, &rect);
				}

				// Deja el renderizador como estaba
				SDL_SetRenderDrawBlendMode(renderer_, blend_mode);
				SDL_SetRenderTarget(renderer_, temp);

				// Modifica el tamaño de los rectangulos
				const auto h = counter_ / 2;
				for (size_t i = 0; i < square_.size(); ++i)
				{
					// A partir del segundo rectangulo se pinta en función del anterior
					square_.at(i).h = i == 0 ? h : std::max(static_cast<int>(square_.at(i - 1).h) - 2, 0);
				}

				int completed = 0;
				for (const auto &square : square_)
				{
					if (square.h >= param.fade.venetian_size)
					{
						++completed;
					}
				}
				value_ = calculateValue(0, square_.size() - 1, completed);
			}
			else
			{
				state_ = FadeState::POST;
			}

			break;
		}
		default:
			break;
		}
		counter_++;
	}

	if (state_ == FadeState::POST)
	{
		// Actualiza el contador
		if (post_counter_ == post_duration_)
		{
			state_ = FadeState::FINISHED;
		}
		else
		{
			post_counter_++;
		}

		// Deja el backbuffer_ todo del mismo color
		cleanBackbuffer(r_, g_, b_, a_);
	}
}

// Activa el fade
void Fade::activate()
{
	// Si ya está habilitado, no hay que volverlo a activar
	if (state_ != FadeState::NOT_ENABLED)
	{
		return;
	}

	state_ = FadeState::PRE;
	counter_ = 0;
	post_counter_ = 0;
	pre_counter_ = 0;

	switch (type_)
	{
	case FadeType::FULLSCREEN:
	{
		// Pinta el backbuffer_ de color sólido
		cleanBackbuffer(r_, g_, b_, 255);
		break;
	}

	case FadeType::CENTER:
	{
		rect1_ = {0, 0, static_cast<float>(param.game.width), 0};
		rect2_ = {0, 0, static_cast<float>(param.game.width), 0};
		a_ = 64;
		break;
	}

	case FadeType::RANDOM_SQUARE:
	{
		rect1_ = {0, 0, static_cast<float>(param.game.width / num_squares_width_), static_cast<float>(param.game.height / num_squares_height_)};
		square_.clear();

		// Añade los cuadrados al vector
		for (int i = 0; i < num_squares_width_ * num_squares_height_; ++i)
		{
			rect1_.x = (i % num_squares_width_) * rect1_.w;
			rect1_.y = (i / num_squares_width_) * rect1_.h;
			square_.push_back(rect1_);
		}

		// Desordena el vector de cuadrados
		auto num = num_squares_width_ * num_squares_height_;
		while (num > 1)
		{
			auto num_arreu = rand() % num;
			SDL_FRect temp = square_[num_arreu];
			square_[num_arreu] = square_[num - 1];
			square_[num - 1] = temp;
			num--;
		}

		// Limpia la textura
		a_ = mode_ == FadeMode::OUT ? 0 : 255;
		cleanBackbuffer(r_, g_, b_, a_);

		// Deja el color listo para usar
		a_ = mode_ == FadeMode::OUT ? 255 : 0;

		break;
	}

	case FadeType::VENETIAN:
	{
		// Limpia la textura
		a_ = mode_ == FadeMode::OUT ? 0 : 255;
		cleanBackbuffer(r_, g_, b_, a_);

		// Deja el color listo para usar
		a_ = mode_ == FadeMode::OUT ? 255 : 0;

		// Añade los cuadrados al vector
		square_.clear();
		rect1_ = {0, 0, static_cast<float>(param.game.width), 0};
		const int MAX = param.game.height / param.fade.venetian_size;

		for (int i = 0; i < MAX; ++i)
		{
			rect1_.y = i * param.fade.venetian_size;
			square_.push_back(rect1_);
		}

		break;
	}
	}
}

// Establece el color del fade
void Fade::setColor(Uint8 r, Uint8 g, Uint8 b)
{
	r_ = r;
	g_ = g;
	b_ = b;
}

// Limpia el backbuffer
void Fade::cleanBackbuffer(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
	// Dibujamos sobre el backbuffer_
	auto temp = SDL_GetRenderTarget(renderer_);
	SDL_SetRenderTarget(renderer_, backbuffer_);

	// Pintamos la textura con el color del fade
	SDL_SetRenderDrawColor(renderer_, r, g, b, a);
	SDL_RenderClear(renderer_);

	// Vuelve a dejar el renderizador como estaba
	SDL_SetRenderTarget(renderer_, temp);
}

// Calcula el valor del estado del fade
int Fade::calculateValue(int min, int max, int current)
{
	if (current < min)
	{
		return 0;
	}

	if (current > max)
	{
		return 100;
	}

	return static_cast<int>(100.0 * (current - min) / (max - min));
}