#include <Game.h>
#include <vector>

#include <Util.h>

#include <SFML/Graphics.hpp>
#include <imgui-SFML.h>
#include <imgui.h>

#include <algorithm>
#include <fstream>
#include <iostream>

Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in)
	: pos(position_in)
	, velocity(vel_in)
{ }

Brick::Brick(sf::Vector2f position_in)
	: pos(position_in)
{ }

Player::Player(sf::Vector2f position_in)
	: pos(position_in)
{ }


Game::Game()
	: window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"})
	, font("assets/fonts/ChakraPetch-Regular.ttf")
	, lives(font)
{ 
	
	if (!ImGui::SFML::Init(window)) return;
	
	if (!parseConfigFile()) return;

	lives.setPosition({10, static_cast<float>(window.getSize().y - 40)});
	window.setFramerateLimit(framerate);

	player.pos = playerStartPos;
	playerHalfSize = playerSize / 2.f;
	brickHalfSize = brickSize / 2.f;

	tempCircle.setOrigin({ballRadius, ballRadius});
	ballMaxSpeed /= numPhysicsUpdates;

	bricks.reserve(totalBricks);
	specialBricks.reserve(totalSpecialBricks);
	balls.reserve(10);
	ballsToAdd.reserve(10);

	setupLevel();

	ballsToAdd.emplace_back(playerStartPos.x, playerStartPos.y - 50);
}

void Game::setupLevel()
{
	const unsigned int specialBrickInterval = totalBricks / totalSpecialBricks;

	constexpr float xSpacing = 20.f;
	constexpr float ySpacing = 20.f;
	const float xOffset = brickSize.x + xSpacing;
	const float yOffset = brickSize.y + ySpacing;
	const unsigned int numRows = window.getSize().x / xOffset;
	const unsigned int numColumns = (window.getSize().y - 300)/ yOffset;
	unsigned int row{};
	unsigned int column{};

	for (size_t i = 0; i < totalBricks; i++)
	{
		if (row >= numRows)
		{
			row = 0;
			column++;
		}

		if (column >= numColumns)
			break;

		if (i % specialBrickInterval == 0) 
		{
			specialBricks.emplace_back(sf::Vector2f{(row * xOffset) + xSpacing + brickHalfSize.x, (column * yOffset) + ySpacing + brickHalfSize.y});
		}
		else 
		{
			bricks.emplace_back(sf::Vector2f{(row * xOffset) + xSpacing + brickHalfSize.x, (column * yOffset) + ySpacing + brickHalfSize.y});
		}
		row++;
	}
}

bool Game::parseConfigFile()
{
	std::ifstream configFile{"Config/game-config.txt"};

	if(!configFile)
	{
		std::cerr << "ERROR: CANNOT OPEN 'game-config.txt'\n";
		return false;
	}

	std::string inputBuff;
	while(configFile >> inputBuff)
	{
		if (inputBuff == "framerate") 
			configFile >> framerate;

		if (inputBuff == "ballRadius") 
			configFile >> ballRadius;

		if (inputBuff == "playerSpeed") 
			configFile >> playerSpeed;

		if (inputBuff == "brickCount") 
			configFile >> totalBricks;

		if (inputBuff == "specialBrickCount") 
			configFile >> totalSpecialBricks;

		if (inputBuff == "brickSize") 
			configFile >> brickSize.x >> brickSize.y;

		if (inputBuff == "playerSize") 
			configFile >> playerSize.x >> playerSize.y;

		if (inputBuff == "ballColor") 
			configFile >> ballColor.r >> ballColor.g >> ballColor.b;

		if (inputBuff == "playerStartPos") 
			configFile >> playerStartPos.x >> playerStartPos.y;

		if (inputBuff == "brickColor") 
			configFile >> brickColor.r >> brickColor.g >> brickColor.b;

		if (inputBuff == "playerColor") 
			configFile >> playerColor.r >> playerColor.g >> playerColor.b;

		if (inputBuff == "specialBrickColor") 
			configFile >> specialBrickColor.r >> specialBrickColor.g >> specialBrickColor.b;

		if (inputBuff == "ballSpeed")
			configFile >> ballMaxSpeed;
	}

	if (totalSpecialBricks > totalBricks)
		totalSpecialBricks = totalBricks;

	return true;
}

