#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include <iostream>
//#include <math.h>


#include "Global.h"
#include "ExtraMath.h"
#include "PhyShapes.h"

#include "Board.h"

sf::RenderWindow window(sf::VideoMode(2 * HALFWIDTH, 2 * HALFHEIGHT), "Sim");

Sim sim;


int mousey;
int mousex;



/*
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(25, 10)),
sf::Vertex(sf::Vector2f(400, 100))
};

window.draw(line, 2, sf::Line);
*/

void drawEverything() {

	//draw board
	sf::RectangleShape sfboard;
	sfboard.setFillColor(sf::Color(20, 20, 20));
	sfboard.setSize({ boardwidth/scale, boardheight/scale });
	sfboard.setPosition(HALFWIDTH - (boardwidth / 2)/scale, HALFHEIGHT - (boardheight / 2)/scale);
	window.draw(sfboard);
	
	//draw slopes
	sf::RectangleShape sfslope;
	sfslope.setFillColor(sf::Color(30, 30, 30));
	sfslope.setSize({ boardwidth / scale, slopesize / scale });
	sfslope.setOrigin(boardwidth / scale / 2, 200 / scale);
	sfslope.setPosition((boardwidth / 2) / scale + WCORRECTION, ((boardheight / 2) - slopesize / 2) / scale + HALFHEIGHT);
	window.draw(sfslope);
	sfslope.setPosition((boardwidth / 2) / scale + WCORRECTION, (-(boardheight / 2) + slopesize / 2) / scale + HALFHEIGHT);
	window.draw(sfslope);
	
	//draw ball
	sf::CircleShape sfball;
	sfball.setRadius(ballradius/scale);
	sfball.setFillColor(sf::Color::White);
	sfball.setOrigin(ballradius/scale, ballradius/scale);
	sfball.setPosition(sim.ball->position.x/scale + WCORRECTION, sim.ball->position.y/scale + HALFHEIGHT);
	window.draw(sfball);


	///BLUE
	sf::RectangleShape sfbox;
	sfbox.setSize({ boxwidth/scale, boxheight/scale });
	sfbox.setOrigin(boxwidth/scale / 2, boxheight/scale / 2);
	
	for (int i = 0; i < 4; i++) {

		if (sim.red[i].collide) {
			sfbox.setFillColor(sf::Color::Red);
		}
		else {
			sfbox.setFillColor(sf::Color(100, 0, 0)); //darkred
		}

		for (int j = 0; j < sim.red[i].size; j++) {
			sfbox.setPosition((sim.red[i].position.x + sim.red[i].x_offset) / scale + WCORRECTION, (sim.red[i].position.y + sim.red[i].player[j].y_offset) / scale + HALFHEIGHT);
			window.draw(sfbox);
		}
	}

	///RED
	

	for (int i = 0; i < 4; i++) {

		if (sim.blu[i].collide) {
			sfbox.setFillColor(sf::Color::Blue);
		}
		else {
			sfbox.setFillColor(sf::Color(0, 0, 100)); //darkblue
		}


		for (int j = 0; j < sim.blu[i].size; j++) {
			sfbox.setPosition((sim.blu[i].position.x + sim.blu[i].x_offset) / scale + WCORRECTION, (sim.blu[i].position.y + sim.blu[i].player[j].y_offset) / scale + HALFHEIGHT);
			window.draw(sfbox);
		}
	}

	//draw axes

	for (int i = 0; i < 4; i++) {

		sf::Vertex line[] = {
			sf::Vertex(sf::Vector2f(sim.red[i].position.x / scale + WCORRECTION, -boardheight / 2 / scale + HALFHEIGHT)),
			sf::Vertex(sf::Vector2f(sim.red[i].position.x / scale + WCORRECTION, boardheight / 2 / scale + HALFHEIGHT))
		};
		for (int i = 0; i < 2; i++) line[i].color = (sf::Color::Red);
		window.draw(line, 2, sf::Lines);
	}

	for (int i = 0; i < 4; i++) {

		sf::Vertex line[] = {
			sf::Vertex(sf::Vector2f(sim.blu[i].position.x / scale + WCORRECTION, -boardheight / 2 / scale + HALFHEIGHT)),
			sf::Vertex(sf::Vector2f(sim.blu[i].position.x / scale + WCORRECTION, boardheight / 2 / scale + HALFHEIGHT))
		};
		for (int i = 0; i < 2; i++) line[i].color = (sf::Color::Blue);
		window.draw(line, 2, sf::Lines);
	}
	
	//draw boundary
	sf::RectangleShape sfbound;
	sfbound.setFillColor(sf::Color(50, 50, 50));

	
	for (int i = 0; i < 6; i++) {
		sfbound.setSize({ sim.boundary[i].width/scale, sim.boundary[i].height/scale });
		sfbound.setOrigin(sim.boundary[i].width/scale / 2, sim.boundary[i].height/scale / 2);
		sfbound.setPosition(sim.boundary[i].position.x / scale + WCORRECTION, sim.boundary[i].position.y / scale + HALFHEIGHT);
		window.draw(sfbound);
	}



	return;
}



