#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>

const unsigned int winWidth{ 800 };
const unsigned int winHeight{ 800 };

class Board {
	/* The Board will know how big it is and how to draw the dots, lines and boxes
	*/
	int size;
	int moveNo{ 0 };
public:
	Board(sf::RenderWindow& window, int sz, std::vector<sf::CircleShape>& dots, sf::VertexArray& va) : size(sz) {
		//std::vector< sf::CircleShape> dots(size * size);
		double breadth{ 0.8 * winWidth / (size - 1) };
		//double breadth = winWidth / (size - 1);
		//breadth *= 0.8;
		for (int i = 0; i < size * size; ++i) {
			double col = i % size, row = i/size;
			dots[i].setRadius(4);
			dots[i].setFillColor(sf::Color(0, 255, 0));
			dots[i].setPosition(0.1 * winWidth + breadth * col,
				0.1 * winHeight + breadth * row);
			dots[i].setOrigin(4, 4);
		}
		Draw(window, dots, va);
	}
	void Draw(sf::RenderWindow& window, std::vector<sf::CircleShape> dots, sf::VertexArray& lines) {
		for(int i = 0; i < dots.size(); ++i)
			window.draw(dots[i]);
		window.draw(lines);
	}
	void getMove(sf::VertexArray& lines) {
		double breadth{ 0.8 * winWidth / (size - 1) };
		int a{ 0 }, b{ 0 };
		do {
			std::cout << "Enter valid dot numbers to connect (0 is upper left): ";
			std::cin >> a >> b;
		} while (!(abs(a - b) == size || (std::max(a, b) % size != 0 && abs(a - b) == 1)));
		double row = a / size, col = a % size;
		lines[2*moveNo] = sf::Vertex(sf::Vector2f(0.1 * winWidth + col*breadth, 0.1 * winHeight + row*breadth));
		row = b / size, col = b % size;
		lines[2 * moveNo+1] = sf::Vertex(sf::Vector2f(0.1 * winWidth + col * breadth, 0.1 * winHeight + row * breadth));
		++moveNo;
	}

};
int main()
{
	// create the window
	unsigned int edge = 4;
	sf::RenderWindow window(sf::VideoMode(winWidth, winHeight), "Dots Nice");
	window.setFramerateLimit(60);
	
	//std::cout << "How big would you like your board (number of dots on the edge of the square array): ";
	//std::cin >> edge;
	std::vector<sf::CircleShape> dots(edge * edge);
	sf::VertexArray lines(sf::Lines, 2 * edge * (edge - 1)); //100); // 

	Board board(window, edge, dots, lines);
	// run the program as long as the window is open
	while (window.isOpen())
	{
		// check all the window's events that were triggered since the last iteration of the loop
		sf::Event event;
		while (window.pollEvent(event))
		{
			// "close requested" event: we close the window
			if (event.type == sf::Event::Closed)
				window.close();
		}

		// clear the window with black color
		window.clear(sf::Color::Black);

		// draw everything here...
		// window.draw(...);
		
		//for (int i = 0; i < edge * edge; ++i)
		//	window.draw(dots[i]);
		// end the current frame
		board.getMove(lines);
		board.Draw(window, dots, lines);
		//window.draw(lines, 2, sf::Lines);
		window.display();
	}

	return 0;
}