/*********************************************************************//**
*	\brief Hlavni menu.
*	Zde se definuji komponenty hlavniho menu, vsechny jeho okna
*	identifikace ovladacich prvku, obsluha udalosti, vykreslovani.
*	Hlavni menu je pocatecni interakci s uzivatelem pri spusteni hry.
*	umoznuje spoustet hru a menit nastaveni.
*
*	\author Michal Jirous
*	\date 07.04.2009
*	\file game_mainmenu.h
**********************************************************************/

#ifndef __MAINMENU_H__
#define __MAINMENU_H__\

#include "gfg.h"
#include "SDL/SDL.h"
#include "game_levels.h"
#include "ctrl_binding.h"

namespace mainMenu
{
	enum buttons 
	{
		BUTTON_PLAY = 1,
		BUTTON_OPTION,
		BUTTON_HELP,
		BUTTON_ABOUT,
		BUTTON_EXIT,
		BUTTON_RESUME,
		BUTTON_MSGBOX_QUIT,
		BUTTON_MSGBOX_CANCEL,
		BUTTON_PLAYWINDOW_GO,
		BUTTON_PLAYWINDOW_CLOSE,
		LISTBOX_LEVEL_LIST,
		OPTION_BUTTON_SAVE,
		OPTION_BUTTON_DEFAULT,
		OPTION_BUTTON_CANCEL,
		OPTION_KEYBOARD_DEFINITION_MAIN,
		OPTION_KEYBOARD_DEFINITION_ALTERNATIVE,
		OPTION_DEFAULT_MSGBOX_YES,
		BUTTON_PLAYER_SET,
		PLAYER_ADD,
		PLAYER_CLOSE,
		PLAYER_NAME_LIST,
		OPTION_BUTTON_HUDSET

	};

	const float MAINMENU_PANELS_HEIGHT		= 32.0f;
	const float TOP_LABELS_HEIGHT			= 48.0f;

	const float BUTTON_WIDTH				= 200.0f;
	const float BUTTON_HEIGHT				= 32.0f;
	
	//konstanty pro vytvareni oken
	const float WINDOW_WIDTH				= 0.7f;
	const float WINDOW_HEIGHT				= 0.7f;
	const float WINDOW_MINIMUM_WIDTH		= 600.0f;
	const float WINDOW_X_POSITION			= 0.2f;	//of global window width
	const float WINDOW_Y_POSITION			= 0.2f;	//of global window width
	const float WINDOW_POSITION_DIFFERENCE	= 20.0f;
	
	//playwindow definition
	const Color4I COLOR_PANEL_TEXT = Color4I( 0, 0, 0, 230 );

	static const char* PLAYER_NAMES_FILE = "players.list";
	static const char* LOGO_PICTURE_FILE = "textures/mainmenu/logo2.tga";

	class LevelPanel : public CBasePanel
	{
	public:
		LevelPanel();
		void create( std:: string key, levels::LevelInfo *info );
		void updateHighScore();
		Uint32 m_uiTime;
		LabelNoOutline *m_pTimeLabel;
		std::string m_sLevelKey;
		levels::LevelInfo *m_pLevelInfo;
	
	};

	//option window definition
	const bool ALTERNATIVE_KEY = true;
	const bool MAIN_KEY = false;

	class KeyOptionPanel : public CBasePanel
	{
	public:
		KeyOptionPanel();
		void create( std::string sCommand, binding::Keys *keys );
		void refresh();
		std::string m_sCommand;
		binding::Keys m_Keys;

		Button *m_pMainButton;
		Button *m_pAlternativeButton;	
	};

	
	class VideoResolutionPanel : public CBasePanel
	{
	public:
		VideoResolutionPanel();
		void create( int width, int height );
		int m_iResolutionWidth, m_iResolutionHeight;
	};

	class LanguagePanel : public CBasePanel
	{
	public:
		LanguagePanel();
		void create( std::string key, std::string lang );
		std::string m_sLanguage;
	};

	//player set window
	
	

	class NamePanel : public CBasePanel
	{
	public:
		NamePanel();
		void create( std::string name );
		std::string m_sName;
	};



