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

using namespace sf;

#include <vector>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

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

#define w 800
#define h 600
shell loadShell(const char * filename);

int main(int argc, char *argv[]){

    const char *test;
    const char *test2;

    camera camwhore;

    camwhore.setRadius(5);

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


    if(argc > 1)
        test = argv[1] ;
    else
    {
      test2 = "../data/sweedish.shell";
       // test2 = "../data/tank.shell";
       // test2 = "../data/robot.shell";
    }


   // shell s = loadShell(test);
    shell t = loadShell(test2);

    //camwhore.loadShell(t);
    camwhore.moveZ(5);

    float rx = 0,
           ry = 0,
           rz = 0;

    while (app->IsOpened())
    {
        //recevoir la ev
        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);
            t = loadShell(test2);
        }
        app->Clear(Color(0, 0, 0));



           // rx += pi/2000;
            ry += 0.0025;
            rz += 0.00025;

            camwhore.turnY(ry);

            camwhore.addShell(t);
            Shape Ligne;
            Shape Circle;


         cout<<"==============================================="<<endl;

        while(!camwhore.bufferEmpty())
        {

            cout<<camwhore.currentLine()._p1X+w/2<<" "<<
                               camwhore.currentLine()._p1Y+h/2<<" "<<
                               camwhore.currentLine()._p2X+w/2<<" "<<
                               camwhore.currentLine()._p2Y+h/2<<" "<<"Camera Z: "<<
                               camwhore.getZ()<<endl;
            //camera::draw bob = camwhore._stkLigne.top();
            Ligne = Shape::Line(camwhore.currentLine()._p1X+w/2,
                               camwhore.currentLine()._p1Y+h/2,
                               camwhore.currentLine()._p2X+w/2,
                               camwhore.currentLine()._p2Y+h/2,
                               1, Color(camwhore.currentLine()._r, camwhore.currentLine()._g, camwhore.currentLine()._b),
                               0, Color(255,255,255));
            app->Draw(Ligne);
            camwhore.unmountBuffer();
        }

        //refresh screen
        app->Display();
    }
    return EXIT_SUCCESS;
}
