//	AppSettingsExample.cpp - Example of how to implement the unimplemented
//							 AppSettings bits.
//  ----------------------------------------------------------------------------
//	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 "AppSettings.h"
#include "Application.h"
#include "DebugHeaders.h"

#include "GamePlayState.h"
#include "OptionsScreen.h"

#include "drawing/GLImmediateDrawer.h"
#include "sound/GenericFileSounder.h"
#include "drawing/GLESDrawer.h"
#include "TankYouGlobalData.h"

#include <sstream>

using std::wstringstream;
using std::wstring;

//------------------------------------------------------------------------------
wstring AppSettings::applicationName = L"Tank You!";
wstring AppSettings::applicationVersion = L"0.13";

//------------------------------------------------------------------------------
void AppSettings::initialise(CmdLineParams& cmdLine,
							 Application *app,
							 KeyManager& keyManager,
							 wstring& helpText)
{
	//Register your interest in any command line arguments.
	cmdLine.registerCallback(L"oscOutput", L"--oscOutput", L"-oo", true, L"-1", this);

	//Setup the dimensions of the drawing area.
	setDrawingDimensions(1280, 720, AlterRatio);

	//Register all possible GameStates.
	app->registerGameState(L"OptionsScreen", &OptionsScreen::creator, true);
	app->registerGameState(L"GamePlayState", &GamePlayState::creator);

	//Setup some default key mappings.
	// - Player 1 movement (joystick).
	keyManager.addMapping(L"P1Up", L"Joy0Up", true);
	keyManager.addMapping(L"P1Left", L"Joy0Left", true);
	keyManager.addMapping(L"P1Right", L"Joy0Right", true);
	keyManager.addMapping(L"P1Down", L"Joy0Down", true);
	keyManager.addMapping(L"P1Forward", L"Joy0TriggerRight", true);
	keyManager.addMapping(L"P1Backward", L"Joy0TriggerLeft", true);
	keyManager.addMapping(L"P1Primary", L"Joy0Button0", true);
	keyManager.addMapping(L"P1Secondary", L"Joy0Button1", true);

	// - Player 2 movement (joystick).
	keyManager.addMapping(L"P2Up", L"Joy1Up", true);
	keyManager.addMapping(L"P2Left", L"Joy1Left", true);
	keyManager.addMapping(L"P2Right", L"Joy1Right", true);
	keyManager.addMapping(L"P2Down", L"Joy1Down", true);
	keyManager.addMapping(L"P2Forward", L"Joy1TriggerRight", true);
	keyManager.addMapping(L"P2Backward", L"Joy1TriggerLeft", true);
	keyManager.addMapping(L"P2Primary", L"Joy1Button0", true);
	keyManager.addMapping(L"P2Secondary", L"Joy1Button1", true);
	
	// - Player 3 movement (keys).
	keyManager.addMapping(L"P3Up", L"w", true);
	keyManager.addMapping(L"P3Left", L"a", true);
	keyManager.addMapping(L"P3Right", L"d", true);
	keyManager.addMapping(L"P3Down", L"s", true);
	keyManager.addMapping(L"P3Forward", L"w", true);
	keyManager.addMapping(L"P3Backward", L"s", true);
	keyManager.addMapping(L"P3Primary", L"c", true);
	keyManager.addMapping(L"P3Secondary", L"v", true);
	// - Player 3 movement (joystick).
	keyManager.addMapping(L"P3Up", L"Joy2Up", true);
	keyManager.addMapping(L"P3Left", L"Joy2Left", true);
	keyManager.addMapping(L"P3Right", L"Joy2Right", true);
	keyManager.addMapping(L"P3Down", L"Joy2Down", true);
	keyManager.addMapping(L"P3Forward", L"Joy2TriggerRight", true);
	keyManager.addMapping(L"P3Backward", L"Joy2TriggerLeft", true);
	keyManager.addMapping(L"P3Primary", L"Joy2Button0", true);
	keyManager.addMapping(L"P3Secondary", L"Joy2Button1", true);
	
	// - Player 4 movement (keys).
	keyManager.addMapping(L"P4Up", L"CursorUp", true);
	keyManager.addMapping(L"P4Left", L"CursorLeft", true);
	keyManager.addMapping(L"P4Right", L"CursorRight", true);
	keyManager.addMapping(L"P4Down", L"CursorDown", true);
	keyManager.addMapping(L"P4Forward", L"CursorUp", true);
	keyManager.addMapping(L"P4Backward", L"CursorDown", true);
	keyManager.addMapping(L"P4Primary", L"NumPad7", true);
	keyManager.addMapping(L"P4Secondary", L"NumPad8", true);
	// - Player 4 movement (joystick).
	keyManager.addMapping(L"P4Up", L"Joy3Up", true);
	keyManager.addMapping(L"P4Left", L"Joy3Left", true);
	keyManager.addMapping(L"P4Right", L"Joy3Right", true);
	keyManager.addMapping(L"P4Down", L"Joy3Down", true);
	keyManager.addMapping(L"P4Forward", L"Joy3TriggerRight", true);
	keyManager.addMapping(L"P4Backward", L"Joy3TriggerLeft", true);
	keyManager.addMapping(L"P4Primary", L"Joy3Button0", true);
	keyManager.addMapping(L"P4Secondary", L"Joy3Button1", true);

	// - General.
	keyManager.addMapping(L"Escape", L"Escape", true);
	keyManager.addMapping(L"Restart", L"r", true);
	keyManager.addMapping(L"Restart", L"Joy0Button7", true);
	keyManager.addMapping(L"Restart", L"Joy1Button7", true);
	keyManager.addMapping(L"Restart", L"Joy2Button7", true);
	keyManager.addMapping(L"Restart", L"Joy3Button7", true);
	keyManager.addMapping(L"Console", L"Tab", true);

	//Fill out helpText.

	//Set the LoadingScreen colours.
}

//------------------------------------------------------------------------------
DrawerPtr AppSettings::getDrawer(const std::wstring& drawerType)
{
	if(drawerType == L"GLImmediateDrawer")
		return DrawerPtr(new GLImmediateDrawer());
	else
	{
		if(GLESDrawer::isSupported())
			return DrawerPtr(new GLESDrawer());
		else
		{
			DebugStatement(L"GLESDrawer not supported. Falling back to GLImmediateDrawer");
			return DrawerPtr(new GLImmediateDrawer());
		}
	}
}

//------------------------------------------------------------------------------
SounderPtr AppSettings::getSounder()
{
	return SounderPtr(new GenericFileSounder());
}

//------------------------------------------------------------------------------
GlobalDataPtr AppSettings::getGlobalData()
{
	return GlobalDataPtr(new TankYouGlobalData());
}