	//animation structure
	struct AnimatedLogo
	{
		float m_fStaticX, m_fStaticY, m_fStaticWidth, m_fStaticHeight;
		Color4I m_BaseColor;
		float x,y,w,h,r,g,b,a;
		int m_iTextureID;
		void reset()
		{
			x = m_fStaticX;
			y = m_fStaticY;
			w = m_fStaticWidth;
			h = m_fStaticHeight;
			r = m_BaseColor.values[0];
			g = m_BaseColor.values[1];
			b = m_BaseColor.values[2];
			a = m_BaseColor.values[3];
		}
		void draw()
		{
			glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
			glColor4f( r,g,b,a );
			glEnable( GL_BLEND );
			glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
			glBindTexture( GL_TEXTURE_2D, m_iTextureID );
			glEnable( GL_TEXTURE_2D );
				glBegin( GL_QUADS );
					glTexCoord2f( 0, 0 );	glVertex2f( x, y );
					glTexCoord2f( 1, 0 );	glVertex2f( x + w, y );
					glTexCoord2f( 1, 1 );	glVertex2f( x + w, y + h );
					glTexCoord2f( 0, 1 );	glVertex2f( x, y + h );
				glEnd();
			glPopAttrib();
		
		}
	
	};
	
	struct FRect
	{
		float x,y,w,h;
	};

	//mainmenu
	
	
	static const char* NO_HIGHSCORE = "- - : - -";
	

	class MainMenu : public CMainWindow
	{
	public:
		CSubWindow *m_pPlayWindow, *m_pOptionWindow, *m_pPlayerSetWindow;
		ListBox *m_pLevelList, *m_pKeyMapList, *m_pVideoModes, *m_pLanguages, *m_pPlayerNames;
		LabelNoOutline *m_pBestPlayer;
		LabelNoOutline *m_pBestTime;
		LabelNoOutline *m_pLevelName;
		SimpleLabel *m_pCurrentPlayer, *m_pMainMenuPlayerName;
		ScrollLabel *m_pLevelDescription;
		Slider *m_pMouseSensitivity;
		Button *m_pResumeButton;
		CheckButton *m_pReverseHorizontaly, *m_pReverseVerticaly, *m_pWindowedBox, *m_pSkipIntro, *m_pDeveloperMode;
		TextBox *m_pInsertName;
		void setLevelDescriptionDetails( LevelPanel *panel );
		std::string formatTime( Uint32 ms );
		void addTypedPlayerName();
		void setCurrentPlayerName( NamePanel *panel );


		CMessageBox *m_pMsgQuit, *m_pMsgLostChanges;

	private:
		bool m_bSaveNameList;
		std::map<string,binding::Keys> m_OptionKeyMap;
		void copyCurrentWatchedCommandSettings();
		void createKeyboardOptionListPanels();
		void refreshKeyMapListBoxPanels();
		void refreshMouseTabComponents();
		void loadDefaultMouseTabComponentsSettings();
		bool checkIfNameExists( std::string name );
		float m_fTranslateXAnim, m_fTranslateYAnim;
		float m_fTargetAlphaLogoAnim;
		float m_fKeySetAlphaMultiplier;
		FRect m_LogoRect;
		void fillNameList();
		void saveNameList();
		void addPlayerName( std::string name );
		
		fontLibrary::DString m_dsKeySetModeInfo;
		bool m_bKeySetMode, m_bAlternative;
		Uint32 m_uiResetTime, m_uiResetTime2, m_uiResetTime3;

		AnimatedLogo m_Logo, m_LogoFast;
		TextureElement m_LogoTexture;
		
		CSubWindow *createPlayWindow();
		CSubWindow *createOptionWindow();
		CSubWindow *createPlayerSetWindow();
		void createKeyBoardTab( int id, Tabs *tabs );
		void createMouseTab( int id, Tabs *tab );
		void createVideoTab( int id, Tabs *tab );
		void createGeneralTab( int id, Tabs *tab );
		void createHUDTab( int id, Tabs *tab );
		void fillLanguages();
		void createLevelList();
		void fillVideoModesList();
		void refreshPlayWindowComponents();
		void loadDefaultVideoSettings();
		void refreshVideoSettings();
		void refreshGeneralSettings();
		void refreshPlayerWindow();
		void loadDefaultGeneralSettings();
		void setActiveVideoModePanel( int w, int h );
		void setActiveLanguage( std::string lang );
	public:
		MainMenu();
		
		void processSelectedLevel();
		void loadDefaultOptionSettings();
		void saveOptionSettings();
		binding::Keys *getKeysFromMap( std::string command )	{ return binding::getKeyOfWatchedCommand( command, &m_OptionKeyMap ); }
		void setKeySetMode( bool alternative ) { m_bKeySetMode = true; m_bAlternative = alternative; }
		void newKeyboardSet( int key );
		virtual void init( float width, float height );
		virtual void user_run();
		void refresh();
		void user_draw();
		void use_this_draw();
		void use_this_run();
		void SDL_controller( SDL_Event &event );
		void destroy();
	};

	extern MainMenu main_menu;

}

#endif
