/*
______________________________
|        Te Qui La ?         |
|	        Gui              |============\
|  Credits :                 |++++++++++++ \
|   -Anubis82                |==============\
|   -Scotch(Scotchy)         |
|   -WarMuP                  |==============/
|   -Game-Deception          |+++++++++++++/
|____________________________|============/

*/

/*-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-*/
#include "../Cvar.h"
#include "Gui.h"
#include "../Gui/CGUI.h"
/*-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-*/
Gui::Gui()
{
	this->gp_Mouse = new CMouse();
}

Gui::~Gui()
{
	delete this->gp_Mouse;
	delete this->Defaut;
	m_Sprite->Release();
}

void Gui::PostInitialize()
{
	InitalizeCvars();
	LoadCvars();

	D3DXCreateSprite( gpGui->m_pDevice, &m_Sprite );

	this->LoadTextures();

	this->Defaut = new C_SilentFont( gpGui->m_pDevice, "Arial", 14, FW_SEMIBOLD, FALSE );

	this->AddWindow( CPos(100,200), 200, 250, "My Window 1", "MyWindow1");
	this->AddWindow( CPos(500,200), 320, 340, "Another one 2", "MyWindow2");
	this->AddWindow( CPos(500,200), 150, 250, "Sample 3", "MyWindow3");

	this->m_Windows[0]->AddElement(new CLabel(30,70,"Label",				"W0L0"));
	this->m_Windows[0]->AddElement(new CCheckBox(10,10,"Check 1","aim",			"W0C0"));
	this->m_Windows[0]->AddElement(new CCheckBox(10,30,"Check 2","recoil",		"W0C1"));
	this->m_Windows[0]->AddElement(new CCheckBox(10,50,"Check 3","nospread",	"W0C2"));

	this->m_Windows[1]->AddElement(new CLabel(5,10,"Press   [UpArrow]       to Save vars.",			"W1L0"));
	this->m_Windows[1]->AddElement(new CLabel(5,30,"Press   [DownArrow]   to move ProgressBar.",	"W1L1"));
	this->m_Windows[1]->AddElement(new CProgressBar(5,50,250,250,									"W1PB0"));
	this->m_Windows[1]->AddElement(new CRadioButton(10,120,"Single Radio","autopistol",1,			"W1R0"));



	//Too lazy to do : CRadioGroup Class (X, Y, W, H, string Text[], string varname, float val[], string name);
	this->m_Windows[1]->AddElement(new CGroupBox(10,150,200,50, "GroupRadio", "W1GB0"));
	this->m_Windows[1]->AddElement(new CRadioButton(25,170,"Rad 1","spot",1,				"W1aimR0"));
	this->m_Windows[1]->AddElement(new CRadioButton(80,170,"Rad 2","spot",2,			"W1aimR1"));
	this->m_Windows[1]->AddElement(new CRadioButton(140,170,"Rad 3","spot",3,			"W1aimR1"));

}

void Gui::Draw()
{
	if(this->GetVisible())
	{
		for(unsigned int i=0; i < this->m_Windows.size(); i++)
		{
			if( this->m_Windows[i]->GetVisible() )
				this->m_Windows[i]->Draw();
		}
	}
}


bool Gui::GetVisible()
{
	return this->m_Visible;
}

void Gui::SetVisible( bool State )
{
	this->m_Visible = State;
}

void Gui::HandleMessage( unsigned int uMsg, WPARAM wParam, LPARAM lParam )
{
	this->gp_Mouse->HandleMessage( uMsg, wParam, lParam );
}

void Gui::MouseMove( CMouse *Mouse )
{
	CElement *isDragging = this->getMouse()->GetDragging();

	if(!isDragging)
	{
		bool GotWin = false;

		for( int i=this->m_Windows.size()-1; i >= 0; i-- )
		{
			if( this->m_Windows[i]->GetVisible() )
			{
				int h;
				if( this->m_Windows[i]->GetMini() )
					h = TitlebarH;
				else
					h = this->m_Windows[i]->GetHeight();

				if( !GotWin && Mouse->IsInArea( this->m_Windows[i], h ) )
				{
					this->m_Windows[i]->MouseMove( Mouse );
					GotWin = true;
				}
				else
				{
					Mouse->Save();
					Mouse->SetPos( CPos(-1 , -1) );
					this->m_Windows[i]->MouseMove( Mouse );
					Mouse->Load();
				}
			}
		}
	}else
		isDragging->MouseMove( Mouse );
}

