#ifndef _BREAKOUTPADDLE_H_
#define _BREAKOUTPADDLE_H_

#include "Vector2.h"
#include "AABB.h"

#include "BreakoutBox.h"
#include "BreakoutWorldBoundaries.h"
#include "Visualisation.h"

class BreakoutPaddle : public BreakoutBox
{
	public:
	BreakoutPaddle(const Vector2i &pos, const Vector2i &boxSize, const ColourRGB &innerColour, const ColourRGB &outerColour)
	: BreakoutBox(), m_bBallAttached(false), m_pBallAttached(0)
	{
		m_position = pos; m_size = boxSize; updateBBox();
		m_sprID = Visualisation::getSingleton()->CreateSpriteBox(boxSize.x, boxSize.y, outerColour, innerColour);
	}

	virtual void Draw()
	{
		if(m_bIsEnabled)
		{
			Visualisation::getSingleton()->DrawSprite(m_sprID, m_position.x, m_position.y);
		}
	}
	virtual void Update()
	{
		this->SetPosition(m_position + m_velocity);
		if(m_bBallAttached)
		{
			m_pBallAttached->SetPosition(m_position + m_ballOffset);
		}
	}

	bool HandleCollisionWithWorldBoundaries(BreakoutWorldBoundaries* pBounds)
	{
		Vector2i minBounds = pBounds->GetMinBounds();
		Vector2i maxBounds = pBounds->GetMaxBounds();
		
		if(m_boundingBox.Right > maxBounds.x)
		{
			int xDiff = m_boundingBox.Right - maxBounds.x;
			m_position.x -= xDiff;
			updateBBox();
			return true;
		}

		if(m_boundingBox.Left < minBounds.x)
		{
			int xDiff = minBounds.x - m_boundingBox.Left;
			m_position.x += xDiff;
			updateBBox();
			return true;
		}

		return false;
	}

	void AttachBallToPaddle(BreakoutBall* pBall)
	{
		m_pBallAttached = pBall;
		m_bBallAttached = true;

		m_pBallAttached->SetVelocity(Vector2i(0,0));
		m_ballOffset.x = (m_size.x / 2) - (pBall->GetRadius() / 2);
		m_ballOffset.y = -pBall->GetRadius() - 2; //Padding to prevent there being constant
												  //collisions between ball and paddle when attached.
		m_pBallAttached->SetPosition(m_position + m_ballOffset);
	}

	void DetachBallFromPaddle()
	{
		m_pBallAttached = 0;
		m_bBallAttached = false;
	}

	bool IsBallAttached() { return m_bBallAttached; }

	Vector2i GetVelocity() { return m_velocity; }
	void SetVelocity(const Vector2i &vel) { m_velocity = vel; }
	void AddToVelocity(const Vector2i &vec) { m_velocity += vec; }

	protected:
	SpriteID m_sprID;
	Vector2i m_velocity, m_ballOffset;
	bool m_bBallAttached;
	BreakoutBall* m_pBallAttached;
};


#endif