void Game::input()
{
	while (const std::optional event = window.pollEvent())
	{
		ImGui::SFML::ProcessEvent(window, *event);

		if (event->is<sf::Event::Closed>())
		{
			window.close();
		}

		if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
		{
			switch (keyPressed->scancode) 
			{
				case sf::Keyboard::Scan::Escape:
					window.close();
					break;
				
				case sf::Keyboard::Scan::Left:
				case sf::Keyboard::Scan::A:
					player.left = true;
					break;

				case sf::Keyboard::Scan::Right:
				case sf::Keyboard::Scan::D:
					player.right = true;
					break;

				case sf::Keyboard::Scan::F1:
					windowCollasped = !windowCollasped;
					break;

				case sf::Keyboard::Scan::R:
					resetGame();
					break;
					
				default:
					break;
			}
		}

		if (const auto* keyReleased = event->getIf<sf::Event::KeyReleased>())
		{
			switch (keyReleased->scancode) 
			{
				case sf::Keyboard::Scan::Left:
				case sf::Keyboard::Scan::A:
					player.left = false;
					break;

				case sf::Keyboard::Scan::Right:
				case sf::Keyboard::Scan::D:
					player.right = false;
					break;

				default:
					break;
			}
		}

		if (const auto* MouseButtonPressed = event->getIf<sf::Event::MouseButtonPressed>())
		{
			switch (MouseButtonPressed->button) 
			{
				case sf::Mouse::Button::Left:
					//specialBricks.emplace_back((sf::Vector2f)MouseButtonPressed->position);
					break;

				default:
					break;
			}

		}
	}	
}

void Game::collision()
{
	
	checkBallCollision();
	
	const sf:: FloatRect playerBounds = {player.pos, playerHalfSize};
	//player wall collide
	if (playerBounds.position.x + playerBounds.size.x > window.getSize().x)
	{
		player.pos.x = window.getSize().x - playerHalfSize.x;
	}
	
	if (playerBounds.position.x < playerHalfSize.x)
	{
		player.pos.x = playerHalfSize.x;
	}
}

void Game::checkBallCollision()
{
	for (auto& ball : balls)	
	{
		const sf::FloatRect ballBounds = {ball.pos, {ballRadius, ballRadius}};
		const sf::FloatRect previousBallBounds = {ball.previousPos, {ballRadius, ballRadius}};

		for (auto& brick : bricks)
		{
			const sf::FloatRect brickBounds = {brick.pos, brickHalfSize};
			const auto intersect = getOverlap(ballBounds, brickBounds);
			const auto previousIntersect = getOverlap(previousBallBounds, brickBounds);

			// coming from x directon
			if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
			{
				brick.alive = false;
				ball.velocity.x *= -1;
				if (ball.pos.x < brick.pos.x)
				{// from the left
					ball.pos.x -= intersect.x;
				}
				else 
				{// from the right
					ball.pos.x += intersect.x;
				}
			}

			// coming from y direction
			if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
			{
				brick.alive = false;
				ball.velocity.y *= -1;
				if (ball.pos.y < brick.pos.y)
				{// from the top
					ball.pos.y -= intersect.y;
				}
				else 
				{// from the bottom
					ball.pos.y += intersect.y;
				}
			}
		}

		for (auto& brick : specialBricks)
		{
			const sf::FloatRect brickBounds = {brick.pos, brickHalfSize};
			const auto intersect = getOverlap(ballBounds, brickBounds);
			const auto previousIntersect = getOverlap(previousBallBounds, brickBounds);
			bool spawnExtraBall{false};

			// coming from x direction
			if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
			{
				brick.alive = false;
				spawnExtraBall = true;
				ball.velocity.x *= -1;
				if (ball.pos.x < brick.pos.x)
				{// from the left
					ball.pos.x -= intersect.x;
				}
				else 
				{// from the right
					ball.pos.x += intersect.x;
				}
			}

			// coming from y direction
			if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
			{
				brick.alive = false;
				spawnExtraBall = true;
				ball.velocity.y *= -1;
				if (ball.pos.y < brick.pos.y)
				{// from the top
					ball.pos.y -= intersect.y;
				}
				else 
				{// from the bottom
					ball.pos.y += intersect.y;
				}
			}

			if (spawnExtraBall)
			{
				ballsToAdd.emplace_back(ball.pos);
			}
		}

		//wall collide
		if (ballBounds.position.y + ballBounds.size.y > window.getSize().y)
		{
			ball.alive = false;
		}

		if (ball.pos.x - ballRadius <= 0)
		{
			ball.pos.x = ballRadius;
			ball.velocity.x *= -1;
		}

		if (ball.pos.x + ballRadius >= window.getSize().x)
		{
			ball.pos.x = window.getSize().x - ballRadius;
			ball.velocity.x *= -1;
		}

		if (ball.pos.y - ballRadius < 0)
		{
			ball.pos.y = ballRadius;
			ball.velocity.y *= -1;
		}

		// player collide
		const sf:: FloatRect playerBounds = {player.pos, playerHalfSize};
		const sf:: FloatRect previousPlayerBounds = {player.previousPos, playerHalfSize};
		
		const auto intersect = getOverlap(ballBounds, playerBounds);
		const auto previousIntersect = getOverlap(previousBallBounds, previousPlayerBounds);

		// coming from x direction
		if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
		{
			ball.velocity.x *= -1;
			if (ball.pos.x < player.pos.x)
			{
				ball.pos.x -= intersect.x;
			}
			else 
			{
				ball.pos.x += intersect.x;
			}
		}

		// coming from y direction
		if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
		{
			ball.velocity.y *= -1;
			if (ball.pos.y < player.pos.y)
			{
				ball.pos.y -= intersect.y;
			}
			else 
			{
				ball.pos.y += intersect.y;
			}
		}

	}
}

