#include "paddle.h"
#include "ansi.h"
#include <iostream>
#include "user.h"

using namespace std;

//constructor
Paddle::Paddle(int player) {
 //set up the starting position
 if(player == 1) {
   //left hand player
   x = 1;
 } else {
   //right hand player
   x = 80;
 }

 //TODO: remove this!
 cout << clearScreen << cursorOff;

 //start in the middle
 y=12;   //midpoint of the screen

}


//movement functions
void
Paddle::moveUp() {
 if(y==3)
   return;
 y--;
}


void
Paddle::moveDown() {
 if(y==22)
   return;
 y++;
}


//collision detection
bool
Paddle::checkCollision(Ball b) {
 //TODO: Check Collision
}


//screen commands
void
Paddle::draw() {
 //set the background color
 cout << whiteBackground;

 //loop over every spot on the paddle
 for(int sy = y - PADDLE_HEIGHT/2; sy < y+PADDLE_HEIGHT/2; sy++) {
   cout << cursorPosition(x, sy) << ' ';
 }


 //set it  back
 cout << normal;
 cout.flush();
}


void
Paddle::erase() {
 //erase with normal
 cout << normal;

 //loop over every spot on the paddle
 for(int sy = y - PADDLE_HEIGHT/2; sy < y+PADDLE_HEIGHT/2; sy++) {
   cout << cursorPosition(x, sy) << ' ';
 }
 cout.flush();
}


void
Paddle::update() {
 //TODO: Update
}

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

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

//////////////////////////////////////////////////
// Player Paddle
//////////////////////////////////////////////////
PlayerPaddle::PlayerPaddle(int player) : Paddle(player) {

}

void
PlayerPaddle::update() {
 char c;

 //no can move if not has input
 if(!hasInput())
   return;

 //get the key press
 c = cin.get();

 //process the keypress
 switch(c) {
 case 'k':
 case 'w':
   moveUp();
   break;

 case 'j':
 case 's':
   moveDown();
   break;
 }

 //flush the input buffer
 clearStdin();
}




////////////////////////////////////////////////
// computer paddle
///////////////////////////////////////////////
ComputerPaddle::ComputerPaddle(int player) : Paddle(player) {

}

void
ComputerPaddle::update(int ballY) {
 if(ballY > getY())
   moveDown();
 if(ballY < getY())
   moveUp();

}