#include "start.h"
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "clase.h"
#include "functions.h"
#include "initializare.h"
#include "instructiuni.h"
#include <cstdlib>
#include <fstream>

using namespace std;

char nimerit[22]=" Ai prins    cuburi,";
char nume[22]=" NUME";
int dimnume;

void incepe()
{
    MISCARE=2;
    start.coordonate(1.25*250, 1.25*275);
    int m5[20]={5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100};
    int m10[10]={10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
    int m7[10]={7, 14, 21, 28, 34, 42, 49, 56, 70};
    _fantoma aux;
    int nrf=0;
    int prins=0;
    int pause=0;
    int nr_power=0;
    int nr_cooldown=0;
    int i;

    int nr_invincible;

    int copie_prins;
    catch_me.seteaza();

    while (2)
    {
        wallpaper.deseneaza();
        start.deseneaza();
        start.miscare();

        while (SDL_PollEvent(&x_out))
            if (x_out.type==SDL_QUIT)
            {
                close_sdl();
                exit(EXIT_SUCCESS);
            }

        Uint8 *keystates = SDL_GetKeyState( NULL );
        if (keystates[SDLK_LALT] && keystates[SDLK_F4]) {close_sdl(); exit(EXIT_SUCCESS);}

        if (keystates[SDLK_a]) {nume[dimnume++]='A';SDL_Delay(175);}
        if (keystates[SDLK_b]) {nume[dimnume++]='B';SDL_Delay(175);}
        if (keystates[SDLK_c]) {nume[dimnume++]='C';SDL_Delay(175);}
        if (keystates[SDLK_d]) {nume[dimnume++]='D';SDL_Delay(175);}
        if (keystates[SDLK_e]) {nume[dimnume++]='E';SDL_Delay(175);}
        if (keystates[SDLK_f]) {nume[dimnume++]='F';SDL_Delay(175);}
        if (keystates[SDLK_g]) {nume[dimnume++]='G';SDL_Delay(175);}
        if (keystates[SDLK_h]) {nume[dimnume++]='H';SDL_Delay(175);}
        if (keystates[SDLK_i]) {nume[dimnume++]='I';SDL_Delay(175);}
        if (keystates[SDLK_j]) {nume[dimnume++]='J';SDL_Delay(175);}
        if (keystates[SDLK_k]) {nume[dimnume++]='K';SDL_Delay(175);}
        if (keystates[SDLK_l]) {nume[dimnume++]='L';SDL_Delay(175);}
        if (keystates[SDLK_m]) {nume[dimnume++]='M';SDL_Delay(175);}
        if (keystates[SDLK_n]) {nume[dimnume++]='N';SDL_Delay(175);}
        if (keystates[SDLK_o]) {nume[dimnume++]='O';SDL_Delay(175);}
        if (keystates[SDLK_p]) {nume[dimnume++]='P';SDL_Delay(175);}
        if (keystates[SDLK_q]) {nume[dimnume++]='Q';SDL_Delay(175);}
        if (keystates[SDLK_r]) {nume[dimnume++]='R';SDL_Delay(175);}
        if (keystates[SDLK_s]) {nume[dimnume++]='S';SDL_Delay(175);}
        if (keystates[SDLK_t]) {nume[dimnume++]='T';SDL_Delay(175);}
        if (keystates[SDLK_u]) {nume[dimnume++]='U';SDL_Delay(175);}
        if (keystates[SDLK_w]) {nume[dimnume++]='W';SDL_Delay(175);}
        if (keystates[SDLK_y]) {nume[dimnume++]='Y';SDL_Delay(175);}
        if (keystates[SDLK_z]) {nume[dimnume++]='Z';SDL_Delay(175);}

        if (keystates[SDLK_BACKSPACE]) {nume[dimnume]=0; dimnume--; nume[dimnume]=0; SDL_Delay(175);}
        if (start.click()) {nume[dimnume]=0;break;}
        if (dimnume<0) dimnume=0;
        nume[dimnume]='_';

        message = TTF_RenderText_Solid( font, nume, textColor );
        blit_surface(message, screen, 350, 260, NULL);

        SDL_Flip(screen);

    }
    MISCARE=2;

    while (2)
    {
        joc.deseneaza();
        player.deseneaza();
        if (!pause)
            player.miscare();
        catch_me.deseneaza();

        if (!pause)
        {pauza.deseneaza();
        pauza.miscare();}
        if (pauza.click() && pause==0) pause=1;


        if (catch_me.got_me())
        {
            prins++;
            catch_me.seteaza();
            fantoma[nrf].seteaza();
            nrf++;
        }



        for (i=0; i<nrf; i++)
        {
            if (!pause)
                fantoma[i].miscare();
            fantoma[i].deseneaza();
            if (fantoma[i].mori())
            {
                //verific daca scorul e mai mare
                    if ((int)(high[0]-'0')*10+high[1]-'0' < (int)(nimerit[10]-'0')* 10+nimerit[11]-'0')
                    {
                        high[0]=nimerit[10];
                        high[1]=nimerit[11];
                        {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}
                    }
                    else {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}


                    //
                while (2)
                {
                    gameover.deseneaza();
                    meniu.deseneaza();
                    meniu.miscare();
                    restart.deseneaza();
                    restart.miscare();
                    message = TTF_RenderText_Solid( font, nimerit, textColor );
                    blit_surface(message, screen, 350, 260, NULL);
                    message =TTF_RenderText_Solid( font, nume, textColor );
                    blit_surface(message, screen, 660, 260, NULL);
                    //blit_surface(message, screen, 350, 260, NULL);


                    while (SDL_PollEvent(&x_out))
                    if (x_out.type==SDL_QUIT)
                    {
                        close_sdl();
                        exit(EXIT_SUCCESS);
                        {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}
                    }

                    if (restart.click()) {incepe(); {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}}

                    Uint8 *keystates = SDL_GetKeyState( NULL );
                    if (keystates[SDLK_LALT] && keystates[SDLK_F4]) {close_sdl(); exit(EXIT_SUCCESS); {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}}
                    if (meniu.click()) { {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();} SDL_Delay(200); return;}

                    SDL_Flip(screen);
                }
            }
        }

        while (SDL_PollEvent(&x_out))
            if (x_out.type==SDL_QUIT)
            {
                close_sdl();
                exit(EXIT_SUCCESS);
                {ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}
            }

        Uint8 *keystates = SDL_GetKeyState( NULL );
        if (keystates[SDLK_LALT] && keystates[SDLK_F4]) {{ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}close_sdl(); exit(EXIT_SUCCESS);}

        for (i=0; i<20; i++)
            if (prins==m5[i] && m5[i]!=0)
            {
                power_up[nr_power].seteaza();
                nr_power++;
                m5[i]=0;
                break;
            }

        for (i=0; i<nr_power; i++)
        {
            if (!pause)
                power_up[i].miscare();
            power_up[i].deseneaza();

            if (power_up[i].mori())
            {
                nrf-=4;
                nr_power--;
                aux=power_up[i];
                power_up[i]=power_up[nr_power];
                power_up[nr_power]=aux;
            }
        }

        for (i=0; i<10; i++)
            if (prins==m10[i] && m10[i]!=0)
            {
                cooldown[nr_cooldown].seteaza();
                nr_cooldown++;
                m10[i]=0;
                break;
            }

        for (i=0; i<nr_cooldown; i++)
        {
            if (!pause)
                cooldown[i].miscare();
            cooldown[i].deseneaza();

            if (cooldown[i].mori())
            {
                copie_prins=prins;
                MISCARE=1;
                nr_cooldown--;
                aux=cooldown[i];
                cooldown[i]=cooldown[nr_cooldown];
                cooldown[nr_cooldown]=aux;
            }
        }//tine cat sa prin 5 cuburi

        if (prins-copie_prins==5)
            MISCARE=2;


        if (pause==1)
        {
            meniu.deseneaza();
            meniu.miscare();
            if (meniu.click()) {{ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}SDL_Delay(200); return;}

            iesire.deseneaza();
            iesire.miscare();
            if (iesire.click()) {{ofstream fout("resources/highscore.mal"); fout <<high<<'\n'; fout.close();}close_sdl(); exit(EXIT_SUCCESS);}


            inapoi.deseneaza();
            inapoi.miscare();
            if (inapoi.click()) pause=0;
        }
        nimerit[10]=prins/10+'0';
        nimerit[11]=prins%10+'0';

        message = TTF_RenderText_Solid( font, nimerit, textColor );
        blit_surface(message, screen, 400, 10, NULL);
        message =TTF_RenderText_Solid( font, nume, textColor );
        blit_surface(message, screen, 710, 10, NULL);


        SDL_Flip(screen);
    }
}