void Gui::KeyEvent( SKey Key )
{
	if( !Key.m_Key )
	{
		if( !Key.m_Down )
		{
			for( int i=0; i < this->m_Windows.size(); i++ )
			{
				if( this->m_Windows[i]->GetVisible() )
					this->m_Windows[i]->KeyEvent( Key );
			}
		}
		else
		{
			vector<CWindow*> vRepeat;
			bool bTop = false;

			for( int i=this->m_Windows.size()-1; i >= 0; i-- )
			{
				if( this->m_Windows[i]->GetVisible() )
				{
					if( !bTop )
					{
						int h;
						if( this->m_Windows[i]->GetMini() )
							h = TitlebarH;
						else
							h = this->m_Windows[i]->GetHeight();

						if( this->getMouse()->IsInArea( this->m_Windows[i], h ) && !bTop )
						{
							this->m_Windows[i]->KeyEvent( Key );
							bTop = true;
						}
						else
							vRepeat.push_back( this->m_Windows[i] );
					}
					else
					{
						this->getMouse()->Save();
						this->getMouse()->SetPos( CPos( -1, -1 ) );
						this->m_Windows[i]->KeyEvent( Key );
						this->getMouse()->Load();
					}
				}
			}

			for( unsigned int i = 0; i < vRepeat.size(); i++ )
			{
				this->getMouse()->Save();
				this->getMouse()->SetPos( CPos( -1, -1 ) );
				vRepeat[i]->KeyEvent( Key );
				this->getMouse()->Load();
			}
		}
	}//else{ Do keyboard stuff
}

void Gui::FocusWindow( CWindow * Window )
{
	for( unsigned int i=0; i < this->m_Windows.size(); i++ )
		if( this->m_Windows[i] == Window )
			this->m_Windows.erase( this->m_Windows.begin() + i );
	this->m_Windows.insert(  this->m_Windows.end(), Window );
}

void Gui::AddWindow( CPos Abs, int W, int H, std::string Title, std::string Name )
{
	this->m_Windows.push_back( new CWindow(Abs, CPos(0,0) , W, H, Title, Name) );
}

CWindow *Gui::GetWindowByName( string Name )
{
	for( unsigned int i=0; i < this->m_Windows.size(); i++ )
		if( this->m_Windows[i]->GetName().compare( Name ) == 0 )
			return this->m_Windows[i];
}

CTexture *DrawingElementState::GetTexture( std::string String ) 
{ 	
		CTexture *pRet = m_Textures[String];
		if( !pRet )
			pRet = pParent->m_States[pParent->m_DefaultState]->m_Textures[String];

		return pRet;
}

void Gui::LoadTextures()
{

	Window = new DrawingElement();
	Window->m_DefaultState = "Norm";
	DrawingElementState	*	WindowText = Window->m_States["Pressed"]		=	new DrawingElementState();
								WindowText->m_Textures["WindowMin"]			=	new CTexture(myGui->m_Sprite, "Images/Min1.png");
								WindowText->m_Textures["WindowClose"]		=	new CTexture(myGui->m_Sprite, "Images/Close.png");
							WindowText = Window->m_States["Norm"]			=	new DrawingElementState();
								WindowText->m_Textures["WindowClose"]		=	new CTexture(myGui->m_Sprite, "Images/Close.png");
								WindowText->m_Textures["WindowMin"]			=	new CTexture(myGui->m_Sprite, "Images/Min0.png");
	//--------------------------------
	CheckBox = new DrawingElement();
	CheckBox->m_DefaultState = "Norm";
	DrawingElementState *		CheckBoxText = CheckBox->m_States["Pressed"]	=	new DrawingElementState();
									CheckBoxText->m_Textures["CheckBox"]		=	new CTexture(myGui->m_Sprite, "Images/CheckBox1.png");
								CheckBoxText = CheckBox->m_States["Norm"]		=	new DrawingElementState();
									CheckBoxText->m_Textures["CheckBox"]		=	new CTexture(myGui->m_Sprite, "Images/CheckBox0.png");
								CheckBoxText = CheckBox->m_States["MouseOver"]	=	new DrawingElementState();
									CheckBoxText->m_Textures["CheckBox"]		=	new CTexture(myGui->m_Sprite, "Images/CheckBox2.png");
	//--------------------------------
	Progress = new DrawingElement();
	Progress->m_DefaultState = "Norm";
	DrawingElementState *		ProgressText = Progress->m_States["Norm"]		=	new DrawingElementState();
									ProgressText->m_Textures["Fond"]			=	new CTexture(myGui->m_Sprite, "Images/LoadBar0.png");
									ProgressText->m_Textures["Progress"]		=	new CTexture(myGui->m_Sprite, "Images/LoadBar1.png");
									ProgressText->m_Textures["Floating"]		=	new CTexture(myGui->m_Sprite, "Images/LoadBar2.png");
	//--------------------------------
	Radio = new DrawingElement();
	Radio->m_DefaultState = "Norm";
	DrawingElementState *		RadioText = Radio->m_States["Pressed"]		=	new DrawingElementState();
									RadioText->m_Textures["Radio"]			=	new CTexture(myGui->m_Sprite, "Images/Radio1.png");
								RadioText = Radio->m_States["Norm"]			=	new DrawingElementState();
									RadioText->m_Textures["Radio"]			=	new CTexture(myGui->m_Sprite, "Images/Radio0.png");
								RadioText = Radio->m_States["MouseOver"]	=	new DrawingElementState();
									RadioText->m_Textures["Radio"]			=	new CTexture(myGui->m_Sprite, "Images/Radio1.png");


}