#include "stdafx.h"
#include "EndScreen.h"

#include "CellWarsGameManager.h"
#include "Util\StringUtil.h"
#include "Util\MathUtil.h"

const float EndScreen::BIG_FONT_WIDTH = 12;
const float EndScreen::SMALL_FONT_WIDTH = 9;

const String EndScreen::SUCCESS_STRING("Well done! You brought pain and suffer to your victim.");
const String EndScreen::FAIL_STRING("Damn you macrophages! Next time things will be different.");
const String EndScreen::REPLICATIONS_STRING("Replications:");
const String EndScreen::UPGRADES_STRING("Upgrades:");
const String EndScreen::KILLED_MACROPHAGES_STRING("Macrophages killed:");
const String EndScreen::KILLED_CELLS_STRING("Cells eaten:");
const String EndScreen::TIME_SURVIVED_STRING("Time survived:");

EndScreen::EndScreen(CellWarsMTManager* useThisMTManager) : 
useThisMultiTouchTManager(useThisMTManager), restartButt(new ReStartButton(useThisMTManager)), exitButt(new ExitButton(useThisMTManager)), 
	messageText(new TextActor()), replicationsText(new TextActor()), replicationsStat(new TextActor()), upgradesText(new TextActor()), upgradesStat(new TextActor()), 
	killedMacrophagesText(new TextActor()), killedMacrophagesStat(new TextActor()), killedCellsText(new TextActor()), killedCellsStat(new TextActor()), 
	timeSurvivedText(new TextActor()), timeSurvivedStat(new TextActor())
{
	SetPosition(theCamera.GetPosition());
	SetColor(1,1,1,1);
	ClearSpriteInfo();
	SetSize((theCamera.GetWorldMaxVertex() - theCamera.GetWorldMinVertex()) * 0.75f);
	SetName("EndScreen");
	SetSprite("Resources/Images/formbg.png");
	theWorld.Add(this, LayerEnum::FormBackground);
	
	Vector2 leftTop(GetPosition().X - 0.5f * GetSize().X, GetPosition().Y + 0.5f * GetSize().Y);
	Vector2 rightTop(GetPosition().X + 0.5f * GetSize().X, GetPosition().Y + 0.5f * GetSize().Y);
	Vector2 leftBottom(GetPosition().X - 0.5f * GetSize().X, GetPosition().Y - 0.5f * GetSize().Y);
	Vector2 rightBottom(GetPosition().X + 0.5f * GetSize().X, GetPosition().Y - 0.5f * GetSize().Y);
	
	Vector2 messageTextPos(leftTop.X + 3, leftTop.Y - 3);
	Vector2 replicationsTextPos(GetPosition().X, rightBottom.Y + 9);
	Vector2 replicationsStatPos(GetPosition().X + 3, rightBottom.Y + 9);
	Vector2 upgradesTextPos(GetPosition().X, rightBottom.Y + 7.5f);
	Vector2 upgradesStatPos(GetPosition().X + 3, rightBottom.Y + 7.5f);
	Vector2 killedMacrophagesTextPos(GetPosition().X, rightBottom.Y + 6);
	Vector2 killedMacrophagesStatPos(GetPosition().X + 3, rightBottom.Y + 6);
	Vector2 killedCellsTextPos(GetPosition().X, rightBottom.Y + 4.5f);
	Vector2 killedCellsStatPos(GetPosition().X + 3, rightBottom.Y + 4.5f);
	Vector2 timeSurvivedTextPos(GetPosition().X, rightBottom.Y + 3);
	Vector2 timeSurvivedStatPos(GetPosition().X + 3, rightBottom.Y + 3);

	exitButt->SetPosition(leftBottom);
	exitButt->SetSize(3);

	restartButt->SetPosition(rightBottom);
	restartButt->SetSize(3);
	
	int charsPerLineMessage = MathUtil::WorldUnitsToPixels(this->GetSize().X - 6) / BIG_FONT_WIDTH;
	if (theCellWarsGame.isGameSuccess())
		messageText->SetDisplayString(BreakStringIntoLines(SUCCESS_STRING, charsPerLineMessage));
	else
		messageText->SetDisplayString(BreakStringIntoLines(FAIL_STRING, charsPerLineMessage));
	messageText->SetPosition(messageTextPos);
	theWorld.Add(messageText, LayerEnum::FormControls);

	replicationsText->SetAlignment(TXT_Right);
	replicationsText->SetDisplayString(REPLICATIONS_STRING);
	replicationsText->SetPosition(replicationsTextPos);
	theWorld.Add(replicationsText, LayerEnum::FormControls);

	replicationsStat->SetDisplayString(IntToString(theCellWarsGame.getReplications()));
	replicationsStat->SetPosition(replicationsStatPos);
	theWorld.Add(replicationsStat, LayerEnum::FormControls);

	upgradesText->SetAlignment(TXT_Right);
	upgradesText->SetDisplayString(UPGRADES_STRING);
	upgradesText->SetPosition(upgradesTextPos);
	theWorld.Add(upgradesText, LayerEnum::FormControls);

	upgradesStat->SetDisplayString(IntToString(theCellWarsGame.getUpgrades()));
	upgradesStat->SetPosition(upgradesStatPos);
	theWorld.Add(upgradesStat, LayerEnum::FormControls);

	killedMacrophagesText->SetAlignment(TXT_Right);
	killedMacrophagesText->SetDisplayString(KILLED_MACROPHAGES_STRING);
	killedMacrophagesText->SetPosition(killedMacrophagesTextPos);
	theWorld.Add(killedMacrophagesText, LayerEnum::FormControls);

	killedMacrophagesStat->SetDisplayString(IntToString(theCellWarsGame.getKilledMacrophages()));
	killedMacrophagesStat->SetPosition(killedMacrophagesStatPos);
	theWorld.Add(killedMacrophagesStat, LayerEnum::FormControls);

	killedCellsText->SetAlignment(TXT_Right);
	killedCellsText->SetDisplayString(KILLED_CELLS_STRING);
	killedCellsText->SetPosition(killedCellsTextPos);
	theWorld.Add(killedCellsText, LayerEnum::FormControls);

	killedCellsStat->SetDisplayString(IntToString(theCellWarsGame.getKilledCells()));
	killedCellsStat->SetPosition(killedCellsStatPos);
	theWorld.Add(killedCellsStat, LayerEnum::FormControls);

	timeSurvivedText->SetAlignment(TXT_Right);
	timeSurvivedText->SetDisplayString(TIME_SURVIVED_STRING);
	timeSurvivedText->SetPosition(timeSurvivedTextPos);
	theWorld.Add(timeSurvivedText, LayerEnum::FormControls);

	int timeInSec = (int)MathUtil::round(theCellWarsGame.getPlayTime());
	int min = timeInSec / 60;
	int sec = timeInSec % 60;
	timeSurvivedStat->SetDisplayString(IntToString(min) + ":" + ((sec < 10) ? "0" : "") + IntToString(sec));
	timeSurvivedStat->SetPosition(timeSurvivedStatPos);
	theWorld.Add(timeSurvivedStat, LayerEnum::FormControls);

	/*
	messageText
	replicationsText
	replicationsStat
	upgradesText
	upgradesStat
	killedMacrophagesText
	killedMacrophagesStat
	killedCellsText
	killedCellsStat
	timeSurvivedText
	timeSurvivedStat
	*/
}

