#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

using namespace sf;

#include <assert.h>
#include <vector>

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

#include "../shell.hpp"
#include "../disk.hpp"
#include "../shellSTD.hpp"
#include "../point.hpp"

#define w 800
#define h 600
#define mag 800
#define zfactor 1

int main()
{
//	shell cube = loadShell("../data/cube.shell"),
//		  pine = loadShell("../data/pine.shell"),
//		  robo = loadShell("../data/robot.shell"),
//		  tank = loadShell("../data/tank.shell");
//	robo.divS(3);
//	vector<shell> world;
//	world.push_back(tank);
//	world.push_back(cube);
//	world.back().add(point(4,0,4));
//	world.push_back(pine);
//	world.back().add(point(0,0,5));
//	world.push_back(robo);
//	world.back().add(point(5,0,0));

	disk world = loadDisk("../data/battlezone.disk");

    for (int i=0;i<world.size();i++)
        cout << world.get(i).numLine() << endl;

	point cpos(0,16,0),
		  tpos(0,0,0),
		  tmov(0,0,0);
	float trad=0;

    RenderWindow* app = new RenderWindow(VideoMode(w, h, 32), "B==D");
    app->SetFramerateLimit(60);
    while (app->IsOpened())
    {
        //clear screen
        app->Clear(Color(0, 0, 0));
        //recevoir la event queue
        Event* event = new Event();
        //pour chaque évenement de la queue
        while (app->GetEvent(*event))
            if (event->Type == Event::Closed)
                app->Close();
        if (app->GetInput().IsKeyDown(Key::Escape))
            return EXIT_SUCCESS;
        if (app->GetInput().IsKeyDown(Key::Space))
            world = loadDisk("../data/battlezone.disk");
		//move tank
		if (app->GetInput().IsKeyDown(Key::W) || app->GetInput().IsKeyDown(Key::Up))
			tmov = point(0,0,0.01).rotY(trad);
		if (app->GetInput().IsKeyDown(Key::S) || app->GetInput().IsKeyDown(Key::Down))
			tmov = point(0,0,0.01).rotY(trad);
		if (app->GetInput().IsKeyDown(Key::A) || app->GetInput().IsKeyDown(Key::Left))
			trad -= pi/300;
		if (app->GetInput().IsKeyDown(Key::D) || app->GetInput().IsKeyDown(Key::Right))
			trad += pi/300;



		//movetank
		shell nextTank = world.model(0);
		nextTank.rotY(trad);
		int i;
		if (!world.worldWillCollide(0,tmov))
        {
			tpos = tpos + tmov;
			cpos = cpos - tmov;
            nextTank.add(tpos);
        }
        else
        {
            nextTank.add(tpos);
        }
		world.set(0,nextTank);
		tmov = point(0,0,0);
        //render
		for (i=0;i<world.size();i++)
		{
            shell tmp(world.get(i));

            tmp.add(cpos);
            tmp.rotX(pi/2);

            Shape Line;
			Shape Circle;
            point p = tmp.getCenter().deuxdee(mag,zfactor);
			Circle = Shape::Circle(p.getX()+w/2, p.getY()+h/2,
								   tmp.getRadius()*mag/tmp.getCenter().getZ(),
								   Color(0,0,0,0),1,Color(64,64,64,255));
			app->Draw(Circle);

            for (int j=0;j<tmp.numLine();j++)
            {
                point p1 = tmp.getLinePointA(j).deuxdee(mag,zfactor),
                      p2 = tmp.getLinePointB(j).deuxdee(mag,zfactor);
                color c = tmp.getLineColor(j);
                Line = Shape::Line(p1.getX()+w/2,
                                   p1.getY()+h/2,
                                   p2.getX()+w/2,
                                   p2.getY()+h/2,
                                   1, Color(c.r,c.g,c.b),
                                   0, Color(255,255,255));
                app->Draw(Line);
            }
		}
        //draw

        //refresh screen
        app->Display();
    }
    return EXIT_SUCCESS;
}

//ebauche de lecteur de fichier
