#include "complex.h"

#define resolution 1


int iterates(Complex& c) {
	Complex z, znew(c);
	int i = 0;
	while ((z - znew).getMod() > 0.001) {
		++i;
		z = znew;
		znew = z * z + c;
		if ((z - znew).getMod() > 100) return 0;
		if (i > 100) {
			return 100;
		}
	}
	return i;
}

int main() {
	Complex c(-2, -2), 
			deltaco(4*double(resolution)/wWidth, 0), 
		    deltac0(0, 4*double(resolution)/wHeight);
	int N{ 0 }; // the return value of iterates
	vector<vector<sf::RectangleShape >> vectRectS;
	vector<sf::RectangleShape> vectR;
	for (int i = 0; i < wHeight / resolution; ++i) {
		vectR.clear();
		for (int j = 0; j < wWidth / resolution; ++j) {
			N = iterates(c);
			
			sf::RectangleShape rs;
			rs.setSize(sf::Vector2f(resolution, resolution));
			rs.setPosition(c.getReal() * 200 + 400, c.getImag() * 200 + 400);
			switch (N) {
			case 0:
				rs.setFillColor(sf::Color::Color(255, 105, 180));//Pink
				break;
			case 100:
				rs.setFillColor(sf::Color::Color(155, 155, 155));//Gray
				break;
			default:
				rs.setFillColor(sf::Color::Color(0, 0, 10 * N));//Blue
			}
            c = c + deltaco;
			vectR.push_back(rs);
		}
		vectRectS.push_back(vectR);
		c.setReal(-2.0);
		c = c + deltac0;
	}

    for (int i = 0; i < wHeight / resolution; ++i) {
        for (int j = 0; j < wWidth / resolution; ++j) {
            //std::cout << '(' << vectRectS[i][j].getPosition().x << ',' << vectRectS[i][j].getPosition().y << ')' << " ";
        }
        //std::cout << '\n';
    }


	sf::RenderWindow window(sf::VideoMode(wWidth, wHeight), 
		                               "Fractal");
	window.setFramerateLimit(1);

	

	//std::cout << "What is your value of c?\n";
	//std::cin >> c;

	// set coordinates of initial vertices

	// This "while" loop goes round and round- perhaps forever
	sf::CircleShape pt(6);
	pt.setFillColor(sf::Color::Color(250, 125, 62));
	

	while (window.isOpen()) {
		// The next 6 lines of code detect if the window is closed
		// And then shuts down the program
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				// Someone closed the window- bye
				window.close();
		}
		// Clear everything from the last run of the while loop
		
		
		window.clear();
		
		// draw the scene
		
		//pt.setPosition(c.getReal()*200+400, c.getImag()*200+400);
		
		//window.draw(pt);
        /*window.draw(vectRectS[0][0]);
        window.draw(vectRectS[0][1]);
        window.draw(vectRectS[0][2]);
        window.draw(vectRectS[0][3]);
        window.draw(vectRectS[0][4]);
        window.draw(vectRectS[0][5]);
        window.draw(vectRectS[0][6]);
        window.draw(vectRectS[0][7]);
        window.draw(vectRectS[1][0]);
        window.draw(vectRectS[1][1]);
        window.draw(vectRectS[1][2]);
        window.draw(vectRectS[1][3]);
        window.draw(vectRectS[1][4]);
        window.draw(vectRectS[1][5]);
        window.draw(vectRectS[1][6]);
        window.draw(vectRectS[1][7]);
        window.draw(vectRectS[2][0]);
        window.draw(vectRectS[2][1]);
        window.draw(vectRectS[2][2]);
        window.draw(vectRectS[2][3]);
        window.draw(vectRectS[2][4]);
        window.draw(vectRectS[2][5]);
        window.draw(vectRectS[2][6]);
        window.draw(vectRectS[2][7]);
        window.draw(vectRectS[3][0]);
        window.draw(vectRectS[3][1]);
        window.draw(vectRectS[3][2]);
        window.draw(vectRectS[3][3]);
        window.draw(vectRectS[3][4]);
        window.draw(vectRectS[3][5]);
        window.draw(vectRectS[3][6]);
        window.draw(vectRectS[3][7]);
        window.draw(vectRectS[4][0]);
        window.draw(vectRectS[4][1]);
        window.draw(vectRectS[4][2]);
        window.draw(vectRectS[4][3]);
        window.draw(vectRectS[4][4]);
        window.draw(vectRectS[4][5]);
        window.draw(vectRectS[4][6]);
        window.draw(vectRectS[4][7]);
        window.draw(vectRectS[5][0]);
        window.draw(vectRectS[5][1]);
        window.draw(vectRectS[5][2]);
        window.draw(vectRectS[5][3]);
        window.draw(vectRectS[5][4]);
        window.draw(vectRectS[5][5]);
        window.draw(vectRectS[5][6]);
        window.draw(vectRectS[5][7]);
        window.draw(vectRectS[6][0]);
        window.draw(vectRectS[6][1]);
        window.draw(vectRectS[6][2]);
        window.draw(vectRectS[6][3]);
        window.draw(vectRectS[6][4]);
        window.draw(vectRectS[6][5]);
        window.draw(vectRectS[6][6]);
        window.draw(vectRectS[6][7]);
        window.draw(vectRectS[7][0]);
        window.draw(vectRectS[7][1]);
        window.draw(vectRectS[7][2]);
        window.draw(vectRectS[7][3]);
        window.draw(vectRectS[7][4]);
        window.draw(vectRectS[7][5]);
        window.draw(vectRectS[7][6]);
        window.draw(vectRectS[7][7]);*/
		for (int i = 0; i < wHeight / resolution; ++i) {
			for (int j = 0; j < wWidth / resolution; ++j) {
                //std::cout << '(' << vectRectS[i][j].getPosition().x << ',' << vectRectS[i][j].getPosition().y << ')' << " ";
				window.draw(vectRectS[i][j]);
			}
            //std::cout << "\n";
		}
		// Show everything we just drew
		window.display();
	}
}