#include "stdafx.h"
#include "HelpScreen.h"

#include "CellWarsGameManager.h"

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

const String HelpScreen::HELP_STRING0("Survive 5 minutes until the final bacteria storm arrives.");
const String HelpScreen::HELP_STRING1("Get the greatest amount of the strongest bacteria as possible.");
const String HelpScreen::HELP_STRING2("Eat cells to get resources for your evolution and replication.");

const String HelpScreen::BACTERIA_STRING("Those bacteria are under your control. "
										 "Move them around by dragging them with your finger.");
const String HelpScreen::MACROPHAGES_STRING("Those macrophages will devour a single bacterium. "
											"Send multiple bacteria into the battle to defeat them.");
const String HelpScreen::CELLS_STRING("Those cells are resource containers. "
									  "Let your bacteria eat them to be able to get a stronger army.");

const String HelpScreen::SELECTION_CIRCLE_STRING("Draw a circle around your bacteria to select them.");

const String HelpScreen::REPLICATE_STRING("Whats better than one bacterium? Two. The copy will be as strong as its origin.");
const String HelpScreen::UPGRADE_STRING("Do you want your bacteria better, stronger, and faster? Then spend their resources on an upgrade.");
const String HelpScreen::REMOVE_SELECTION_STRING("Touch this button to remove the bacteria from the selection.");

const String HelpScreen::HEALTH_STRING("This health bar shows you how fit your bacteria are.");
const String HelpScreen::RESOURCES_STRING("This resource bar shows you how much resources your bacteria have collected.");

