/*********************************************************************
*	Player set window definition
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 28.7.2008
*	Soubor: menu_playerswindow.cpp
*	Popis: Okno umoznuje zvoleni jmena hrace, ke kteremu se pak budou prirazovat
*			nejlepsi dosazene vysledky.
**********************************************************************/

#include "game_mainmenu.h"
#include "textLibrary.h"
#include "ctrl_var_definition.h"
#include "ctrl_gamecontrol.h"
#include "parsing.h"
#include "sys_console.h"

using namespace mainMenu;
using namespace std;
using namespace systemConsole;
using namespace vardef;

const float PLAYER_WINDOW_WIDTH						= 500.0f;
const float PLAYER_WINDOW_HEIGHT					= 400.0f;
const float PLAYER_WINDOW_CONT_PADDING				= 0.08f;
const float PLAYER_WINDOW_PADDING					= 10.0f;
const float PLAYER_WINDOW_LABEL_HEIGHT				= 32.0f;
const float PLAYER_WINDOW_LABEL_NAME_WIDTH			= 1.0f;
const float PLAYER_WINDOW_NAME_WIDTH				= 3.0f;
const float PLAYER_WINDOW_TEXTBOX_WIDTH				= 3.0f;
const float PLAYER_WINDOW_BOTTOM_VERTICAL_PADDING	= 0.6f;
const size_t PLAYER_WINDOW_MAX_NAMES				= 10;
static const char* PLAYER_LABELS_FONT				= "SERIF";
static const char* PLAYER_FILE_OPEN_ERROR			= "Error: Player name list file could not be opened.";

/***************************************************************
***********	PLAYER SET WINDOW CREATION AND FUNCTIONS ***********
****************************************************************/
CSubWindow *MainMenu::createPlayerSetWindow()
{
	CSubWindow *window = new CSubWindow( m_fWidth / 2.0f - PLAYER_WINDOW_WIDTH / 2.0f, m_fHeight / 2.0f - PLAYER_WINDOW_HEIGHT / 2.0f,
			PLAYER_WINDOW_WIDTH, PLAYER_WINDOW_HEIGHT);
		window->setCaption( textLibrary::getText( TXT_PLAYER_NAME ) );

	ContainerRelativeUpToDown *container = new ContainerRelativeUpToDown();
	container->setHorizontalPadding( PLAYER_WINDOW_CONT_PADDING );
	container->setPadding( PLAYER_WINDOW_PADDING );

	window->setComponent( container );
	ContainerRelativeLeftToRight * containerLR = new ContainerRelativeLeftToRight();
	container->addComponent( containerLR, PLAYER_WINDOW_LABEL_HEIGHT );
	containerLR->setVerticalPadding( PLAYER_WINDOW_CONT_PADDING );

	SimpleLabel *label = new SimpleLabel(0,0);
	label->setFont( PLAYER_LABELS_FONT );
	label->setCaption( textLibrary::getText( TXT_CURRENT ) );
	containerLR->addComponent( label, PLAYER_WINDOW_LABEL_NAME_WIDTH );

	m_pCurrentPlayer = new SimpleLabel(0,0);
	m_pCurrentPlayer->setFont( PLAYER_LABELS_FONT );
	m_pCurrentPlayer->setAlign( FONT_ALIGN_RIGHT );
	containerLR->addComponent( m_pCurrentPlayer, PLAYER_WINDOW_NAME_WIDTH );

	containerLR = new ContainerRelativeLeftToRight();
	container->addComponent( containerLR, PLAYER_WINDOW_LABEL_HEIGHT );
	containerLR->setVerticalPadding( PLAYER_WINDOW_CONT_PADDING );

	m_pInsertName = new TextBox(0,0);
	m_pInsertName->setMaxLenght( vardef::NAME_MAXLENGHT );
	containerLR->addComponent( m_pInsertName, PLAYER_WINDOW_TEXTBOX_WIDTH );

	Button *button = GFG_newButton( textLibrary::getText( TXT_ADD ), PLAYER_ADD );
	containerLR->addComponent( button );

	m_pPlayerNames = new ListBox(0,0);
	m_pPlayerNames->setObjectID( PLAYER_NAME_LIST );
	container->addComponent( m_pPlayerNames, container->getScissorBox().h - 3.0f * PLAYER_WINDOW_LABEL_HEIGHT );

	containerLR = new ContainerRelativeLeftToRight();
	container->addComponent( containerLR, PLAYER_WINDOW_LABEL_HEIGHT );

	containerLR->setVerticalPadding( PLAYER_WINDOW_BOTTOM_VERTICAL_PADDING );

	button = GFG_newButton( textLibrary::getText( 16 ), PLAYER_CLOSE );
	containerLR->addComponent( button );
	
	fillNameList();
	return window;
}

