#include "SDL\SDL.h"
#include "SDL/SDL_image.h"
#include <cstring>
#include <fstream>

using namespace std;
ofstream fout("nivele.out");

void init_sdl(bool is_fullscreen);
void close_sdl();
void citire();
SDL_Surface* screen;
SDL_Surface* img, *img2, *img3, *img4, *img5;
SDL_Surface *incarca_imaginea( std::string filename );
SDL_Rect patrat, obstacol[21], iesire;

int n, nivel = 0;

int main(int argc,char *args[])
{
    meniu:
    nivel++;
    if (nivel == 10) nivel = 1;
    init_sdl(0);
    SDL_Event evenim;
    img=incarca_imaginea("meniu.jpg");
    img2=incarca_imaginea("help.jpg");
    img3=incarca_imaginea("joker.jpg");
    img4=incarca_imaginea("you-win.jpg");
    img5=incarca_imaginea("you-lost.jpg");

    citire();

    bool directie[4] = {0,0,0,0};
    int i, primul = 0, pe_obs;
    Uint32 color = SDL_MapRGB(screen->format, 224,81,70);
    Uint32 color1 = SDL_MapRGB(screen->format, 156,131,139);
    Uint32 color2 = SDL_MapRGB(screen->format, 233,242,48);

    while(1){
        SDL_BlitSurface(img, NULL, screen, NULL);
        while(SDL_PollEvent(&evenim)){
            if (evenim.type == SDL_QUIT || evenim.key.keysym.sym==SDLK_ESCAPE){
                close_sdl();
                return 0;
                }
            if (evenim.type == SDL_KEYDOWN){
                if(evenim.key.keysym.sym == SDLK_p)
                        SDL_FillRect(img,NULL,0x000000);
                if(evenim.key.keysym.sym == SDLK_h)
                        SDL_FillRect(img,NULL,0x000000);
                if(evenim.key.keysym.sym == SDLK_e){
                close_sdl();
                return 0;
                }
                if(evenim.key.keysym.sym == SDLK_ESCAPE){
                close_sdl();
                return 0;
                }
            }
            if (evenim.type == SDL_KEYUP){
                if(evenim.key.keysym.sym == SDLK_p)
                        while(1){
                            SDL_BlitSurface(img3, NULL, screen, NULL);

                            for (i = 1; i <= n; i++)
                                SDL_FillRect(screen,&obstacol[i],color1);

                            SDL_FillRect(screen,&patrat,color2);
                            SDL_FillRect(screen,&iesire,color);
                            SDL_Flip(screen);
                            while(SDL_PollEvent(&evenim)){
                                if (evenim.type == SDL_QUIT || evenim.key.keysym.sym==SDLK_ESCAPE){
                                    close_sdl();
                                    return 0;
                                }
                                if(evenim.key.keysym.sym==SDLK_BACKSPACE){
                                    goto meniu;
                                }
                                if(evenim.type==SDL_KEYDOWN)
                                {
                                    if (primul == 0)
                                        {
                                            primul = 1;
                                            if(evenim.key.keysym.sym==SDLK_UP) {directie[0] = 1; directie[1] = 0; directie[2] = 0; directie[3] = 0;}
                                            else
                                                if(evenim.key.keysym.sym==SDLK_LEFT)
                                                    {directie[0] = 0; directie[1] = 1; directie[2] = 0; directie[3] = 0;}
                                                else
                                                    if(evenim.key.keysym.sym==SDLK_DOWN)
                                                        {directie[0] = 0; directie[1] = 0; directie[2] = 1; directie[3] = 0;}
                                                    else
                                                        if(evenim.key.keysym.sym==SDLK_RIGHT)
                                                            {directie[0] = 0; directie[1] = 0; directie[2] = 0; directie[3] = 1;}
                                        }
                                   else
                                    {
                                        pe_obs = 0;
                                        for (i = 1; i <= n; i++)
                                        {
                                            if (patrat.x == obstacol[i].x && patrat.y == obstacol[i].y)
                                                pe_obs = 1;
                                        }
                                        if (pe_obs)
                                            if(evenim.key.keysym.sym==SDLK_UP) {directie[0] = 1; directie[1] = 0; directie[2] = 0; directie[3] = 0;}
                                            else
                                                if(evenim.key.keysym.sym==SDLK_LEFT)
                                                    {directie[0] = 0; directie[1] = 1; directie[2] = 0; directie[3] = 0;}
                                                else
                                                    if(evenim.key.keysym.sym==SDLK_DOWN)
                                                        {directie[0] = 0; directie[1] = 0; directie[2] = 1; directie[3] = 0;}
                                                    else
                                                        if(evenim.key.keysym.sym==SDLK_RIGHT)
                                                            {directie[0] = 0; directie[1] = 0; directie[2] = 0; directie[3] = 1;}
                                    }
                                }
                            }
                            if(directie[0])patrat.y--;
                            if(directie[1])patrat.x--;
                            if(directie[2])patrat.y++;
                            if(directie[3])patrat.x++;
                            if(patrat.y==obstacol[1].y&& patrat.x==obstacol[1].x){
                                    directie[2] = 0;
                            }
                                for (i = 1; i <= n; i++)
                                    if(patrat.y==obstacol[i].y && patrat.x==obstacol[i].x){
                                        directie[0]=0;
                                        directie[1] = 0;
                                        directie[2] = 0;
                                        directie[3] = 0;
                                    }

                                if(patrat.y==iesire.y && patrat.x==iesire.x){
                                    directie[0]=0;
                                    directie[1] = 0;
                                    directie[2] = 0;
                                    directie[3] = 0;
                                    while(1){
                                        SDL_BlitSurface(img4, NULL, screen, NULL);
                                        SDL_Flip(screen);
                                        while(SDL_PollEvent(&evenim)){
                                            if (evenim.type == SDL_QUIT || evenim.key.keysym.sym==SDLK_ESCAPE){
                                                close_sdl();
                                                return 0;
                                            }
                                            if(evenim.key.keysym.sym==SDLK_BACKSPACE){
                                                goto meniu;
                                            }
                                        }
                                    }

                                }
                                if(patrat.x==775 || patrat.y==575 || patrat.x==0 || patrat.y==0)
                                {
                                    directie[0]=0;
                                    directie[1] = 0;
                                    directie[2] = 0;
                                    directie[3] = 0;
                                    while(1){
                                        SDL_BlitSurface(img5, NULL, screen, NULL);
                                        SDL_Flip(screen);
                                        while(SDL_PollEvent(&evenim)){
                                            if (evenim.type == SDL_QUIT || evenim.key.keysym.sym==SDLK_ESCAPE){
                                                close_sdl();
                                                return 0;
                                            }
                                            if(evenim.key.keysym.sym==SDLK_BACKSPACE){
                                                nivel--;
                                                goto meniu;
                                            }
                                        }
                                    }
                                }




                        }
                if(evenim.key.keysym.sym == SDLK_h)
						while(1){
							SDL_BlitSurface(img2, NULL, screen, NULL);
							SDL_Flip(screen);
							while(SDL_PollEvent(&evenim)){
								 if (evenim.type == SDL_QUIT || evenim.key.keysym.sym==SDLK_ESCAPE){
                                    close_sdl();
                                    return 0;
									}
                                if(evenim.key.keysym.sym==SDLK_BACKSPACE){
                                    goto meniu;
                                }

							}

						}
                if(evenim.key.keysym.sym == SDLK_e){
                close_sdl();
                return 0;
                }
                if(evenim.key.keysym.sym == SDLK_ESCAPE){
                close_sdl();
                return 0;
                }
            }

        }
        SDL_Flip(screen);
        SDL_Delay(10);
    }

    close_sdl();

    return 0;
}
void init_sdl(bool is_fullscreen){
    SDL_Init(SDL_INIT_EVERYTHING);
    if(is_fullscreen==0){
        screen=SDL_SetVideoMode(800,600,32,0);
    }
    else{
        screen=SDL_SetVideoMode(800,600,32,SDL_FULLSCREEN);
    }
}

void close_sdl(){
    SDL_Quit();
}

SDL_Surface *incarca_imaginea( std::string filename )
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = IMG_Load(filename.c_str());
    if(loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        SDL_FreeSurface( loadedImage );
    }
    return optimizedImage;
}

void citire()
{
    int nro;
    char fisier[10]="nivel0.in";
    fisier[5]=(char)(48 + nivel);
    ifstream fin(fisier);
    fin>>n;   //numar obstacole
    fin>>patrat.x>>patrat.y>>patrat.w>>patrat.h;
    for (nro = 1; nro <= n; nro++)
    {
        fin>>obstacol[nro].x;
        fin>>obstacol[nro].y;
        fin>>obstacol[nro].w;
        fin>>obstacol[nro].h;
    }
    fin>>iesire.x>>iesire.y>>iesire.w>>iesire.h;
    fin.close();
}
