#include "SDL/SDL.h"
#include "constants.h"
#include "globals.h"
#include "functions.h"
#include "text.h"
#include "menu.h"
#include "paddle.h"
#include "ball.h"

#ifdef PSP
    #include <pspkernel.h>
    extern "C"
    {
        int main (int argc, char *argv[]);
    }
#endif

enum {ONEPLAYER, TWOPLAYER, LATERAL, QUIT};

void play(int numPlayers, bool lateral);

int main (int argc, char** argv)
{
    init();

    Text titleText("Pong", titleFont);
	SDL_Surface *underlineImage = load_image("underline.png");
    SDL_Rect underlineOffset;
    underlineOffset.x = SCREENOFFSET;
    underlineOffset.y = SCREENHEIGHT / 2 - 40;

	Menu mainMenu(SCREENHEIGHT / 2 - 10);
	mainMenu.add("One Player");
	mainMenu.add("Two Player");
	mainMenu.add("Lateral Movement: Off");
	mainMenu.add("Quit");

	bool lateral = false;
    bool done = false;
    while (!done)
    {
        while (SDL_PollEvent(&event))
        {
            #ifndef PSP
                if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE))
                    done = true;
            #endif
            switch (mainMenu.input())
            {
                case ONEPLAYER: play(1, lateral); break;
                case TWOPLAYER: play(2, lateral); break;
                case LATERAL:
                {
                    if (lateral)
                        mainMenu.change(LATERAL, "Lateral Movement: Off");
                    else
                        mainMenu.change(LATERAL, "Lateral Movement: On");
                    lateral = !lateral;
                    break;
                }
                case QUIT: done=true; break;
            }
        }
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));

        titleText.draw(SCREENWIDTH / 2, SCREENHEIGHT / 2 - 65, true, false);
        SDL_BlitSurface(underlineImage, NULL, screen, &underlineOffset);
        mainMenu.draw();

        SDL_Flip(screen);
    }
    #ifdef PSP
        sceKernelExitGame();
    #endif
    return 0;
}

void play(int numPlayers, bool lateral)
{
    Paddle player1(1, numPlayers, lateral);
    Paddle player2(2, numPlayers - 1, lateral);

    Ball ball;

    int startTicks = SDL_GetTicks();
    bool done = false;
    while (!done)
    {
        if (player1.checkWin() || player2.checkWin())
            return;

        while (SDL_PollEvent(&event))
        {
            player1.input();
            player2.input();
            switch (event.type)
            {
                #ifndef PSP
                    case SDL_QUIT: done = true; break;
                    case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) done = true; break;
                #else
                    case SDL_JOYBUTTONDOWN: if (event.jbutton.button == START) done = true; break;
                #endif
            }
        }
        player2.AI(ball.getY());

        player1.move(SDL_GetTicks() - startTicks);
        player2.move(SDL_GetTicks() - startTicks);
        switch (ball.move(player1, player2, SDL_GetTicks() - startTicks))
        {
            case 1: player1.incrementScore(); break;
            case 2: player2.incrementScore(); break;
        }
        startTicks = SDL_GetTicks();

        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));

        player1.draw();
        player2.draw();
        ball.draw();

        SDL_Flip(screen);
    }
}