int main() {

	sim.setup();

	//Initial ball speed
	sim.ball->velocity.x = 200; 
	sim.ball->velocity.y = -1000; 

	window.setFramerateLimit(FPS);
	sf::Event event;

	while (window.isOpen()) {

		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
			if (event.type == sf::Event::KeyPressed) {
				if (event.key.code == sf::Keyboard::Key::Escape) {
					window.close();
				}
				else if (event.key.code == sf::Keyboard::Key::Return) {
					std::cin.get();
				}

			}
			//Right mouse button
			if (event.type == sf::Event::MouseButtonPressed) {
				if (event.key.code == sf::Mouse::Right) {
					sim.ball->position = Vec2(mousex - WCORRECTION, mousey - HALFHEIGHT).scale(scale);
				}
			}
		}//end event testing
		

		mousex = sf::Mouse::getPosition(window).x;
		mousey = sf::Mouse::getPosition(window).y;

		//Left mouse button
		if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
			sim.ball->velocity = vecSum(sim.ball->velocity, vecDiff(Vec2(mousex, mousey), Vec2(sim.ball->position.x / scale + WCORRECTION, sim.ball->position.y / scale + HALFHEIGHT)).scale(TIMESTEP*100));
		}

		window.clear();
		
		sim.move();
		sim.collide();

		

		drawEverything();


		window.display();

		//Speed of ball
		//std::cout << sqrt(sim.ball->velocity.lengthSquared()) << "\n";

		//Keyboard control
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
			for (int i = 0; i < 4; i++) sim.red[i].velocity.y = -8000;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
			for (int i = 0; i < 4; i++) sim.red[i].velocity.y = 8000;
		}
		else {
			for (int i = 0; i < 4; i++) sim.red[i].velocity.y = 0;
		}
		
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
			for (int i = 0; i < 4; i++) sim.red[i].angular_velocity = -8000;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
			for (int i = 0; i < 4; i++) sim.red[i].angular_velocity = 8000;
		}
		else {
			for (int i = 0; i < 4; i++) sim.red[i].angular_velocity = 0;
		}

		
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
			for (int i = 0; i < 4; i++) sim.blu[i].velocity.y = -8000;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
			for (int i = 0; i < 4; i++) sim.blu[i].velocity.y = 8000;
		}
		else {
			for (int i = 0; i < 4; i++) sim.blu[i].velocity.y = 0;
		}


		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
			for (int i = 0; i < 4; i++) sim.blu[i].angular_velocity = -8000;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
			for (int i = 0; i < 4; i++) sim.blu[i].angular_velocity = 8000;
		}
		else {
			for (int i = 0; i < 4; i++) sim.blu[i].angular_velocity = 0;
		}
		

	}//end game loop

	sim.del();

	return 0;
}
