#include "stdafx.h"
#include "SelectionButton.h"

#include "CellWarsGameManager.h"
#include "CellWarsMTManager.h"
#include "MultiSelectionCircle.h"
#include "Infrastructure\Camera.h"

#include "Util\MathUtil.h"
#include <math.h>

SelectionButton::SelectionButton(int buttonSlot, CellWarsMTManager *useThisMultiTouchManager, 
	MultiSelectionCircle *belongsToMultiSelectionCircle, bool isMirrored) : 
Actor(), Touchable(useThisMultiTouchManager), mIsMirrored(isMirrored), 
	belongsToThisMSCircle(belongsToMultiSelectionCircle), belongsToThisBacterium(NULL),
	useThisMTManager(useThisMultiTouchManager), thisButtonSlot(buttonSlot),
	fingerOnThisWholeTime(true)
{
	SetColor(1,1,1,1);
	ClearSpriteInfo();
	SetSize(1);
	SetName("SelectionButton");
	theWorld.Add(this, LayerEnum::SelectionCircleButton);

	if (mIsMirrored)
		SetRotation(180);
	
	setPriorityLevel(1);
}

SelectionButton::SelectionButton(int buttonSlot, CellWarsMTManager *useThisMultiTouchManager, 
	Bacterium *belongsToBacterium, bool isMirrored) :
Actor(), Touchable(useThisMultiTouchManager), mIsMirrored(isMirrored), 
	belongsToThisMSCircle(NULL), belongsToThisBacterium(belongsToBacterium),
	useThisMTManager(useThisMultiTouchManager), thisButtonSlot(buttonSlot),
	fingerOnThisWholeTime(true)
{
	SetColor(1,1,1,1);
	ClearSpriteInfo();
	SetSize(1);
	SetName("SelectionButton");
	theWorld.Add(this, LayerEnum::SelectionCircleButton);

	if (mIsMirrored)
		SetRotation(180);
	
	setPriorityLevel(1);
}


SelectionButton::~SelectionButton()
{
}

void SelectionButton::ReceiveMessage(Message *m)
{
	Actor::ReceiveMessage(m);
}

void SelectionButton::Render()
{
	Actor::Render();
}

void SelectionButton::Update(float dt)
{
	Actor::Update(dt);

	static const float BUTTON_SLOT_MARGIN = 0.01f;
	float radius = 0;
	if (belongsToThisMSCircle)
		radius = 0.5f * belongsToThisMSCircle->GetSize().Y + 0.5f * GetSize().Y;
	else if (belongsToThisBacterium)
		radius = 0.5f * belongsToThisBacterium->GetSize().Y + 0.5f * GetSize().Y;
	float anglePerButtonSlot = (GetSize().X + BUTTON_SLOT_MARGIN) / radius;
	float angleOffset = -MathUtil::Pi * 0.5f;
	if (mIsMirrored)
		angleOffset += MathUtil::Pi;
	float angle = angleOffset - anglePerButtonSlot * thisButtonSlot;

	Vector2 newPos;
	if (belongsToThisMSCircle)
		newPos = belongsToThisMSCircle->GetPosition();
	else if (belongsToThisBacterium)
		newPos = belongsToThisBacterium->GetPosition();
	newPos.X += cos(angle) * radius;
	newPos.Y += sin(angle) * radius;
	
	float maxSlotIndex = 4;
	if (belongsToThisBacterium)
		maxSlotIndex = 1;
	float slotFactor = (float)thisButtonSlot;
	if (mIsMirrored)
		slotFactor = (float)(maxSlotIndex - thisButtonSlot);
	float minY = theCamera.GetWorldMinVertex().Y + GetSize().Y * 0.5f;
	if (newPos.Y < minY) newPos.Y = minY;
	float maxY = theCamera.GetWorldMaxVertex().Y - GetSize().Y * 0.5f;
	if (newPos.Y > maxY) newPos.Y = maxY;
	float minX = theCamera.GetWorldMinVertex().X + GetSize().X * 0.5f + GetSize().X * (maxSlotIndex - slotFactor);
	if (newPos.X < minX) newPos.X = minX;
	float maxX = theCamera.GetWorldMaxVertex().X - GetSize().X * 0.5f - GetSize().X * slotFactor;
	if (newPos.X > maxX) newPos.X = maxX;

	SetPosition(newPos);
}

void SelectionButton::fingerDown(Vector2 posDownInAngelCoords)
{
	Touchable::fingerDown(posDownInAngelCoords);
	fingerOnThisWholeTime = true;
}

void SelectionButton::fingerMove(Vector2 moveToInAngelCoords)
{
	Touchable::fingerMove(moveToInAngelCoords);
	Vector2 dist(moveToInAngelCoords - GetPosition());
	if (dist.Length() > GetSize().X)
		fingerOnThisWholeTime = false;
}

void SelectionButton::fingerUp(Vector2 lastPosInAngelCoords)
{
	Touchable::fingerUp(lastPosInAngelCoords);
	if (fingerOnThisWholeTime)
		click();
}
