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

#include "Sounderer.h"

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

    // [1.2] Préparation de fermeture
    atexit(SDL_Quit);
    bool done = false;

    // [1.3] Para-fenêtre
    SDL_WM_SetCaption("Tests du son", 0);

    /// [2] Préparation des composants
    // [2.1] Préparation de la fenêtre
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        std::cout << "Impossible de crée la fenêtre : " << SDL_GetError() << std::endl;
        return 1;
    }

    // [2.2] Préparation du son
    Sounderer soundPlayer("Sons/");
    if (soundPlayer.init() == false) done = true;
    else
    {
        soundPlayer.preLoad("growl1", EXT_OGG);
        soundPlayer.preLoad("breathe1", EXT_OGG);
        soundPlayer.preLoad("fire", EXT_OGG);
        soundPlayer.preLoad("metal1", EXT_OGG);
    }

    // [2.3] Mesures de performance
    /*Uint32 tempsDepart, difference, moyenne, nbTests(100000);
    tempsDepart = SDL_GetTicks();
    for (unsigned int i(0); i<nbTests; i++)
    {
        //quelque chose à mesurer
    }
    difference = SDL_GetTicks() - tempsDepart;
    moyenne = difference / nbTests;
    std::cout << "L'appel de play() a duré " <<difference<<" / "<<nbTests<<" soit "<<moyenne<<"ms en moyenne."<<std::endl;*/

    /// [3] Boucle principale
    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:
                switch (event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    done = true;
                    break;
                case SDLK_b:
                    soundPlayer.play("breathe1");
                    break;
                case SDLK_e:
                    soundPlayer.fadePlay("effect", 3692, INFINITY_LOOP);
                    break;
                case SDLK_f:
                    soundPlayer.play("fire");
                    break;
                case SDLK_g:
                    soundPlayer.play("growl1");
                    break;
                case SDLK_h:
                    soundPlayer.play("hou", 2);
                    break;
                default:
                    soundPlayer.play("metal1", INFINITY_LOOP);
                    break;
                }
                break;
            case SDL_KEYUP:
                switch (event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    done = true;
                    break;
                case SDLK_b:
                    soundPlayer.stop("breathe1");
                    break;
                case SDLK_e:
                    soundPlayer.stop("effect");
                    break;
                case SDLK_f:
                    soundPlayer.stop("fire");
                    break;
                case SDLK_g:
                    soundPlayer.stop("growl1");
                    break;
                case SDLK_h:
                    soundPlayer.stop("hou");
                    break;
                default:
                    soundPlayer.stop("metal1");
                    break;
                }
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs


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

        //

        SDL_Flip(screen);
    } //fin bcl principale

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


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