/*********************************************************************
*	SystemConsole
*	HEADER FILE
*	Autor:	Michal Jirouš
*	Datum: 25.7.2008
*	Soubor: sys_console.h
*	Popis: Modul implementuje konzoli, ktera se pouziva pro textovou
* 			interakci s uzivatelem. Definje objekt, pomoci nehoz lze
* 			retezit datove typy do textoveho retezce a vzpisovat ho.
* 			Umoznuje logovani do souboru. 
**********************************************************************/

#ifndef SYS_CONSOLE_H_
#define SYS_CONSOLE_H_

#include <iostream>
#include <fstream>
#include <string>
#include "gfg.h"
#include "SDL_to_GFG.h"

namespace systemConsole
{
	struct Console
	{
		Console& operator<<( const int text );
		Console& operator<<( const std::string text );
		Console& operator<<( const float text);
		Console& operator<<( const double text );
		Console& operator<<( const long long int text );
		Console& operator<<( const unsigned int text );
		Console& operator<<( const unsigned long long int text );
		Console& operator<<( const char text );
		Console& operator<<( const char *text );
		Console& operator<<( const unsigned char text );
		Console& operator<<( const unsigned char *text );
	};

	static const char * HUDCONSOLE_BACKGROUND_IMAGE = "textures/console/background3.tga";
	static const char * HUDCONSOLE_TEXTBOX_LEFT = "textures/console/textbox_2px_prol_left.tga";
	static const char * HUDCONSOLE_TEXTBOX_CENTER = "textures/console/textbox_2px_prol_center.tga";
	static const char * HUDCONSOLE_TEXTBOX_RIGHT = "textures/console/textbox_2px_prol_right.tga";

	const float HUDCONSOLE_CONTAINER_PADDING					= 10.0f;
	const float HUDCONSOLE_CONTAINER_HORIZONTAL_PADDING_PERCENT = 0.1f;
	const float HUDCONSOLE_TEXTBOX_HEIGHT						= 28.0f;
	static const char* HUDCONSOLE_FONT							= "SERIF10";
	static const char* HUDCONSOLE_LABEL_FONT					= HUDCONSOLE_FONT;
	const Color4I HUDCONSOLE_LABEL_COLOR						= Color4I( 200,200,200,255 );
	const Color4I HUDCONSOLE_TEXTBOX_COLOR						= Color4I( 200,200,200,255 );
	const Color4I HUDCONSOLE_BOTTOM_LINE_COLOR					= Color4I( 200,200,200,255);
	const float HUDCONSOLE_BOTTOM_LINE_SIZE						= 2.0f;
	const size_t HUDCONSOLE_LABEL_MAX_LINES						= 40;
	const size_t HUDCONSOLE_COMMAND_LINE_MAX_LENGTH				= 100;
	const float HUDCONSOLE_BACKGROUND_ALPHA_MULT				= 0.8f;

	const float HUDCONSOLE_ANIM_FINNISH_SPEED					= 5.0f;
	const float HUDCONSOLE_MIN_ANIM_SPEED						= 20.0f;
	const float HUDCONSOLE_RESIZING_BORDER						= 10.0f;
	const float HUDCONSOLE_ANIM_SPEED_MULTIPLICATOR				= 0.2f;
	
	
	const float CONSOLE_TEXTBOX_TEX_WIDTH						= 16.0f;
	const float CONSOLE_TEXTBOX_TEX_HEIGHT						= 32.0f;
	const float CONSOLE_TEXTBOX_CURSOR_PADDING					= 3.0f;
	const Color4I HUDCONSOLE_TEXTBOX_OUTLINE_COLOR				= Color4I( 200,200,200,255);

	const Color4I HUDCONSOLE_SCROLLBAR_COLOR_IDLE				= Color4I( 200,200,200,255);
	const Color4I HUDCONSOLE_COLOR_MOUSE_OVER					= Color4I( 255,255,255,255);

	

	class ConsoleTextBox : public TextBox
	{
		GLuint m_uiTexLeft, m_uiTexCenter, m_uiTexRight;
	public:
		ConsoleTextBox( float w, float h );
		~ConsoleTextBox();
		virtual void draw( ScissorBox &ScissorBox );
	};

	class ConsoleScrollLabel : public ScrollLabel
	{
	public:	
		ConsoleScrollLabel( float w, float h );
		virtual void drawOutLines() {}
		void setScrollBarValue( float value );
	};

	//Trida SystemConsole implementuje systemovou a HUD konzoli
	//ovladajici funkce a promenne
	class SystemConsole : public CMainWindow
	{
		//hud console components
		ConsoleTextBox *m_pCommandLine;
		ConsoleScrollLabel *m_pConsoleText;
		ContainerRelativeUpToDown *m_pConsoleBox;
		void processCommand();
		void historyStepUp();
		void historyStepDown();
		void setCommandLineCaption( list<string>::iterator &iter );

		std::string buffer;
		bool m_bFlush;
		bool m_bHUDConsoleReady;
		bool m_bLogingEnabled;
		bool m_bPrintTime;
		bool m_bFadingIn, m_bFadingOut;
		bool m_bAnimationFinishing;
		bool m_bIsActive;
		TextureElement m_pTexBackGround;
		std::fstream m_Fout;

		std::list<string>::iterator m_CurrentCommand;
		std::list<string> m_History;
	public:
		void insertToHUDConsole( std::string text );
		void logMessage( std::string text );
		void print( std::string text );
		void command( std::string command );
		bool setLogingFile( const char *filename );
		bool startLoging();
		void stopLoging();
		bool getLoging() { return m_bLogingEnabled; }
		SystemConsole();
		~SystemConsole();

		//HUD console specification
		void resetHistoryPointer();
		void addCommand( std::string sCommand );
		void createHUDConsole( float width, float height );
		void user_run();
		void user_draw();
		void HUDConsole_use_this_draw();
		void HUDConsole_use_this_run();
		void HUDConsole_SDL_controller( SDL_Event &event );
		void destroy();
		void fadeinAnimated( float value = GFG_MAINWINDOW_FADE_IN_VALUE );
		void fadeoutAnimated( float value = GFG_MAINWINDOW_FADE_OUT_VALUE );
		void fadeSwitch();
		bool isActive()	{ return m_bIsActive; }
	};
	
	const char endline = '\n';			//pouhe pojmenovani mezery
	extern Console console;				//objekt pro retezeni textu
	extern SystemConsole sys_console;	//objekt s funkcemi pro praci s konzoli
};


#endif /*SYS_CONSOLE_H_*/