HelpScreen::HelpScreen(CellWarsMTManager* useThisMTManager) : 
useThisMultiTouchTManager(useThisMTManager), helpText(new TextActor()), 
	restartButt(new ReStartButton(useThisMTManager)), exitButt(new ExitButton(useThisMTManager)),
	nextPageButton(new SwitchHelpButton(useThisMTManager)), 
	bacterium(new Actor()), macrophage(new Actor()), cell(new Actor()), 
	selecting(new Actor()), selected(new Actor()), replicate(new Actor()), 
	upgrade(new Actor()), removeSelection(new Actor()), healthBar(new Actor()), 
	resources(new Actor()), bacteriumText(new TextActor()), macrophageText(new TextActor()), 
	cellText(new TextActor()), selectingText(new TextActor()), 
	/*selectedText(new TextActor()), */replicateText(new TextActor()), 
	upgradeText(new TextActor()), removeSelectionText(new TextActor()), 
	healthBarText(new TextActor()), resourcesText(new TextActor()), currentPage(0)
{
	SetPosition(theCamera.GetPosition());
	SetColor(1,1,1,1);
	ClearSpriteInfo();
	SetSize((theCamera.GetWorldMaxVertex() - theCamera.GetWorldMinVertex()) * 0.75f);
	SetName("HelpScreen");
	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 helpTextPos(leftTop.X + 3, leftTop.Y - 3);
	Vector2 bacteriaPos(leftTop.X + 3, leftTop.Y - 10);
	Vector2 bacteriaTextPos(leftTop.X + 7, leftTop.Y - 10);
	Vector2 macrophagePos(leftTop.X + 3, leftTop.Y - 14);
	Vector2 macrophageTextPos(leftTop.X + 7, leftTop.Y - 14);
	Vector2 cellPos(leftTop.X + 3, leftTop.Y - 18);
	Vector2 cellTextPos(leftTop.X + 7, leftTop.Y - 18);

	Vector2 selectingPos(leftTop.X + 6, leftTop.Y - 6);
	Vector2 selectedPos(leftTop.X + 14, leftTop.Y - 6);
	Vector2 selectingTextPos(leftTop.X + 3, leftTop.Y - 10);
	//Vector2 selectedTextPos(leftTop.X + 3, leftTop.Y - 3);
	Vector2 healthBarPos(leftTop.X + 3, leftTop.Y - 14);
	Vector2 healthBarTextPos(leftTop.X + 7, leftTop.Y - 14);
	Vector2 resourcesPos(leftTop.X + 3, leftTop.Y - 18);
	Vector2 resourcesTextPos(leftTop.X + 7, leftTop.Y - 18);

	Vector2 replicatePos(leftTop.X + 3, leftTop.Y - 3);
	Vector2 replicateTextPos(leftTop.X + 7, leftTop.Y - 3);
	Vector2 upgradePos(leftTop.X + 3, leftTop.Y - 7);
	Vector2 upgradeTextPos(leftTop.X + 7, leftTop.Y - 7);
	Vector2 removeSelectionPos(leftTop.X + 3, leftTop.Y - 11);
	Vector2 removeSelectionTextPos(leftTop.X + 7, leftTop.Y - 11);
	
	restartButt->SetPosition(rightBottom);
	restartButt->SetSize(3);

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

	nextPageButton->SetPosition(rightBottom);
	nextPageButton->SetSize(3);
	nextPageButton->setHelpScreen(this);
	
	int charsPerLineHelp = MathUtil::WorldUnitsToPixels(this->GetSize().X - 6) / BIG_FONT_WIDTH;
	helpText->SetPosition(helpTextPos);
	helpText->SetDisplayString(
		BreakStringIntoLines(HELP_STRING0, charsPerLineHelp) + "\n" + 
		BreakStringIntoLines(HELP_STRING1, charsPerLineHelp) + "\n" + 
		BreakStringIntoLines(HELP_STRING2, charsPerLineHelp));
	helpText->SetAlpha(0);
	theWorld.Add(helpText, LayerEnum::FormControls);
	
	bacterium->SetPosition(bacteriaPos);
	bacterium->SetColor(1,1,1,0);
	bacterium->ClearSpriteInfo();
	bacterium->SetSize(3);
	bacterium->SetName("HelpBacterium");
	bacterium->SetSprite("Resources/Images/bacterium.png");
	theWorld.Add(bacterium, LayerEnum::FormControls);
	
	macrophage->SetPosition(macrophagePos);
	macrophage->SetColor(1,1,1,0);
	macrophage->ClearSpriteInfo();
	macrophage->SetSize(3);
	macrophage->SetName("Helpmacrophage");
	macrophage->SetSprite("Resources/Images/macrophage.png");
	theWorld.Add(macrophage, LayerEnum::FormControls);
	
	cell->SetPosition(cellPos);
	cell->SetColor(1,1,1,0);
	cell->ClearSpriteInfo();
	cell->SetSize(3);
	cell->SetName("Helpcell");
	cell->SetSprite("Resources/Images/cell.png");
	theWorld.Add(cell, LayerEnum::FormControls);
	
	selecting->SetPosition(selectingPos);
	selecting->SetColor(1,1,1,0);
	selecting->ClearSpriteInfo();
	selecting->SetSize(5);
	selecting->SetName("Helpselecting");
	selecting->SetSprite("Resources/Images/selecting.png");
	theWorld.Add(selecting, LayerEnum::FormControls);
	
	selected->SetPosition(selectedPos);
	selected->SetColor(1,1,1,0);
	selected->ClearSpriteInfo();
	selected->SetSize(5);
	selected->SetName("Helpselected");
	selected->SetSprite("Resources/Images/selected.png");
	theWorld.Add(selected, LayerEnum::FormControls);
	
	replicate->SetPosition(replicatePos);
	replicate->SetColor(1,1,1,0);
	replicate->ClearSpriteInfo();
	replicate->SetSize(3);
	replicate->SetName("Helpreplicate");
	replicate->SetSprite("Resources/Images/replicate.png");
	theWorld.Add(replicate, LayerEnum::FormControls);
	
	upgrade->SetPosition(upgradePos);
	upgrade->SetColor(1,1,1,0);
	upgrade->ClearSpriteInfo();
	upgrade->SetSize(3);
	upgrade->SetName("Helpupgrade");
	upgrade->SetSprite("Resources/Images/upgrade.png");
	theWorld.Add(upgrade, LayerEnum::FormControls);
	
	removeSelection->SetPosition(removeSelectionPos);
	removeSelection->SetColor(1,1,1,0);
	removeSelection->ClearSpriteInfo();
	removeSelection->SetSize(3);
	removeSelection->SetName("HelpremoveSelection");
	removeSelection->SetSprite("Resources/Images/removeSelection active.png");
	theWorld.Add(removeSelection, LayerEnum::FormControls);
	
	healthBar->SetPosition(healthBarPos);
	healthBar->SetColor(1,1,1,0);
	healthBar->ClearSpriteInfo();
	healthBar->SetSize(3);
	healthBar->SetName("HelphealthBar");
	healthBar->SetSprite("Resources/Images/health.png");
	theWorld.Add(healthBar, LayerEnum::FormControls);
	
	resources->SetPosition(resourcesPos);
	resources->SetColor(1,1,1,0);
	resources->ClearSpriteInfo();
	resources->SetSize(3);
	resources->SetName("Helpresources");
	resources->SetSprite("Resources/Images/resources.png");
	theWorld.Add(resources, LayerEnum::FormControls);
	
	int charsPerLineDescription = MathUtil::WorldUnitsToPixels(this->GetSize().X - 10) / BIG_FONT_WIDTH;

	bacteriumText->SetPosition(bacteriaTextPos);
	bacteriumText->SetDisplayString(BreakStringIntoLines(BACTERIA_STRING, charsPerLineDescription));
	bacteriumText->SetAlpha(0);
	theWorld.Add(bacteriumText, LayerEnum::FormControls);

	macrophageText->SetPosition(macrophageTextPos);
	macrophageText->SetDisplayString(BreakStringIntoLines(MACROPHAGES_STRING, charsPerLineDescription));
	macrophageText->SetAlpha(0);
	theWorld.Add(macrophageText, LayerEnum::FormControls);

	cellText->SetPosition(cellTextPos);
	cellText->SetDisplayString(BreakStringIntoLines(CELLS_STRING, charsPerLineDescription));
	cellText->SetAlpha(0);
	theWorld.Add(cellText, LayerEnum::FormControls);

	selectingText->SetPosition(selectingTextPos);
	selectingText->SetDisplayString(BreakStringIntoLines(SELECTION_CIRCLE_STRING, charsPerLineDescription));
	selectingText->SetAlpha(0);
	theWorld.Add(selectingText, LayerEnum::FormControls);
	/*
	selectedText->SetPosition(bacteriaTextPos);
	selectedText->SetDisplayString(BreakStringIntoLines(SELECTION_CIRCLE_STRING, charsPerLineDescription));
	theWorld.Add(selectedText, LayerEnum::FormControls);
	*/
	replicateText->SetPosition(replicateTextPos);
	replicateText->SetDisplayString(BreakStringIntoLines(REPLICATE_STRING, charsPerLineDescription));
	replicateText->SetAlpha(0);
	theWorld.Add(replicateText, LayerEnum::FormControls);

	upgradeText->SetPosition(upgradeTextPos);
	upgradeText->SetDisplayString(BreakStringIntoLines(UPGRADE_STRING, charsPerLineDescription));
	upgradeText->SetAlpha(0);
	theWorld.Add(upgradeText, LayerEnum::FormControls);

	removeSelectionText->SetPosition(removeSelectionTextPos);
	removeSelectionText->SetDisplayString(BreakStringIntoLines(REMOVE_SELECTION_STRING, charsPerLineDescription));
	removeSelectionText->SetAlpha(0);
	theWorld.Add(removeSelectionText, LayerEnum::FormControls);

	healthBarText->SetPosition(healthBarTextPos);
	healthBarText->SetDisplayString(BreakStringIntoLines(HEALTH_STRING, charsPerLineDescription));
	healthBarText->SetAlpha(0);
	theWorld.Add(healthBarText, LayerEnum::FormControls);

	resourcesText->SetPosition(resourcesTextPos);
	resourcesText->SetDisplayString(BreakStringIntoLines(RESOURCES_STRING, charsPerLineDescription));
	resourcesText->SetAlpha(0);
	theWorld.Add(resourcesText, LayerEnum::FormControls);

	setControlsVisibleOnPage(currentPage);
}

