#include <iostream>
#include <SDL/SDL.h>

#include <ctime>
#include <cstdlib>

#include <deque>

#include "Coordonnee.h"
#include "FeuFolet.h"

using namespace std;

int main ( int argc, char** argv )
{
    /// [1] Démarrage
    // [1.1] Démarrages SDL
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
    {
        cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << endl;
        return 1;
    }

    // [1.2] Préparation de fermeture
    atexit(SDL_Quit);

    // [1.3] Para-fenêtre
    SDL_WM_SetCaption("Application SDL", 0);

    /// [2] Préparation des composants
    // [2.1] Préparation de la fenêtre
    int largeur(640);
    int hauteur(480);
    SDL_Surface* screen = SDL_SetVideoMode(largeur, hauteur, 32,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        cout << "Unable to set 640x480 video: " << SDL_GetError() << endl;
        return 1;
    }

    // [2.2] Préparation
    srand(time(0));
    Uint32 tempsPrecedent(0);

    int power(5);
    int graviteY(1);
    int nbPop(5);
    int rayon(12);
    Uint32 couleur(0);
    Uint8 rouge(0);
    Uint8 vert(0);
    Uint8 bleu(0);

    DoubleAxe saved;
    DoubleAxe souris;
    initialiserDbAxes(&saved);
    initialiserDbAxes(&souris);

    deque<DoubleAxe> precedent;
    deque<DoubleAxe> maintenant;
    deque<Uint32> couleurBille;

    SDL_ShowCursor(SDL_DISABLE);
    /// [3] Boucle principale
    bool done = false;
    while (!done)
    {
        // [3.1] Gestion évènements
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = true;
                break;
            case SDL_KEYDOWN:
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            case SDL_MOUSEMOTION:
                souris.x = event.motion.x;
                souris.y = event.motion.y;
                vert = souris.x*255/largeur;
                rouge = souris.y*255/hauteur;
                if ((int)vert+rouge > 255)
                    bleu = 0;
                else
                    bleu = 255-vert-rouge;

                couleur=SDL_MapRGB(screen->format,rouge,vert,bleu);
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs
        //Ajout des nouveaux feux
        for (int i(0); i<nbPop; i++)
        {
            //Positions
            maintenant.push_back(souris);//Position souris
            saved.x=souris.x+(power-rand()%(power*2+1));
            saved.y=souris.y/*+(power-rand()%(power*2+1))*/+power;//+power rajouté
            precedent.push_back(saved);//Orientation

            //Couleur
            couleurBille.push_back(couleur);
        }

        //Deplacement
        for (Uint32 i(0); i<maintenant.size(); i++)
        {
            saved=maintenant[i];
            maintenant[i].x+=maintenant[i].x-precedent[i].x;
            maintenant[i].y+=maintenant[i].y-precedent[i].y+graviteY;
            precedent[i]=saved;
        }
        //nettoyer
        while((maintenant.front().x>largeur || maintenant.front().x<0
              || maintenant.front().y>hauteur || maintenant.front().y<0)
              && !maintenant.empty())
        {
            maintenant.pop_front();
            precedent.pop_front();
            couleurBille.pop_front();
        }

        // [3.3] Dessin des composants
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

        if (!maintenant.empty())
            for (Uint32 i(0); i<maintenant.size(); i++)
                blitFeuFollet(screen, maintenant[i].x, maintenant[i].y, rayon, couleurBille[i], 128);

        SDL_Flip(screen);

        // [3.4] Gestion du temps
        if (SDL_GetTicks()-tempsPrecedent > 40)
            cout << "Temps de calcul trop long ! Cadence dépassée de "
            <<SDL_GetTicks()-tempsPrecedent-40
            <<" mili secondes." << endl;
        while (40>SDL_GetTicks()-tempsPrecedent)
        {
            //ça bloque pendant 30 ms moins le temps de calcul
        }
        tempsPrecedent=SDL_GetTicks();
    } //fin bcl principale

    ///[4] Destruction des composants
    SDL_FreeSurface(screen);

    cout << "Aucune erreur détectée." << endl;
    return 0;
}