//aktualizuje okno podle aktualniho jmena hrace
void MainMenu::refreshPlayerWindow()
{
	addPlayerName( gamevarsLibrary::getsData( vardef::NAME_NAME ) );
	if( m_pCurrentPlayer )
		m_pCurrentPlayer->setCaption( gamevarsLibrary::getsData( vardef::NAME_NAME ) );
	if( m_pMainMenuPlayerName )
		m_pMainMenuPlayerName->setCaption( gamevarsLibrary::getsData( vardef::NAME_NAME ) );
}

//vlozi napsane jmeno do seznamu
void MainMenu::addTypedPlayerName()
{
	if( m_pInsertName )
	{
		string name = m_pInsertName->getCaption();
		addPlayerName( name );
	}
}

//testuje, zda jmeno neni v seznamu jmen
bool MainMenu::checkIfNameExists( std::string name )
{
	list<CBasePanel*> *tmpList = m_pPlayerNames->getPanelList();
	NamePanel *tmpPanel = NULL;
	for( list<CBasePanel*>::iterator iter = tmpList->begin(); iter != tmpList->end(); iter++ )
	{
		tmpPanel = reinterpret_cast<NamePanel*>( (*iter) );
		if( tmpPanel && tmpPanel->m_sName == name )
		{
			m_pPlayerNames->setSelectedPanel( iter );
			setCurrentPlayerName( tmpPanel );
			return true;
		}
	}
	return false;
}

void MainMenu::setCurrentPlayerName( NamePanel *panel )
{
	if( panel )
	{
		if( m_pCurrentPlayer )
			m_pCurrentPlayer->setCaption( panel->m_sName );
		if( m_pMainMenuPlayerName )
			m_pMainMenuPlayerName->setCaption( panel->m_sName );
		gamevarsLibrary::setVariable( vardef::NAME_NAME, panel->m_sName );
	}
}

void MainMenu::fillNameList()
{
	ifstream fin( PLAYER_NAMES_FILE );
	if( !fin.is_open() )
	{
		console << PLAYER_FILE_OPEN_ERROR << endline;
		return;
	}

	string name;
	while( true )
	{
		name = parsing::readPosibleQuotedData( fin );
		if( fin.eof() )
			break;
		addPlayerName( name );
	}
	fin.close();

	string current_name = gamevarsLibrary::getsData( vardef::NAME_NAME );
	if( !current_name.empty() && current_name != vardef::NAME_DEFAULT )
		addPlayerName( current_name );
	
	/*if( m_pCurrentPlayer )
		m_pCurrentPlayer->setCaption( gamevarsLibrary::getsData( vardef::NAME_NAME ) );
	if( m_pMainMenuPlayerName )
		m_pMainMenuPlayerName->setCaption( gamevarsLibrary::getsData( vardef::NAME_NAME ) );*/
	m_bSaveNameList = true;
}

//prida jmeno do seznamu, pokud tam neni
void MainMenu::addPlayerName( std::string name )
{
	if( !name.empty() && !checkIfNameExists( name ) )
	{
		if( m_pPlayerNames->getPanelList() && m_pPlayerNames->getPanelList()->size() >= PLAYER_WINDOW_MAX_NAMES )
			m_pPlayerNames->removePanel();

		NamePanel *newPanel = new NamePanel();
		newPanel->create( name );
		m_pPlayerNames->addPanel( newPanel, false );
		m_pPlayerNames->setSelectedPanel( newPanel );
		if( m_bSaveNameList )
			saveNameList();
	}
}

//ulozi seznam jmen do souboru
void MainMenu::saveNameList()
{
	string currentName = gamevarsLibrary::getsData( vardef::NAME_NAME );
	ofstream fout( PLAYER_NAMES_FILE );
	list<CBasePanel*> *tmpList = m_pPlayerNames->getPanelList();
	NamePanel *tmpPanel = NULL;
	for( list<CBasePanel*>::reverse_iterator iter = tmpList->rbegin(); iter != tmpList->rend(); iter++ )
	{
		tmpPanel = reinterpret_cast<NamePanel*>( (*iter) );
		if( tmpPanel )
		{
			if( currentName != tmpPanel->m_sName )
				fout << "\"" << tmpPanel->m_sName << "\"" << endl;
		}
	}
	fout << "\"" << currentName << "\"" << endl;
	fout.close();
}


/************************************************************************
**************	PLAYER NAMES PANEL DEFINITION AND FUNCTIONS	*********
************************************************************************/
NamePanel::NamePanel()
{

}

void NamePanel::create( std::string name )
{
	m_sName = name;
	setHeight( MAINMENU_PANELS_HEIGHT );
	setPadding( 0 );
	SimpleLabel *label = new SimpleLabel(0,0);
	setComponent( label );
	label->setFont( PLAYER_LABELS_FONT );
	label->setColorText( COLOR_PANEL_TEXT );
	label->setCaption( m_sName );
}
