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

using namespace sf;

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

#include "../shell.hpp"
#include "../shellSTD.hpp"
#include "../point.hpp"

#define w 800
#define h 600
#define mag 800
#define zfactor 1


int main(int argc, char *argv[]){

    const char *test;

    RenderWindow* app = new RenderWindow(VideoMode(w, h, 32), "B==D");


    if(argc > 1)
        test = argv[1] ;
    else
          test = "../data/menu.shell";
       // test = "../data/bullet.shell";



    shell s = loadShell(test);

    float rx = 0,
           ry = 0,
           rz = 0;
    app->SetFramerateLimit(60);
    while (app->IsOpened())
    {
        //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();
        //detection de touche sans la queue d'évenement
        //il y a aussi  .GetMouseX .. Y ... .IsMouseButtonDown()
        //enum  	Button {
        //  Left,
        //  Right,
        //  Middle,
        //  XButton1,
        //  XButton2,
        //  ButtonCount
        //}
        if (app->GetInput().IsKeyDown(Key::Escape))
            return EXIT_SUCCESS;
        if (app->GetInput().IsKeyDown(Key::Space))
            s = loadShell(test);
        //clear screen
        app->Clear(Color(0, 0, 0));

        //draw
             shell tmp(s);

            rx += pi/200;
            ry += pi/400;
            rz += pi/300;

           // tmp.rotX(rx);
            tmp.rotY(ry);
           // tmp.rotZ(rz);

            tmp.add(point(0,0,10));
//            tmp.mulS(400);

            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),1,Color(64,64,64));
            app->Draw(Circle);
            for (int i=0;i<tmp.numLine();i++)
            {
                point p1 = tmp.getLinePointA(i).deuxdee(mag,zfactor),
                      p2 = tmp.getLinePointB(i).deuxdee(mag,zfactor);
                color c = tmp.getLineColor(i);
                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);
            }
            for (int i=0;i<s.numPoint();i++)
            {
                p = tmp.getPoint(i).deuxdee(mag,zfactor);
                Circle = Shape::Circle(p.getX()+w/2,
                                       p.getY()+h/2,
                                       1, Color(255,255,255),
                                       0, Color(255,255,255));
                app->Draw(Circle);
            }

        //draw

        //refresh screen
        app->Display();
    }
    return EXIT_SUCCESS;
}

//ebauche de lecteur de fichier
