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

#include <Util.h>

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

#include <fstream>
#include <iostream>


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

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

	window.setFramerateLimit(framerate);

}

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)
	{

	}

	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::Up:
				case sf::Keyboard::Scan::W:
					player.up = true;
					break;

				case sf::Keyboard::Scan::Down:
				case sf::Keyboard::Scan::S:
					player.down = true;
					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;

				case sf::Keyboard::Scan::Up:
				case sf::Keyboard::Scan::W:
					player.up = false;
					break;

				case sf::Keyboard::Scan::Down:
				case sf::Keyboard::Scan::S:
					player.down = false;
					break;					

				default:
					break;
			}
		}
	}	
}

void Game::collision()
{
	

}

void Game::movement()
{

}

void Game::imgui()
{
	ImGui::SetNextWindowCollapsed(false, ImGuiCond_Once);
	ImGui::SetNextWindowSize({490,375}, ImGuiCond_Once);
	ImGui::SetNextWindowPos({26,29}, ImGuiCond_Once);
	if(!ImGui::Begin("menu"))
	{
		ImGui::End();
		return;
	}
	ImGui::Text("Controls:");
	ImGui::Indent();
	ImGui::Text("A/D or arrow keys: move left and right");
	ImGui::Text("R: reset game");
	ImGui::Unindent();

	if (ImGui::SliderFloat("Game Volume", &volume, 0, 100, "%.1f"))
	{

	}


	ImGui::End();
}

void Game::render()
{
		window.clear();
		
		lives.setString("Lives: " + std::to_string(player.lives));
		score.setString("Score: " + std::to_string(player.score));

		window.draw(lives);
		window.draw(score);

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

void Game::soundSystem()
{

}

void Game::scoreSystem()
{

}

void Game::resetGame()
{

}

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

		movement();
			
		collision();
			
		soundSystem();

		scoreSystem();
		
		imgui();

		render();
		
	}

	ImGui::SFML::Shutdown();
}