HelpScreen::~HelpScreen()
{
}

void HelpScreen::PreDestroy()
{
	helpText->Destroy();
	restartButt->Destroy();
	exitButt->Destroy();
	nextPageButton->Destroy();

	bacterium->Destroy();
	macrophage->Destroy();
	cell->Destroy();
	selecting->Destroy();
	selected->Destroy();
	replicate->Destroy();
	upgrade->Destroy();
	removeSelection->Destroy();
	healthBar->Destroy();
	resources->Destroy();

	bacteriumText->Destroy();
	macrophageText->Destroy();
	cellText->Destroy();
	selectingText->Destroy();
	//selectedText->Destroy();
	replicateText->Destroy();
	upgradeText->Destroy();
	removeSelectionText->Destroy();
	healthBarText->Destroy();
	resourcesText->Destroy();
}

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

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

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

void HelpScreen::SetAlpha(float newAlpha)
{
	Actor::SetAlpha(newAlpha);
	
	restartButt->SetAlpha(newAlpha);
	exitButt->SetAlpha(newAlpha);
	nextPageButton->SetAlpha(newAlpha);
	helpText->SetAlpha(newAlpha);

	bacterium->SetAlpha(newAlpha);
	macrophage->SetAlpha(newAlpha);
	cell->SetAlpha(newAlpha);
	selecting->SetAlpha(newAlpha);
	selected->SetAlpha(newAlpha);
	replicate->SetAlpha(newAlpha);
	upgrade->SetAlpha(newAlpha);
	removeSelection->SetAlpha(newAlpha);
	healthBar->SetAlpha(newAlpha);
	resources->SetAlpha(newAlpha);

	bacteriumText->SetAlpha(newAlpha);
	macrophageText->SetAlpha(newAlpha);
	cellText->SetAlpha(newAlpha);
	selectingText->SetAlpha(newAlpha);
	//selectedText->SetAlpha(newAlpha);
	replicateText->SetAlpha(newAlpha);
	upgradeText->SetAlpha(newAlpha);
	removeSelectionText->SetAlpha(newAlpha);
	healthBarText->SetAlpha(newAlpha);
	resourcesText->SetAlpha(newAlpha);
}

