#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <fmod.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_rotozoom.h>
#include "fonctions.hpp"
#include "jouer.hpp"

int angle = 0;
int angle2 = 180;
int rotateAngle = 0;
int rotateAngle2 = 180;
extern TTF_Font *police;
extern SDL_Surface *ecran;

void menu()
{
    SDL_Color noir = {0,0,0};
    SDL_Rect milieu;
    SDL_Event event;
    difficult defaultDifficulte;
    button Jouer, Quitter, Option, Jeu_Libre;

    defaultDifficulte.min = 70;
    defaultDifficulte.max = 110;
    defaultDifficulte.temps = 30;
    defaultDifficulte.pxPerS = 2;
    defaultDifficulte.libre = false;

    jouerMusique("Menu");

    SDL_Surface* rectangle = SDL_CreateRGBSurface(SDL_HWSURFACE, 50, 50, 32, 0, 0, 0, 0);
    SDL_Surface* rotatedRectangle = NULL;
    color(rectangle, 255, 77, 77);
    SDL_Rect positionRectangle;

    SDL_Surface* rectangle2 = SDL_CreateRGBSurface(SDL_HWSURFACE, 50, 50, 32, 0, 0, 0, 0);
    SDL_Surface* rotatedRectangle2=NULL;
    color(rectangle2,77,255,77);
    SDL_Rect positionRectangle2;

    milieu.x = ecran->w/2;
    milieu.y = ecran->h/2;

    while (1)
    {
        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 48, 48, 48));
        SDL_PollEvent(&event);
        SDL_Delay(10);
        switch(event.type)
        {
        case SDL_MOUSEMOTION:
            if(checkZone(event.motion.x, event.motion.y, xyToRect(Jouer.x, Jouer.y), Jouer.w, Jouer.h))
            {
                Jouer = createButton(milieu.x - 50, milieu.y - 100, noir, rgbToColor(125,124,124), "Jouer");
                Quitter = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Quitter");
                Option = createButton(milieu.x - 50, milieu.y + 50,rgbToColor(207,207,207), rgbToColor(125,124,124), "Options");
                Jeu_Libre = createButton(milieu.x - 50, milieu.y,rgbToColor(207,207,207), rgbToColor(125,124,124), "Jeu Libre");

            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(Quitter.x, Quitter.y),Quitter.w,Quitter.h))
            {
                Jouer = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Jouer");
                Quitter = createButton(milieu.x - 50, milieu.y - 50, noir, rgbToColor(125,124,124), "Quitter");
                Option = createButton(milieu.x - 50, milieu.y + 50,rgbToColor(207,207,207), rgbToColor(125,124,124), "Options");
                Jeu_Libre = createButton(milieu.x - 50, milieu.y,rgbToColor(207,207,207), rgbToColor(125,124,124), "Jeu Libre");

            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(Option.x, Option.y),Option.w,Option.h))
            {
                Jouer = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Jouer");
                Quitter = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Quitter");
                Option = createButton(milieu.x - 50, milieu.y + 50,noir, rgbToColor(125,124,124), "Options");
                Jeu_Libre = createButton(milieu.x - 50, milieu.y,rgbToColor(207,207,207), rgbToColor(125,124,124), "Jeu Libre");

            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(Jeu_Libre.x, Jeu_Libre.y),Jeu_Libre.w,Jeu_Libre.h))
            {
                Jouer = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Jouer");
                Quitter = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Quitter");
                Option = createButton(milieu.x - 50, milieu.y + 50,rgbToColor(207,207,207), rgbToColor(125,124,124), "Options");
                Jeu_Libre = createButton(milieu.x - 50, milieu.y,noir, rgbToColor(125,124,124), "Jeu Libre");

            }
            else
            {
                Jouer = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Jouer");
                Quitter = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Quitter");
                Option = createButton(milieu.x - 50, milieu.y + 50,rgbToColor(207,207,207), rgbToColor(125,124,124), "Options");
                Jeu_Libre = createButton(milieu.x - 50, milieu.y,rgbToColor(207,207,207), rgbToColor(125,124,124), "Jeu Libre");

            }
            break;

        case SDL_MOUSEBUTTONUP:
            if(checkZone(event.button.x,event.button.y,xyToRect(Jouer.x, Jouer.y),Jouer.w,Jouer.h))
            {
                jouer(defaultDifficulte);
                SDL_WarpMouse(event.button.x,event.button.y+1);
                jouerMusique("Menu");
            }
            else if(checkZone(event.button.x,event.button.y,xyToRect(Quitter.x, Quitter.y),Quitter.w,Quitter.h))
                return;
            else if(checkZone(event.button.x,event.button.y,xyToRect(Option.x, Option.y),Option.w,Option.h))
            {
                SDL_WarpMouse(event.button.x,event.button.y+1);
                defaultDifficulte = option();
            }
            else if(checkZone(event.button.x,event.button.y,xyToRect(Jeu_Libre.x, Jeu_Libre.y),Jeu_Libre.w,Jeu_Libre.h))
            {
                defaultDifficulte.libre = true;
                jouer(defaultDifficulte);
                defaultDifficulte.libre = false;
                SDL_WarpMouse(event.button.x,event.button.y+1);
                jouerMusique("Menu");
            }
            Jouer = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Jouer");
            Quitter = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Quitter");
            Option = createButton(milieu.x - 50, milieu.y + 50,rgbToColor(207,207,207), rgbToColor(125,124,124), "Options");
            Jeu_Libre = createButton(milieu.x - 50, milieu.y,rgbToColor(207,207,207), rgbToColor(125,124,124), "Jeu Libre");

            break;
        case SDL_QUIT:
            exit(0);
            break;
        default:
            Jouer = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Jouer");
            Quitter = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Quitter");
            Option = createButton(milieu.x - 50, milieu.y + 50,rgbToColor(207,207,207), rgbToColor(125,124,124), "Options");
            Jeu_Libre = createButton(milieu.x - 50, milieu.y,rgbToColor(207,207,207), rgbToColor(125,124,124), "Jeu Libre");
        }

        positionRectangle = trajectoireCirculaire(300,angle);
        angle += 1;
        rotateAngle += 5;
        if(angle == 360)
            angle = 0;
        if(rotateAngle == 360)
            rotateAngle = 0;
        rotatedRectangle = rotozoomSurface(rectangle,-rotateAngle,1.0,10);
        positionRectangle.x += milieu.x - rotatedRectangle->w / 2;
        positionRectangle.y += milieu.y - rotatedRectangle->h / 2;
        delColor(rotatedRectangle,0,0,0);
        advancedBlit(rotatedRectangle, &positionRectangle);
        SDL_FreeSurface(rotatedRectangle);

        positionRectangle2 = trajectoireCirculaire(300,angle2);
        angle2 += 1;
        rotateAngle2 += 5;
        if(angle2 == 360)
            angle2 = 0;
        if(rotateAngle2 == 360)
            rotateAngle2 = 0;
        rotatedRectangle2 = rotozoomSurface(rectangle2,-rotateAngle2,1.0,10);
        positionRectangle2.x += milieu.x - rotatedRectangle2->w / 2;
        positionRectangle2.y += milieu.y - rotatedRectangle2->h / 2;
        delColor(rotatedRectangle2,0,0,0);
        advancedBlit(rotatedRectangle2, &positionRectangle2);
        SDL_FreeSurface(rotatedRectangle2);

        SDL_Flip(ecran);

    }


}