EndScreen::~EndScreen()
{
}

void EndScreen::PreDestroy()
{
	restartButt->Destroy();
	exitButt->Destroy();
	messageText->Destroy();
	replicationsText->Destroy();
	replicationsStat->Destroy();
	upgradesText->Destroy();
	upgradesStat->Destroy();
	killedMacrophagesText->Destroy();
	killedMacrophagesStat->Destroy();
	killedCellsText->Destroy();
	killedCellsStat->Destroy();
	timeSurvivedText->Destroy();
	timeSurvivedStat->Destroy();

}

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

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

void EndScreen::Update(float dt)
{
	Actor::Update(dt);
	
}

void EndScreen::SetAlpha(float newAlpha)
{
	Actor::SetAlpha(newAlpha);

	exitButt->SetAlpha(newAlpha);
	restartButt->SetAlpha(newAlpha);
	messageText->SetAlpha(newAlpha);
	replicationsText->SetAlpha(newAlpha);
	replicationsStat->SetAlpha(newAlpha);
	upgradesText->SetAlpha(newAlpha);
	upgradesStat->SetAlpha(newAlpha);
	killedMacrophagesText->SetAlpha(newAlpha);
	killedMacrophagesStat->SetAlpha(newAlpha);
	killedCellsText->SetAlpha(newAlpha);
	killedCellsStat->SetAlpha(newAlpha);
	timeSurvivedText->SetAlpha(newAlpha);
	timeSurvivedStat->SetAlpha(newAlpha);
}

void EndScreen::ChangeAlphaTo(const float newAlpha, float duration, bool smooth, String onCompletionMessage)
{
	Actor::ChangeAlphaTo(newAlpha, duration, smooth, onCompletionMessage);
	
	exitButt->ChangeAlphaTo(newAlpha, duration, smooth);
	restartButt->ChangeAlphaTo(newAlpha, duration, smooth);
	messageText->ChangeAlphaTo(newAlpha, duration, smooth);
	replicationsText->ChangeAlphaTo(newAlpha, duration, smooth);
	replicationsStat->ChangeAlphaTo(newAlpha, duration, smooth);
	upgradesText->ChangeAlphaTo(newAlpha, duration, smooth);
	upgradesStat->ChangeAlphaTo(newAlpha, duration, smooth);
	killedMacrophagesText->ChangeAlphaTo(newAlpha, duration, smooth);
	killedMacrophagesStat->ChangeAlphaTo(newAlpha, duration, smooth);
	killedCellsText->ChangeAlphaTo(newAlpha, duration, smooth);
	killedCellsStat->ChangeAlphaTo(newAlpha, duration, smooth);
	timeSurvivedText->ChangeAlphaTo(newAlpha, duration, smooth);
	timeSurvivedStat->ChangeAlphaTo(newAlpha, duration, smooth);
}