#include "stdafx.h"
#include "MultiSelectionCircle.h"

#include "CellWarsGameManager.h"
#include "CellWarsMTManager.h"
#include "Bacterium.h"
#include "SelectionCircle.h"
#include "SelectionCircleButtons.h"

MultiSelectionCircle::MultiSelectionCircle(CellWarsMTManager *useThisMultiTouchManager) : 
Actor(), Touchable(useThisMultiTouchManager)
{
	selectedBacteriaList = new std::vector<Bacterium*>();
	buttons = new std::vector<SelectionButton*>();
	useThisMTManager = useThisMultiTouchManager;

	SetColor(1,1,1,1);
	ClearSpriteInfo();
	SetSize(0);
	SetName("MultiSelectionCircle");
	SetSprite("Resources/Images/multiSelectionCircle.png");
	theWorld.Add(this, LayerEnum::MultiSelectionCircle);
	
	setPriorityLevel(1);
	
	buttons->push_back(new SelectionCircleButtonReplicate(0, useThisMultiTouchManager, this));
	buttons->push_back(new SelectionCircleButtonUpgrade(1, useThisMultiTouchManager, this));
	buttons->push_back(new SelectionCircleButtonRemoveSelection(4, useThisMultiTouchManager, this));
	
	buttons->push_back(new SelectionCircleButtonReplicate(0, useThisMultiTouchManager, this, true));
	buttons->push_back(new SelectionCircleButtonUpgrade(1, useThisMultiTouchManager, this, true));
	buttons->push_back(new SelectionCircleButtonRemoveSelection(4, useThisMultiTouchManager, this, true));

	//std::cout << "MultiSelectionCircle " << _name << std::endl;
}

MultiSelectionCircle::~MultiSelectionCircle()
{
	delete selectedBacteriaList;
	delete buttons;

	//std::cout << "~MultiSelectionCircle " << _name << std::endl;
}

void MultiSelectionCircle::PreDestroy()
{
	useThisMTManager->removeTouchableFromCallbackList(this);
	useThisMTManager = NULL;

	std::vector<Bacterium*>::iterator bactIt(selectedBacteriaList->begin());
	while (bactIt != selectedBacteriaList->end())
	{
		(*bactIt)->skipMissingFingerUpEventsFromMSCircle(numFingersOnThis);
		(*bactIt)->isNowDeselected();
		++bactIt;
	}

	std::vector<SelectionButton*>::iterator buttonIt = buttons->begin();
	while(buttonIt != buttons->end())
	{
		(*buttonIt)->Destroy();
		buttonIt = buttons->erase(buttonIt);
	}

	//std::cout << "predestroy MultiSelectionCircle " << _name << std::endl;
}

void MultiSelectionCircle::addBacteriumToSelectionList(Bacterium* newBacterium)
{
	if (!newBacterium->isCurrentlySelected())
	{
		selectedBacteriaList->push_back(newBacterium);
		newBacterium->isNowSelected(this);
	}
}

void MultiSelectionCircle::removeBacteriumFromSelectionList(Bacterium* remBacterium)
{
	std::vector<Bacterium*>::iterator it(selectedBacteriaList->begin());
	while (it != selectedBacteriaList->end())
	{
		if ((*it) == remBacterium)
		{
			selectedBacteriaList->erase(it);
			remBacterium->isNowDeselected();
			break;
		}
		++it;
	}

	if (selectedBacteriaList->begin() == selectedBacteriaList->end())
		useThisMTManager->removeMultiSelectionCircle(this);
}

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

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

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

	Vector2 centerOfSelectedBacteria(0, 0);
	
	// get center of circle
	int numSelectedBacteria = 0;
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		centerOfSelectedBacteria += (*it)->GetPosition();
		++numSelectedBacteria;
		++it;
	}
	centerOfSelectedBacteria /= (float)numSelectedBacteria;

	// get minimum radius
	float maxDistancePlusRadius = 0;
	it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		Vector2 dist(centerOfSelectedBacteria - (*it)->GetPosition());
		float thisDistance = dist.Length() + ((*it)->GetSize().X * 0.5f);
		if (thisDistance > maxDistancePlusRadius)
		{
			maxDistancePlusRadius = thisDistance;
		}
		++it;
	}

	SetPosition(centerOfSelectedBacteria);
	SetSize(maxDistancePlusRadius * 2);
}

void MultiSelectionCircle::fingerDown(Vector2 posDownInAngelCoords)
{
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		(*it)->fingerDown(posDownInAngelCoords);
		++it;
	}
	Touchable::fingerDown(posDownInAngelCoords);
}

void MultiSelectionCircle::fingerMove(Vector2 moveToInAngelCoords)
{
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		(*it)->fingerMove(moveToInAngelCoords);
		++it;
	}
	Touchable::fingerMove(moveToInAngelCoords);
}

void MultiSelectionCircle::fingerUp(Vector2 lastPosInAngelCoords)
{
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		(*it)->fingerUp(lastPosInAngelCoords);
		++it;
	}
	Touchable::fingerUp(lastPosInAngelCoords);
}

void MultiSelectionCircle::upgradeSelected()
{
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		(*it)->upgrade();
		++it;
	}
}

void MultiSelectionCircle::replicateSelected()
{
	std::vector<Bacterium*>* newBactList = new std::vector<Bacterium*>();

	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		Bacterium* newBact = (*it)->replicate();
		if (newBact)
			newBactList->push_back(newBact);
		++it;
	}
	
	std::vector<Bacterium*>::iterator newBactIt = newBactList->begin();
	while(newBactIt != newBactList->end())
	{
		addBacteriumToSelectionList(*newBactIt);
		++newBactIt;
	}
}

bool MultiSelectionCircle::anySelectedHasFullResources()
{
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		if ((*it)->hasFullResources())
			return true;

		++it;
	}
	return false;
}

bool MultiSelectionCircle::hasBacteriumSelected(Bacterium* bact)
{
	std::vector<Bacterium*>::iterator it = selectedBacteriaList->begin();
	while(it != selectedBacteriaList->end())
	{
		if ((*it) == bact)
			return true;

		++it;
	}
	return false;
}