//	TitleScreen.cpp - The title screen.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "DwindlingSounder.h"
#include "drawing/Drawer.h"
#include "TitleScreen.h"
#include "Random.h"

using namespace std;

//------------------------------------------------------------------------------
GameStateCreator<TitleScreen> TitleScreen::creator;

//------------------------------------------------------------------------------
TitleScreen::TitleScreen(Application *app, Sounder *sound):
GameState(app, sound),
fadeIn(1.0f),
fadeOut(0.0f),
colInterp(0.0f)
{
	const ThreeFloats white(1.0f);

	lastCol.x = Random::getGlobal().getRandomFloat();
	lastCol.y = Random::getGlobal().getRandomFloat();
	lastCol.z = Random::getGlobal().getRandomFloat();
	while(lastCol.getDistance(white) < 0.1f)
	{
		lastCol.x = Random::getGlobal().getRandomFloat();
		lastCol.y = Random::getGlobal().getRandomFloat();
		lastCol.z = Random::getGlobal().getRandomFloat();
	}

	nextCol.x = Random::getGlobal().getRandomFloat();
	nextCol.y = Random::getGlobal().getRandomFloat();
	nextCol.z = Random::getGlobal().getRandomFloat();
	while(nextCol.getDistance(white) < 0.1f)
	{
		nextCol.x = Random::getGlobal().getRandomFloat();
		nextCol.y = Random::getGlobal().getRandomFloat();
		nextCol.z = Random::getGlobal().getRandomFloat();
	}

	((DwindlingSounder *)sound)->triggerSound(L"Speech_Intro");
}

//------------------------------------------------------------------------------
TitleScreen::~TitleScreen()
{

}

//------------------------------------------------------------------------------
void TitleScreen::draw(DrawerPtr d)
{
	const TwoFloats origin;
	const ThreeFloats black;
	const ThreeFloats white(1.0f);
	const TwoFloats& drawSize(d->getDrawingAreaSize());
	ThreeFloats tempCol;

	tempCol = lastCol * (1.0f-colInterp);
	tempCol += nextCol * colInterp;
	d->fillRect(origin, drawSize, tempCol);

	d->drawImage(L"Title", TwoFloats(32.0f));
	d->drawImage(L"AnyKey", TwoFloats(drawSize.x-365.0f, drawSize.y-65.0f));

	colInterp += 0.005f;
	if(colInterp >= 1.0f)
	{
		colInterp = 0.0f;

		lastCol = nextCol;
		nextCol.x = Random::getGlobal().getRandomFloat();
		nextCol.y = Random::getGlobal().getRandomFloat();
		nextCol.z = Random::getGlobal().getRandomFloat();
		while(nextCol.getDistance(white) < 0.1f)
		{
			nextCol.x = Random::getGlobal().getRandomFloat();
			nextCol.y = Random::getGlobal().getRandomFloat();
			nextCol.z = Random::getGlobal().getRandomFloat();
		}
	}

	if(fadeIn > 0.0f)
	{
		d->fillRect(origin, drawSize, black, fadeIn);

		fadeIn -= 0.02f;
	}
	else if(fadeOut > 0.0f)
	{
		d->fillRect(origin, drawSize, black, fadeOut);

		fadeOut += 0.02f;
		if(fadeOut > 1.0f)
			nextState(L"GamePlayState");
	}
}

//------------------------------------------------------------------------------
void TitleScreen::keyDown(const wstring& action,
						  const wstring& keyPressed,
						  int keyModifier)
{
	if(action == L"Escape")
		quit();
	else if(fadeOut <= 0.0f)
		fadeOut = 0.02f;
}

//------------------------------------------------------------------------------
void TitleScreen::mouseDown(int button, const TwoFloats& pos)
{
	nextState(L"GamePlayState");
}
