#include "menuFire.h"
bool optionsFire::continuer=true;
unsigned int optionsFire::difficulte=0;
bool optionsFire::pleinEcran=false;
SDL_Surface* optionsFire::level=0;

int menuFire()
{
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    // Lorsque le programme ferme, la SDL se ferme automatiquement.
    atexit(SDL_Quit);

    // create a new window
    SDL_WM_SetIcon(SDL_LoadBMP("Images/Icone Prog D.bmp"),NULL);//Icone en haut � droite de la fen�tre.
    SDL_Surface* ecran = SDL_SetVideoMode(640, 480, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);//Cr�ation de la fen�tre
    SDL_WM_SetCaption("Fire !!!",NULL);//Nom de la fen�tre
    if ( !ecran )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    int quit(0);
    SDL_ShowCursor(SDL_ENABLE);
    //EVENT LOOP***********************************
    SDL_Surface* bmp = SDL_LoadBMP("Images/Hiver.bmp");
    SDL_Surface* bmpDeux = SDL_LoadBMP("Images/Collines.bmp");
    SDL_Surface* bmpTrois = SDL_LoadBMP("Images/Soleil.bmp");
    SDL_Surface* bmpSelect = bmp;//Choix par defaut
    int choice(0);
    SDL_Surface* indication = SDL_LoadBMP("Images/MenuFire.bmp");
    SDL_SetColorKey(indication,SDL_SRCCOLORKEY,SDL_MapRGB(indication->format,255,255,255));
    if (!bmp)//Recherche d'erreurs
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    // Coordonn�es des surfaces
    SDL_Rect position;  SDL_Rect positionIndication;
    position.x = 0;     positionIndication.x = 160;
    position.y = 0;     positionIndication.y = 120;

    bool done = false;
    while (!done)
    {
        // message processing loop
        SDL_Event event;
        SDL_WaitEvent(&event);
            // check for messages
        switch (event.type)
        {
            // exit if the window is closed
        case SDL_QUIT:
            optionsFire::continuer=false;
            done = true;
            quit = 2;//Signifie que le joueur quitte pour de bon.
            break;

            // check for keypresses
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
                case SDLK_ESCAPE://Touche Echape
                    done = true;
                    break;
                case SDLK_LEFT://Petit ecran
                    optionsFire::pleinEcran=false;
                    break;
                case SDLK_RIGHT://Plein ecran
                    optionsFire::pleinEcran=true;
                    break;
                case SDLK_UP://Monte le niveau de difficult�
                    if (choice<2)
                    {
                        choice++;
                    }

                    break;
                case SDLK_DOWN://Baisse le niveau de difficulte
                    if (choice>0)
                    {
                        choice--;
                    }

                    break;
            }
            break;
        } // end switch
        // CALCULING STARTS HERE
        switch(choice)
        {
            case 0:
                bmpSelect=bmp;
                break;
            case 1:
                bmpSelect=bmpDeux;
                break;
            case 2:
                bmpSelect=bmpTrois;
                break;
        }
        // DRAWING STARTS HERE

        // clear screen
        SDL_FillRect(ecran, 0, SDL_MapRGB(ecran->format, 0, 0, 0));

        // draw bitmap
        SDL_BlitSurface(bmpSelect, 0, ecran, &position);
        SDL_BlitSurface(indication, 0, ecran, &positionIndication);

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(ecran);
    } // end main loop
    //Attribution des options
    optionsFire::level=bmpSelect;
    optionsFire::difficulte=choice;

    // free loaded bitmap
    if (!(bmpSelect==bmp))
    {
        SDL_FreeSurface(bmp);
    }
    else if(!(bmpSelect==bmpDeux))
    {
        SDL_FreeSurface(bmpDeux);
    }
    else if (!(bmpSelect==bmpTrois))
    {
        SDL_FreeSurface(bmpTrois);
    }
    SDL_FreeSurface(indication);
    //END EVENT LOOP*******************************

    // all is well ;)
    printf("Exited cleanly\n");

    return quit;
}