#include "cCourt.hpp"

using Equipment::cCourt;
using Equipment::cBall;
using Equipment::cPaddle;

using MathEngine::cCollision;
using MathEngine::Vector4;
using MathEngine::Vector2;

cCourt::cCourt( cBall* ball /*= nullptr*/, cPaddle* player1 /*= nullptr*/, cPaddle* player2 /*= nullptr*/, VideoEngine::cImage** image /*= nullptr*/ )
: cSprite( 0, 0, 0, 0, 640, 480, image), mp_ball(ball), m_pause(false)
{	
	m_topWall.x = 0.0f;
	m_topWall.y = 0.0f;
	m_topWall.z = 640.0f;
	m_topWall.w = 14.0f;

	m_bottomWall.x = 0.0f;
	m_bottomWall.y = 464.0f;
	m_bottomWall.z = 640.0f;
	m_bottomWall.w = 14.0f;

	m_leftOff.x = 0.0f;
	m_leftOff.y = 0.0f;
	m_leftOff.z = 480.0f;
	m_leftOff.w = 1.0f;

	m_rightOff.x = 640.0f;
	m_rightOff.y = 0.0f;
	m_rightOff.z = 480.0f;
	m_rightOff.w = 1.0f;

	m_court.x = 0.0f;
	m_court.y = 15.0f;
	m_court.z = 640.0f;
	m_court.w = 450.0f;

	mp_player[0] = player1;
	mp_player[1] = player2;

	setImageArea();
}

cCourt::~cCourt()
{}

///Functions
void cCourt::CheckCollision()
{
	if (m_pause == false)
	{
		CheckPaddles();
		CheckWalls();
		CheckOffCourt();
	}
}

void cCourt::Quit()
{
}

const bool cCourt::isPause() const
{
	return m_pause;
}

void cCourt::Pause()
{
	m_pause = !m_pause;

	mp_ball->Pause();
	mp_player[0]->Pause();
	mp_player[1]->Pause();
}

//private:
//Checks to see if the ball has hit a paddles and then if the paddles have hit a wall.
void cCourt::CheckPaddles()
{
	for (int count = 0; count <= 1; ++count)
	{
		//Check to see if the ball hit the Paddles.
		if (cCollision::BoundingBox(mp_player[count]->getPosition(), mp_ball->getPosition()) == true)
			mp_ball->BounceX();

		//Check to see if the Paddles hit the top wall.
		if (cCollision::BoundingBox(mp_player[count]->getPosition(), m_topWall) == true)
			mp_player[count]->setPosY((long)m_topWall.w + 1);

		//Check to see if the Paddles hit the bottom wall.
		if (cCollision::BoundingBox(mp_player[count]->getPosition(), m_bottomWall) == true)
			mp_player[count]->setPosY((long)m_bottomWall.y - 1);
	}
}

//Cheaks to see if the ball has hit the provide wall.
void cCourt::CheckWall(const Vector4& wall)
{
	if (cCollision::BoundingBox(mp_ball->getPosition(), wall) == true)
		mp_ball->BounceY();
}

//Cheaks to see if the ball has hit the wall.
void cCourt::CheckWalls()
{
	CheckWall(m_topWall);
	CheckWall(m_bottomWall);
}

//Checks to see if the ball is off the provide side of the court.
const bool cCourt::CheckOffCourt(const Vector4& offside)
{
	bool rtn = false;

	if (cCollision::BoundingBox(mp_ball->getPosition(), offside) == true)
	{
		mp_ball->setSpeed(0,0);
		rtn = true;
	}

	return rtn;
}

//Checks to see if the ball is off the court
void cCourt::CheckOffCourt()
{
	MathEngine::Vector2 mov = cCollision::Inside(m_court, mp_ball->getPosition());

	if (mov.x != 0.0f)
	{
// 		if (mov.x > 0.0f);
// 			//point to player 1
// 		if (mov.x < 0.0f);
// 			//point to player 2
	}

	if (mov.y != 0.0f)
		mp_ball->AddPosY((long)mov.y);

	//if (CheckOffCourt(m_leftOff) == true)
	//	;
	//if (CheckOffCourt(m_rightOff) == true)
	//	;
}