#include "paddle.h"

Paddle::Paddle(int PaddleplayerNum, bool Paddlehuman, bool Paddlelateral)
{
	playerNum = PaddleplayerNum;
	human = Paddlehuman;
	lateral = Paddlelateral;

	switch (playerNum)
	{
        case 1:
        {
            paddleImage = load_image("paddle1.png");
            x = SCREENOFFSET;
            break;
        }
        case 2:
        {
            paddleImage = load_image("paddle2.png");
            x = SCREENWIDTH - SCREENOFFSET - paddleImage->w;
            break;
        }
	}
	y = SCREENHEIGHT / 2 - paddleImage->h / 2;
	xVel = 0;
	yVel = 0;

	score = -1;
	incrementScore();
}

int Paddle::getX() const
{
	return (int)x;
}

int Paddle::getY() const
{
	return (int)y;
}

int Paddle::getW() const
{
    return paddleImage->w;
}

int Paddle::getH() const
{
    return paddleImage->h;
}

void Paddle::incrementScore()
{
	++score;
	char tempscoreText[256];
	sprintf(tempscoreText, "Player %d: %d", playerNum, score);
	scoreText.change(tempscoreText);
}

void Paddle::input()
{
	if (human)
	{
	    #ifndef PSP
            if (event.type == SDL_KEYDOWN)
            {
                if (playerNum == 1)
                {
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_w: yVel -= MOVEMENT; break;
                        case SDLK_s: yVel += MOVEMENT; break;
                        case SDLK_a: if (lateral) xVel -= MOVEMENT; break;
                        case SDLK_d: if (lateral) xVel += MOVEMENT; break;
                    }
                }
                else if (playerNum == 2)
                {
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_UP:    yVel -= MOVEMENT; break;
                        case SDLK_DOWN:  yVel += MOVEMENT; break;
                        case SDLK_LEFT:  if (lateral) xVel -= MOVEMENT; break;
                        case SDLK_RIGHT: if (lateral) xVel += MOVEMENT; break;
                    }
                }
            }
            else if (event.type == SDL_KEYUP)
            {
                if (playerNum == 1)
                {
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_w: if (yVel != 0) yVel += MOVEMENT; break;
                        case SDLK_s: if (yVel != 0) yVel -= MOVEMENT; break;
                        case SDLK_a: if (xVel != 0) xVel += MOVEMENT; break;
                        case SDLK_d: if (xVel != 0) xVel -= MOVEMENT; break;
                    }
                }
                else if (playerNum == 2)
                {
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_UP:    if (yVel != 0) yVel += MOVEMENT; break;
                        case SDLK_DOWN:  if (yVel != 0) yVel -= MOVEMENT; break;
                        case SDLK_LEFT:  if (xVel != 0) xVel += MOVEMENT; break;
                        case SDLK_RIGHT: if (xVel != 0) xVel -= MOVEMENT; break;
                    }
                }
            }
        #else
            if (event.type == SDL_JOYBUTTONDOWN)
            {
                if (playerNum == 1)
                {
                    switch (event.jbutton.button)
                    {
                        case UP:    yVel -= MOVEMENT; break;
                        case DOWN:  yVel += MOVEMENT; break;
                        case LEFT:  if (lateral) xVel -= MOVEMENT; break;
                        case RIGHT: if (lateral) xVel += MOVEMENT; break;
                    }
                }
                else if (playerNum == 2)
                {
                    switch (event.jbutton.button)
                    {
                        case TRIANGLE: yVel -= MOVEMENT; break;
                        case CROSS:    yVel += MOVEMENT; break;
                        case SQUARE:   if (lateral) xVel -= MOVEMENT; break;
                        case CIRCLE:   if (lateral) xVel += MOVEMENT; break;
                    }
                }
            }
            else if (event.type == SDL_JOYBUTTONUP)
            {
                if (playerNum == 1)
                {
                    switch (event.jbutton.button)
                    {
                        case UP:    if (yVel != 0) yVel += MOVEMENT; break;
                        case DOWN:  if (yVel != 0) yVel -= MOVEMENT; break;
                        case LEFT:  if (xVel != 0) xVel += MOVEMENT; break;
                        case RIGHT: if (xVel != 0) xVel -= MOVEMENT; break;
                    }
                }
                else if (playerNum == 2)
                {
                    switch (event.jbutton.button)
                    {
                        case TRIANGLE: if (yVel != 0) yVel += MOVEMENT; break;
                        case CROSS:    if (yVel != 0) yVel -= MOVEMENT; break;
                        case SQUARE:   if (xVel != 0) xVel += MOVEMENT; break;
                        case CIRCLE:   if (xVel != 0) xVel -= MOVEMENT; break;
                    }
                }
            }
        #endif
	}
}

void Paddle::AI(int bally)
{
    if (!human)
	{
	    if (bally >= y + paddleImage->h / 2 - 20 && bally + 20 <= y + paddleImage->h / 2 + 20)
            yVel = 0;
		else if (bally + 10 < y + paddleImage->h / 2)
			yVel = -1 * MOVEMENT;
		else if (bally + 10 > y + paddleImage->h / 2)
			yVel = MOVEMENT;
	}
}

void Paddle::move(Uint32 deltaTicks)
{
    x += xVel * (deltaTicks / 1000.f);
    if (playerNum == 1)
    {
        if (x < SCREENOFFSET)
            x = SCREENOFFSET;
        else if (x + paddleImage->w > SCREENWIDTH / 2 - SCREENOFFSET)
            x = SCREENWIDTH / 2 - SCREENOFFSET - paddleImage->w;
    }
    if (playerNum == 2)
    {
        if (x < SCREENWIDTH / 2 + SCREENOFFSET)
            x = SCREENWIDTH / 2 + SCREENOFFSET;
        else if (x + paddleImage->w > SCREENWIDTH - SCREENOFFSET)
            x = SCREENWIDTH - SCREENOFFSET - paddleImage->w;
    }

    y += yVel * (deltaTicks / 1000.f);
    if (y < SCREENOFFSET)
        y = SCREENOFFSET;
    else if (y + paddleImage->h > SCREENHEIGHT - SCREENOFFSET)
        y = SCREENHEIGHT - SCREENOFFSET - paddleImage->h;
}

bool Paddle::checkWin() const
{
    if (score == WINNINGSCORE)
    {
        char tempwinText[256];
        sprintf(tempwinText, "Player %d Wins!", playerNum);
        Text winText(tempwinText);

        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));
        winText.draw(SCREENWIDTH / 2, SCREENHEIGHT / 2, true, true);
        SDL_Flip(screen);
        SDL_Delay(2500);

        return true;
    }
    return false;
}

void Paddle::draw() const
{
    SDL_Rect offset;
    offset.x = (int)x;
    offset.y = (int)y;
    SDL_BlitSurface(paddleImage, NULL, screen, &offset);

    if (playerNum == 1)
        scoreText.draw(SCREENOFFSET, 0);
    else if (playerNum == 2)
        scoreText.draw(SCREENWIDTH -SCREENOFFSET - scoreText.getW(), 0);
}