difficult option()
{
    button facile, moyen, difficile, hardcore;
    SDL_Color noir = {0,0,0};
    SDL_Rect milieu;
    int continuer = 1;
    milieu.x = ecran->w/2;
    milieu.y = ecran->h/2;
    SDL_Event event;
    facile = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Facile");
    moyen = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Moyen");
    difficile = createButton(milieu.x - 50, milieu.y , rgbToColor(207,207,207), rgbToColor(125,124,124), "Difficile");
    hardcore = createButton(milieu.x - 50, milieu.y + 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Hardcore");
    difficult FMD;

    SDL_Surface* rectangle = SDL_CreateRGBSurface(SDL_SWSURFACE, 50, 50, 32, 0, 0, 0, 0);
    SDL_Surface* rotatedRectangle=NULL;
    color(rectangle,255,77,77);
    SDL_Rect positionRectangle;

    SDL_Surface* rectangle2 = SDL_CreateRGBSurface(SDL_SWSURFACE, 50, 50, 32, 0, 0, 0, 0);
    SDL_Surface* rotatedRectangle2=NULL;
    color(rectangle2,77,255,77);
    SDL_Rect positionRectangle2;

    SDL_Flip(ecran);
    while (continuer)
    {
        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 48, 48, 48));
        SDL_PollEvent(&event);
        SDL_Delay(10);
        switch(event.type)
        {
        case SDL_MOUSEMOTION:
            if(checkZone(event.motion.x,event.motion.y,xyToRect(facile.x, facile.y),facile.w,facile.h))
            {
                facile = createButton(milieu.x - 50, milieu.y - 100, noir, rgbToColor(125,124,124), "Facile");
                moyen = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Moyen");
                difficile = createButton(milieu.x - 50, milieu.y , rgbToColor(207,207,207), rgbToColor(125,124,124), "Difficile");
                hardcore = createButton(milieu.x - 50, milieu.y + 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Hardcore");

            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(moyen.x, moyen.y),moyen.w,moyen.h))
            {
                facile = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Facile");
                moyen = createButton(milieu.x - 50, milieu.y - 50, noir, rgbToColor(125,124,124), "Moyen");
                difficile = createButton(milieu.x - 50, milieu.y , rgbToColor(207,207,207), rgbToColor(125,124,124), "Difficile");
                hardcore = createButton(milieu.x - 50, milieu.y + 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Hardcore");

            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(difficile.x, difficile.y),difficile.w,difficile.h))
            {
                facile = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Facile");
                moyen = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Moyen");
                difficile = createButton(milieu.x - 50, milieu.y, noir, rgbToColor(125,124,124), "Difficile");
                hardcore = createButton(milieu.x - 50, milieu.y + 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Hardcore");

            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(hardcore.x, hardcore.y),hardcore.w,hardcore.h))
            {
                facile = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Facile");
                moyen = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Moyen");
                difficile = createButton(milieu.x - 50, milieu.y , rgbToColor(207,207,207), rgbToColor(125,124,124), "Difficile");
                hardcore = createButton(milieu.x - 50, milieu.y + 50, noir, rgbToColor(125,124,124), "Hardcore");

            }
            else
            {
                facile = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Facile");
                moyen = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Moyen");
                difficile = createButton(milieu.x - 50, milieu.y , rgbToColor(207,207,207), rgbToColor(125,124,124), "Difficile");
                hardcore = createButton(milieu.x - 50, milieu.y + 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Hardcore");

            }
            break;
        case SDL_MOUSEBUTTONUP:
            if(checkZone(event.motion.x,event.motion.y,xyToRect(facile.x, facile.y),facile.w,facile.h))
            {
                FMD.min = 100;
                FMD.max = 140;
                FMD.temps = 45;
                FMD.pxPerS = 1;
                SDL_Flip(ecran);
                SDL_WarpMouse(event.button.x,event.button.y+1);
                return FMD;
            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(moyen.x, moyen.y),moyen.w,moyen.h))
            {
                FMD.min = 70;
                FMD.max = 110;
                FMD.temps = 30;
                FMD.pxPerS = 2;
                SDL_Flip(ecran);
                SDL_WarpMouse(event.button.x,event.button.y+1);
                return FMD;
            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(difficile.x, difficile.y),difficile.w,difficile.h))
            {
                FMD.min = 40;
                FMD.max = 70;
                FMD.temps = 15;
                FMD.pxPerS = 4;
                SDL_Flip(ecran);
                SDL_WarpMouse(event.button.x,event.button.y+1);
                return FMD;
            }
            else if(checkZone(event.motion.x,event.motion.y,xyToRect(hardcore.x, hardcore.y),hardcore.w,hardcore.h))
            {
                FMD.min = 40;
                FMD.max = 70;
                FMD.temps = 10;
                FMD.pxPerS = 8;
                SDL_Flip(ecran);
                SDL_WarpMouse(event.button.x,event.button.y+1);
                return FMD;
            }
            break;
        case SDL_QUIT:
            exit(0);
            break;
        default:
            facile = createButton(milieu.x - 50, milieu.y - 100, rgbToColor(207,207,207), rgbToColor(125,124,124), "Facile");
            moyen = createButton(milieu.x - 50, milieu.y - 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Moyen");
            difficile = createButton(milieu.x - 50, milieu.y , rgbToColor(207,207,207), rgbToColor(125,124,124), "Difficile");
            hardcore = createButton(milieu.x - 50, milieu.y + 50, rgbToColor(207,207,207), rgbToColor(125,124,124), "Hardcore");
        }

        positionRectangle = trajectoireCirculaire(300,angle);
        angle += 1;
        rotateAngle += 5;
        if(angle == 360)
            angle = 0;
        if(rotateAngle == 360)
            rotateAngle = 0;
        rotatedRectangle = rotozoomSurface(rectangle,-rotateAngle,1.0,1);
        positionRectangle.x += milieu.x - rotatedRectangle->w / 2;
        positionRectangle.y += milieu.y - rotatedRectangle->h / 2;
        delColor(rotatedRectangle,0,0,0);
        advancedBlit(rotatedRectangle, &positionRectangle);
        SDL_FreeSurface(rotatedRectangle);
        positionRectangle2 = trajectoireCirculaire(300,angle2);
        angle2 += 1;
        rotateAngle2 += 5;
        if(angle2 == 360)
            angle2 = 0;
        if(rotateAngle2 == 360)
            rotateAngle2 = 0;
        rotatedRectangle2 = rotozoomSurface(rectangle2,-rotateAngle2,1.0,1);
        positionRectangle2.x += milieu.x - rotatedRectangle2->w / 2;
        positionRectangle2.y += milieu.y - rotatedRectangle2->h / 2;
        delColor(rotatedRectangle2,0,0,0);
        advancedBlit(rotatedRectangle2, &positionRectangle2);
        SDL_FreeSurface(rotatedRectangle2);

        SDL_Flip(ecran);
    }
    return FMD;
}

