/*********************************************************************//**
*	Vstupni funkce programu.
*	provadi inicializace modulu a pote vstoupi do hlavni smycky
*
*	author: Michal Jirous
*	date: 09.04.2009
*	file: main.cpp
**********************************************************************/

#include "game.h"
#include "sys_config.h"
#include "intro.h"
#include "SDL/SDL_image.h"
#include "textLibrary.h"
#include "textureslib.h"
#include "game_levels.h"
#include "game_highscore.h"
#include "ctrl_keydatabase.h"
#include "fonts.h"

#include "ctrl_gamevars.h"
#include "ctrl_commands.h"
#include "ctrl_binding.h"
#include "ctrl_gamecontrol.h"

#include "game_loadingscreen.h"
#include "game_mainmenu.h"
#include "game_musicplayer.h"
#include "parsing.h"

#include "sys_controller.h"
#include "HUD.h"


#include "spriteslib.h"
#include "modelslib.h"
#include "soundslib.h"
#include "sys_snd_reserv.h"
#include "general_sounds.h"
#include "error_handler.h"


int main(int argc, char **argv)
{
	std::string gameDir( argv[0] );
	size_t lastSlash = gameDir.find_last_of("\\/");
	gameDir = gameDir.substr( 0, lastSlash + 1 );

	systemconf::setGameDirectory( gameDir );

	//problem se zastuocem nebo spoustenim ze vzdaleneho adresare
#ifdef WIN32
	SetCurrentDirectory( gameDir.c_str() );
#else
	chdir(gameDir.c_str() );	//linux snad - not tested yet
#endif

	if (systemConsole::sys_console.setLogingFile( "game.log" ) )	//TODO
		systemConsole::sys_console.startLoging();

	CONSOLE_PRINT_LN("System starting");

	srand( (unsigned int)time(NULL) );
	if( !systemconf::loadConfig( systemconf::SYSTEM_CONFIG_FILE ) )
		return 1;
	
	systemconf::parseCommandLine( argc, argv );
	SDL_ShowCursor( SDL_DISABLE );	//skryty kurzor mysi

	/*	Game Intro
	*	Prehravani pokud neni zakazano
	*	Pouziva zakladni SDL surface, po prehrani je treba ho 
	*	smazat a vytvorit novy s OpenGL
	*/
	if( systemconf::getSystemInfo().m_iSkipIntro == FALSE )
	{
		Uint32 flags = systemconf::INTRO_FLAGS;
		if( systemconf::getSystemInfo().m_iWindowedMode == FALSE )
			flags |= SDL_FULLSCREEN;
		SDL_Surface *introSurface = SDL_SetVideoMode(systemconf::getSystemInfo().m_iResolutionWidth,
			systemconf::getSystemInfo().m_iResolutionHeight, 0, flags);
		gameIntro.init( introSurface, systemconf::INTRO_LIST_FILE );
		gameIntro.main_loop();
		SDL_Quit();			//ukoncime sdl a pak ho znovu nahodime s opengl
		systemconf::init();
	}

	systemconf::initVideo();
	


	/*	System loader
	*
	*
	*/
	
	//init languages
	textLibrary::init( systemconf::LANGUAGES_LIST_FILE, systemconf::getSystemInfo().m_sLanguage );
	
	//init textures
	textureLibrary.loadWAD3( "wad/halflife.wad" );
	textureLibrary.loadWAD3( "wad/chronophobia.wad" );
	textureLibrary.loadWAD3( "wad/decals.wad" );

	//level list
	levels::loadDatabase( systemconf::LEVEL_LIST_FILE );
	
	//highscore load
	highscore::loadDatabase( systemconf::HIGHSCORE_DATA_FILE );

	//key database load
	key_database::loadDatabase( systemconf::KEY_DATABASE_FILE );

	//fonst database load
	fontLibrary::loadFonts( systemconf::FONTS_LIST_FILE );

	//game variables init
	gamevarsLibrary::init();

	//commands init
	commandsLibrary::init();

	//bind database load
	binding::loadDatabase( systemconf::WATCHED_COMMADS_FILE );

	//inicializace ovladaciho modulu
	gameControl::game_control.init();

	//inicializace uzivatelskeho rozhrani
	GFG_init();

	loadingScreen::loading_screen.init( 	(float)systemconf::getSystemInfo().m_iResolutionWidth,
		(float)systemconf::getSystemInfo().m_iResolutionHeight );
	
	mainMenu::main_menu.init( (float)systemconf::getSystemInfo().m_iResolutionWidth,
		(float)systemconf::getSystemInfo().m_iResolutionHeight );
	
	systemConsole::sys_console.createHUDConsole( (float)systemconf::getSystemInfo().m_iResolutionWidth,
		(float)systemconf::getSystemInfo().m_iResolutionHeight );

	HUD::hud.init( (float)systemconf::getSystemInfo().m_iResolutionWidth,
		(float)systemconf::getSystemInfo().m_iResolutionHeight );

	soundLibrary.init( &argc, argv );
	generalSounds.init();
	game.init();
	
	

	sys_reserveSounds();
	
	//soundLibrary.unloadSound( m_ShootSound1 );
	music_player::init();
	
	systemConsole::sys_console.setMyAlphaMultiplier( ALPHA_TRANSPARENT );
	mainMenu::main_menu.setMyAlphaMultiplier( ALPHA_TRANSPARENT );
	loadingScreen::loading_screen.setMyAlphaMultiplier( ALPHA_OPAQUE );
		
	if( systemconf::getSystemInfo().m_sStartMap.empty() )
		game.loadMap( systemconf::getSystemInfo().m_sMenuMap, systemController::MAINMENU );
	else
		game.loadMap( systemconf::getSystemInfo().m_sStartMap, systemController::GAME );

	systemController::main();

	CONSOLE_PRINT_LN("Running system clean up.");
	//ObjectInfo::object_info.destroy();
	game.destroy();
	loadingScreen::loading_screen.destroy();
	mainMenu::main_menu.destroy();
	systemConsole::sys_console.destroy();
	HUD::hud.deleteAll();

	generalSounds.destroy();
	sys_freeSounds();
	soundLibrary.quit();
	music_player::stop();
	music_player::destroy();
	fontLibrary::destructor();
	textureLibrary.deleteAll();
	modelLib::modelLibrary.destroy();
	spritesLibrary.destroy();
	CONSOLE_PRINT_LN("Quiting.");
	systemConsole::sys_console.stopLoging();
	systemconf::quit();
	return 0;
}
