// G. Hagopian

#include <iostream>
#include<vector>
#include <cstdlib>
#include <ctime>
#include <SFML/Graphics.hpp>

const float ballNo = 4;
const float ballRadius = 10;
const float sqrt3 = 1.73205080756887729;
const int tableWidth = 1200;
const int tableHeight = 600;

class Axis : public sf::Drawable {

};

std::vector<sf::CircleShape> initPoolBalls(std::vector<sf::Vector2f>& vel) {
	std::vector<sf::CircleShape> balls(ballNo);
	for (int i = 0; i < ballNo; ++i) {
		balls[i].setRadius(ballRadius);
		balls[i].setOrigin(ballRadius,ballRadius);
		vel[i] = sf::Vector2f{ float(rand() % 10) / 10,float(rand() % 10) / 10 };
	}
	balls[0].setFillColor(sf::Color::White);
	balls[1].setFillColor(sf::Color(255,255,0)); //Yellow
	balls[2].setFillColor(sf::Color(255, 0, 0)); //Red
	balls[3].setFillColor(sf::Color(0, 0, 255)); //Blue
	balls[0].setPosition(sf::Vector2f(tableWidth/4, tableHeight/2));
	balls[1].setPosition(sf::Vector2f(3*tableWidth/4, tableHeight/2));
	balls[2].setPosition(sf::Vector2f(3*tableWidth/4+ballRadius*sqrt3,
								      tableHeight/2+ballRadius));
	balls[3].setPosition(sf::Vector2f(3 * tableWidth / 4 + ballRadius * sqrt3,
									      tableHeight / 2 - ballRadius));
	return balls;
}

void updateBalls(std::vector<sf::CircleShape>& balls,
	        std::vector<sf::Vector2f>& vel) {
	for (int i = 0; i < balls.size(); ++i) {
		balls[i].move(vel[i].x, vel[i].y);
		if (balls[i].getPosition().x < ballRadius
			|| balls[i].getPosition().x > tableWidth - ballRadius)
			vel[i].x *= -1;
		if (balls[i].getPosition().y < ballRadius
			|| balls[i].getPosition().y > tableHeight - ballRadius)
			vel[i].y *= -1;
	}
}

int main() {
	srand(time(0));
	sf::RenderWindow window(sf::VideoMode(tableWidth, tableHeight), "Billiards!");
	window.setFramerateLimit(120);
    std::vector<sf::Vector2f> vel(ballNo);
	std::vector<sf::CircleShape> balls = initPoolBalls(vel);
	   	
	vel[0].x = 1.f;
	vel[0].y = 1.f;
	sf::RectangleShape r(sf::Vector2f{ 20.f, 10.f });
	r.setPosition(100, 200);

	// create an empty shape
	sf::ConvexShape convex;

	// resize it to 5 points
	convex.setPointCount(5);

	// define the points
	convex.setPoint(0, sf::Vector2f(0.f, 0.f));
	convex.setPoint(1, sf::Vector2f(150.f, 10.f));
	convex.setPoint(2, sf::Vector2f(120.f, 90.f));
	convex.setPoint(3, sf::Vector2f(30.f, 100.f));
	convex.setPoint(4, sf::Vector2f(0.f, 50.f));
	convex.setFillColor(sf::Color(0, 200, 200));
	convex.setPosition(sf::Vector2f(600, 150));

	sf::Vertex line[] =
	{
		sf::Vertex(sf::Vector2f(10.f, 10.f)),
		sf::Vertex(sf::Vector2f(150.f, 150.f)),
		sf::Vertex(sf::Vector2f(300.f, 10.f)),
		sf::Vertex(sf::Vector2f(450.f, 150.f))
	};

	sf::Texture texture1, texture2;
	if (!texture1.loadFromFile("balls.jpg")) {
		std::cerr << "wth";
	}
	if (!texture2.create(200, 200)) {
		std::cerr << "texture2 failed to be created.\n";
	}
	int width = 10, height = 10;
	// update a texture from an array of pixels
	sf::Uint8* pixels = new sf::Uint8[width * height * 4]; // * 4 because pixels have 4 components (RGBA)
	for (int i = 0; i < width * height * 4; ++i)
		pixels[i] = 100; // rand() % 256;
	//texture2.update(pixels);

	// update a texture from a sf::Image
	//sf::Image image;
	//texture1.update(image);
	sf::Sprite sprite;
	sprite.setTexture(texture1);

	while (window.isOpen()) 	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		window.clear(sf::Color(21,88,67)); //Green?
		updateBalls(balls, vel);
		r.rotate(1);
		window.draw(sprite);
		window.draw(r);
		convex.rotate(1);
		window.draw(convex);
		window.draw(line, 4, sf::LineStrip);
		
		for (int i = 0; i < balls.size(); ++i)
			window.draw(balls[i]);
		window.display();
	}

	return 0;
}