void jouer(difficult defaultDifficulte)
{
    srand(time(NULL));
    jouerMusique("Jouer");

    int actualColorR = rand() % 255, actualColorV = rand() % 255,actualColorB = rand() % 255;
    int continuer = 1;
    int sizex = rand () % (defaultDifficulte.max - defaultDifficulte.min + 1) + defaultDifficulte.min;
    int sizey = rand () % (defaultDifficulte.max - defaultDifficulte.min + 1) + defaultDifficulte.min;
    int tempsRestant, tempsTotal;
    int score = 0;
    int oldTicks;
    tempsTotal = SDL_GetTicks() + defaultDifficulte.temps*1000;
    bool directionHaut = 0, directionDroite = 1;

    SDL_Surface *rectangle = NULL;
    SDL_Rect positionRectangle;
    SDL_Event event;
    SDL_Color couleurBlanche = {255,255,255,0};

    positionRectangle.x = rand() % ecran->w - sizex;
    positionRectangle.y = rand() % ecran->h - sizey;

    TTF_Font *police = TTF_OpenFont("game_over.ttf", 50);

    rectangle = SDL_CreateRGBSurface(SDL_HWSURFACE, sizex, sizey, 32, 0, 0, 0, 0);

    color(rectangle, actualColorR, actualColorV, actualColorB);

    while (continuer)
    {
        color(ecran,0,0,0);
        SDL_PollEvent(&event);
        SDL_Delay(10);
        if(defaultDifficulte.libre == false)
            tempsRestant = (tempsTotal - SDL_GetTicks())/1000;
        else
            tempsRestant = 1000;
        switch(event.type)
        {
        case SDL_MOUSEMOTION:
            if(checkZone(event.motion.x, event.motion.y, positionRectangle,sizex,sizey))
            {
                color(rectangle, (int)actualColorR + ((255 - actualColorR)/6)*5, (int)actualColorV + ((255 - actualColorV)/6)*5, (int)actualColorB + ((255 - actualColorB)/6)*5);
            }
            else
            {
                color(rectangle, actualColorR, actualColorV, actualColorB);
            }
            break;
        case SDL_MOUSEBUTTONDOWN:
            if(checkZone(event.button.x, event.button.y, positionRectangle,sizex,sizey))
            {
                SDL_FreeSurface(rectangle);
                score++;
                actualColorR = rand () % (255 - 30 + 1) + 30;
                actualColorV = rand () % (255 - 30 + 1) + 30;
                actualColorB = rand () % (255 - 30 + 1) + 30;

                sizex = rand () % (defaultDifficulte.max - defaultDifficulte.min + 1) + defaultDifficulte.min;
                sizey = rand () % (defaultDifficulte.max - defaultDifficulte.min + 1) + defaultDifficulte.min;

                positionRectangle.x = rand() % ecran->w - sizex;
                positionRectangle.y = rand() % ecran->h - sizey;

                rectangle = SDL_CreateRGBSurface(SDL_SWSURFACE, sizex, sizey, 32, 0, 0, 0, 0);
                color(rectangle, actualColorR, actualColorV, actualColorB);
                SDL_BlitSurface(rectangle,NULL, ecran, &positionRectangle);
                SDL_WarpMouse(event.button.x,event.button.y+1);

                directionDroite = rand() % 2;
                directionHaut = rand() %2;
            }
            break;
        case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_ESCAPE)
                tempsRestant = 0;
            break;
        case SDL_QUIT:
            continuer = 0;
            break;
        }


        if(positionRectangle.x + sizex >= ecran->w)
            directionDroite = 0;
        if(positionRectangle.x <= 0)
            directionDroite = 1;
        if(positionRectangle.y + sizey >= ecran->h)
            directionHaut = 1;
        if(positionRectangle.y <= 0)
            directionHaut = 0;

        if(directionDroite)
            positionRectangle.x = positionRectangle.x + defaultDifficulte.pxPerS;
        if(directionHaut)
            positionRectangle.y = positionRectangle.y - defaultDifficulte.pxPerS;
        if(!directionDroite)
            positionRectangle.x = positionRectangle.x - defaultDifficulte.pxPerS;
        if(!directionHaut)
            positionRectangle.y = positionRectangle.y + defaultDifficulte.pxPerS;

        SDL_BlitSurface(rectangle,NULL, ecran, &positionRectangle);
        if(!defaultDifficulte.libre)
            write(couleurBlanche,10,50,false, "Temps restant : %d s", tempsRestant);
        write(couleurBlanche,10,10,false, "Score : %d", score);

        afficherFps(SDL_GetTicks(),oldTicks);
        oldTicks = SDL_GetTicks();
        SDL_Flip(ecran);
        if(tempsRestant == 0)
        {
            write(couleurBlanche,(ecran->w/2)-30,(ecran->h/2)-20,false, "Score : %d", score);
            SDL_Flip(ecran);
            pause();
            continuer = 0;
        }
    }

    SDL_FreeSurface(rectangle);
    TTF_CloseFont(police);
}
