#include "stdafx.h"
#include "StartScreen.h"

#include "CellWarsGameManager.h"

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

const String StartScreen::CREDITS_STRING0("A game by Philipp Erler");
const String StartScreen::CREDITS_STRING1("Powered by Angel2D");
const String StartScreen::CREDITS_STRING2("Thanks to:");
const String StartScreen::CREDITS_STRING3("milgrim+rize (Music and Sounds)");
const String StartScreen::CREDITS_STRING4("Prof. Dr.-Ing. Reinhard Schmidt, Peter Dück, Fabian Müller (Hochschule Esslingen members)");
const String StartScreen::CREDITS_STRING5("Chasing Carrots KG, Kristian Dürsch, Tim Erler, Patrick Tejada (Advices and Tests)");

StartScreen::StartScreen(CellWarsMTManager* useThisMTManager) : 
useThisMultiTouchTManager(useThisMTManager), creditsText(new TextActor("ConsoleSmall")), 
	goButt(new StartButton(useThisMTManager)), exitButt(new ExitButton(useThisMTManager)), 
	helpButt(new HelpButton(useThisMTManager)), title(new Actor()), inVolButt(new IncreaseVolumeButton(useThisMTManager)), 
	deVolButt(new DecreaseVolumeButton(useThisMTManager)), volumeText(new TextActor("ConsoleSmall")), 
	volumeValueText(new TextActor("ConsoleSmall"))
{
	SetPosition(theCamera.GetPosition());
	SetColor(1,1,1,1);
	ClearSpriteInfo();
	SetSize((theCamera.GetWorldMaxVertex() - theCamera.GetWorldMinVertex()) * 0.75f);
	SetName("StartScreen");
	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 titlePos(this->GetPosition().X, this->GetPosition().Y + this->GetSize().Y * 0.2f);
	Vector2 creditsTextPos(leftTop.X + 3, this->GetPosition().Y - 1.5f);
	Vector2 inVolButtPos(leftTop.X + 15, this->GetPosition().Y - 8);
	Vector2 deVolButtPos(leftTop.X + 8, this->GetPosition().Y - 8);
	Vector2 volPos(leftTop.X + 3, this->GetPosition().Y - 8);
	Vector2 volValTextPos(leftTop.X + 11, this->GetPosition().Y - 8);

	title->SetPosition(titlePos);
	title->SetColor(1,1,1,1);
	title->ClearSpriteInfo();
	title->SetSize((rightTop.X - leftBottom.X) * 0.9f, (rightTop.Y - leftBottom.Y) * 0.5f);
	title->SetName("Title");
	title->SetSprite("Resources/Images/title.png");
	theWorld.Add(title, LayerEnum::FormControls);

	goButt->SetPosition(rightBottom);
	goButt->SetSize(3);

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

	inVolButt->SetPosition(inVolButtPos);
	inVolButt->SetSize(3);

	deVolButt->SetPosition(deVolButtPos);
	deVolButt->SetSize(3);

	helpButt->SetPosition(this->GetPosition().X, leftBottom.Y);
	helpButt->SetSize(3);
	
	int charsPerLineCredits = MathUtil::WorldUnitsToPixels(this->GetSize().X - 6) / SMALL_FONT_WIDTH;
	creditsText->SetDisplayString(
		BreakStringIntoLines(CREDITS_STRING0, charsPerLineCredits) + "\n" + 
		BreakStringIntoLines(CREDITS_STRING1, charsPerLineCredits) + "\n" + 
		BreakStringIntoLines(CREDITS_STRING2, charsPerLineCredits) + "\n" + 
		BreakStringIntoLines(CREDITS_STRING3, charsPerLineCredits) + "\n" + 
		BreakStringIntoLines(CREDITS_STRING4, charsPerLineCredits) + "\n" + 
		BreakStringIntoLines(CREDITS_STRING5, charsPerLineCredits));
	creditsText->SetPosition(creditsTextPos);
	theWorld.Add(creditsText, LayerEnum::FormControls);

	volumeText->SetDisplayString("Volume");
	volumeText->SetPosition(volPos);
	theWorld.Add(volumeText, LayerEnum::FormControls);

	volumeValueText->SetDisplayString("0.5");
	volumeValueText->SetPosition(volValTextPos);
	theWorld.Add(volumeValueText, LayerEnum::FormControls);
}

StartScreen::~StartScreen()
{
}

void StartScreen::PreDestroy()
{
	title->Destroy();
	creditsText->Destroy();
	goButt->Destroy();
	exitButt->Destroy();
	helpButt->Destroy();
	inVolButt->Destroy();
	deVolButt->Destroy();
	volumeText->Destroy();
	volumeValueText->Destroy();
}

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

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

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

	volumeValueText->SetDisplayString(FloatToString( MathUtil::round(theCellWarsGame.getMainVolume() * 100)));
}

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

	title->SetAlpha(newAlpha);
	goButt->SetAlpha(newAlpha);
	exitButt->SetAlpha(newAlpha);
	helpButt->SetAlpha(newAlpha);
	creditsText->SetAlpha(newAlpha);
	inVolButt->SetAlpha(newAlpha);
	deVolButt->SetAlpha(newAlpha);
	volumeText->SetAlpha(newAlpha);
	volumeValueText->SetAlpha(newAlpha);
}

void StartScreen::ChangeAlphaTo(const float newAlpha, float duration, bool smooth, String onCompletionMessage)
{
	Actor::ChangeAlphaTo(newAlpha, duration, smooth, onCompletionMessage);
	
	title->ChangeAlphaTo(newAlpha, duration, smooth);
	goButt->ChangeAlphaTo(newAlpha, duration, smooth);
	exitButt->ChangeAlphaTo(newAlpha, duration, smooth);
	helpButt->ChangeAlphaTo(newAlpha, duration, smooth);
	creditsText->ChangeAlphaTo(newAlpha, duration, smooth);
	inVolButt->ChangeAlphaTo(newAlpha, duration, smooth);
	deVolButt->ChangeAlphaTo(newAlpha, duration, smooth);
	volumeText->ChangeAlphaTo(newAlpha, duration, smooth);
	volumeValueText->ChangeAlphaTo(newAlpha, duration, smooth);
}