void HelpScreen::ChangeAlphaTo(const float newAlpha, float duration, bool smooth, String onCompletionMessage)
{
	Actor::ChangeAlphaTo(newAlpha, duration, smooth, onCompletionMessage);
	
	restartButt->ChangeAlphaTo(newAlpha, duration, smooth);
	exitButt->ChangeAlphaTo(newAlpha, duration, smooth);
	nextPageButton->ChangeAlphaTo(newAlpha, duration, smooth);
	helpText->ChangeAlphaTo(newAlpha, duration, smooth);
	
	bacterium->ChangeAlphaTo(newAlpha, duration, smooth);
	macrophage->ChangeAlphaTo(newAlpha, duration, smooth);
	cell->ChangeAlphaTo(newAlpha, duration, smooth);
	selecting->ChangeAlphaTo(newAlpha, duration, smooth);
	selected->ChangeAlphaTo(newAlpha, duration, smooth);
	replicate->ChangeAlphaTo(newAlpha, duration, smooth);
	upgrade->ChangeAlphaTo(newAlpha, duration, smooth);
	removeSelection->ChangeAlphaTo(newAlpha, duration, smooth);
	healthBar->ChangeAlphaTo(newAlpha, duration, smooth);
	resources->ChangeAlphaTo(newAlpha, duration, smooth);

	bacteriumText->ChangeAlphaTo(newAlpha, duration, smooth);
	macrophageText->ChangeAlphaTo(newAlpha, duration, smooth);
	cellText->ChangeAlphaTo(newAlpha, duration, smooth);
	selectingText->ChangeAlphaTo(newAlpha, duration, smooth);
	//selectedText->ChangeAlphaTo(newAlpha, duration, smooth);
	replicateText->ChangeAlphaTo(newAlpha, duration, smooth);
	upgradeText->ChangeAlphaTo(newAlpha, duration, smooth);
	removeSelectionText->ChangeAlphaTo(newAlpha, duration, smooth);
	healthBarText->ChangeAlphaTo(newAlpha, duration, smooth);
	resourcesText->ChangeAlphaTo(newAlpha, duration, smooth);
}

void HelpScreen::gotoNextPage()
{
	++currentPage;
	setControlsVisibleOnPage(currentPage);
}

void HelpScreen::setControlsVisibleOnPage(int page)
{
	float nextPageButtonAlpha = 0;
	if (page == 0 || page == 1)
	{
		nextPageButtonAlpha = 1;
		restartButt->setInactive();
		nextPageButton->setActive();
	}
	else
	{
		restartButt->setActive();
		nextPageButton->setInactive();
	}
	float page0Alpha = 0;
	if (page == 0)
		page0Alpha = 1;
	float page1Alpha = 0;
	if (page == 1)
		page1Alpha = 1;
	float page2Alpha = 0;
	if (page == 2)
		page2Alpha = 1;
	
	restartButt->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	exitButt->ChangeAlphaTo(1, theCellWarsGame.FADE_IN_OUT_TIME);
	nextPageButton->ChangeAlphaTo(nextPageButtonAlpha, theCellWarsGame.FADE_IN_OUT_TIME);
	helpText->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	
	bacterium->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	macrophage->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	cell->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	bacteriumText->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	macrophageText->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	cellText->ChangeAlphaTo(page0Alpha, theCellWarsGame.FADE_IN_OUT_TIME);

	selecting->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	selected->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	healthBar->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	resources->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	selectingText->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	healthBarText->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	resourcesText->ChangeAlphaTo(page1Alpha, theCellWarsGame.FADE_IN_OUT_TIME);

	replicate->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	upgrade->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	removeSelection->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	replicateText->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	upgradeText->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
	removeSelectionText->ChangeAlphaTo(page2Alpha, theCellWarsGame.FADE_IN_OUT_TIME);
}