void Game::updateEntities()
{
	balls.erase(std::remove_if(balls.begin(), balls.end(), [](const Ball& ball){ return !ball.alive; }), balls.end());
	bricks.erase(std::remove_if(bricks.begin(), bricks.end(), [](const Brick& brick){ return !brick.alive; }), bricks.end());
	specialBricks.erase(std::remove_if(specialBricks.begin(), specialBricks.end(), [](const Brick& brick){ return !brick.alive; }), specialBricks.end());

	const auto limit = ballsToAdd.size();

	for (size_t i = 0; i < limit; i++)
	{
		balls.emplace_back(ballsToAdd[i], velocityInRandomDir(ballMaxSpeed));
		ballsToAdd[i] = {0,0};
	}

	ballsToAdd.clear();
}

void Game::checkEndGame()
{
	if (balls.empty())
	{
		player.lives--;
		ballsToAdd.emplace_back(sf::Vector2f{player.pos.x, player.pos.y - 50});
	}


	if (player.lives <= 0)
	{
		player.lives = 3;
		resetGame();
		ballsToAdd.emplace_back(sf::Vector2f{player.pos.x, player.pos.y - 50});
	}
}

void Game::movement()
{
	for (auto& ball : balls)
	{
		ball.previousPos = ball.pos;
		ball.pos += ball.velocity;
	}

	const int dir = player.right - player.left;

	player.previousPos = player.pos;
	player.pos.x += (playerSpeed / numPhysicsUpdates) * dir;
}

void Game::imgui()
{
	
	ImGui::Begin("menu");
	ImGui::SetWindowCollapsed(true);
	ImGui::Text("Controls:");
	ImGui::Indent();
	ImGui::Text("A/D or arrow keys: move left and right");
	ImGui::Text("R: reset game");
	ImGui::Text("F1: expand/collaspe this window");
	ImGui::Text("left click: spawn brick");
	ImGui::Unindent();

	ImGui::SliderFloat("Player Speed", &playerSpeed, 1.f, 50.f);
	ImGui::SliderFloat("Ball Speed", &ballMaxSpeed, 1.f, 50.f);
	if (ImGui::SliderFloat("Player Length", &playerSize.x, 1.f, 600.f))
	{
		playerHalfSize.x = playerSize.x / 2;
	}

	ImGui::ColorEdit3("Player Color", playerColor.imgui());
	ImGui::ColorEdit3("Ball Color", ballColor.imgui());
	ImGui::ColorEdit3("Brick Color", brickColor.imgui());
	ImGui::ColorEdit3("Special Brick Color", specialBrickColor.imgui());  

	ImGui::End();
}

void Game::render()
{
		window.clear();
		
		lives.setString("Lives: " + std::to_string(player.lives));
		window.draw(lives);
		tempRect.setOrigin(brickHalfSize);
		tempRect.setSize(brickSize);
		for (auto& brick : bricks)
		{
			tempRect.setPosition(brick.pos);
			tempRect.setFillColor(brickColor.sfml());
			window.draw(tempRect);
		}
		
		for (auto& brick : specialBricks)
		{
			tempRect.setPosition(brick.pos);
			tempRect.setFillColor(specialBrickColor.sfml());
			window.draw(tempRect);
		}
		
		tempRect.setOrigin(playerHalfSize);
		tempRect.setSize(playerSize);
		tempRect.setPosition(player.pos);
		tempRect.setFillColor(playerColor.sfml());
		window.draw(tempRect);

		tempCircle.setRadius(ballRadius);
		for (auto& ball : balls)
		{
			tempCircle.setPosition(ball.pos);
			tempCircle.setFillColor(ballColor.sfml());
			window.draw(tempCircle);
		}

		ImGui::SFML::Render(window);
		
		window.display();
}

void Game::resetGame()
{
	bricks.clear();
	specialBricks.clear();
	balls.clear();
	ballsToAdd.clear();

	setupLevel();
}

void Game::run()
{
	while(window.isOpen())
	{		
		ImGui::SFML::Update(window, clock.restart());
		
		input();

		for (size_t i = 0; i <= numPhysicsUpdates; i++)
		{
			updateEntities();

			movement();
			
			collision();
		}

		checkEndGame();
		
		imgui();

		render();
	}

	ImGui::SFML::Shutdown();
}

void Game::runNoImgui()
{
	while(window.isOpen())
	{		
		ImGui::SFML::Update(window, clock.restart());
		
		input();

		for (size_t i = 0; i <= numPhysicsUpdates; i++)
		{
			updateEntities();

			movement();
			
			collision();
		}

		checkEndGame();

		render();
	}

	ImGui::SFML::Shutdown();
}