#include "MenuManager.h"

#include "Menu.h"
#include "Label.h"
#include "Image.h"
#include "Panel.h"
#include "Console.h"

//MenuManager::MenuManager(DeviceStateManager *device,CDIFunctions* inputdevice,SoundDevice* sounddevice)
MenuManager::MenuManager(DeviceStateManager *device,CDIFunctions* inputdevice)
{
	m_Device = device;
	m_InputDevice = inputdevice;
	//m_SoundDevice = sounddevice;
	m_Console = NULL;

	m_VB = m_Device->GetBufferManager()->CreateDynamicVB(2048);
	m_IB = m_Device->GetBufferManager()->CreateDynamicIB(2048, false);

	m_Shader = device->GetShaderManager()->CreateFX("Shaders\\Menu.fx");

	D3DVERTEXELEMENT9 VDeclTex[] =
    { 
        { 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },  
        { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
    };
    m_Device->GetDevice()->CreateVertexDeclaration(VDeclTex,&m_VDeclTex);

	D3DVERTEXELEMENT9 VDeclColor[] =
    { 
        { 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		D3DDECL_END()
    };
    m_Device->GetDevice()->CreateVertexDeclaration(VDeclColor,&m_VDeclColor);

	m_MouseOver = NULL;
	m_DragStart = NULL;
	m_DragButton = MBTN_LEFT;
}

MenuManager::~MenuManager(void)
{
	m_Menus.Release();
	FOREACH(el, m_CreatedElements, GUIElement)
	{
		el->GetInfo()->RemoveAllChilds();
	}
	m_CreatedElements.Clear();
	SAFERELEASE(m_Shader);
	SAFERELEASE(m_VDeclTex);
	SAFERELEASE(m_VDeclColor);
	SAFERELEASE(m_IB);
	SAFERELEASE(m_VB);
	delete m_Console;
}

void MenuManager::Update(void)
{
	m_MouseOver = NULL;
	FOREACH(me,m_Menus,Menu)
	{
		me->GetInfo()->Update();
	}

	if(m_Console)
		m_Console->Update();

	if (m_MouseOver)
		m_MouseOver->UpdateMouseOver();

	if(m_InputDevice && m_InputDevice->GetMouseButton(m_DragButton) & BUTTONUP)
		m_DragStart = NULL;
}

void MenuManager::Draw(void)
{
	FOREACH(me,m_Menus,Menu)
	{
		me->GetInfo()->Draw();
	}
	if(m_Console)
		m_Console->Draw();
}

void MenuManager::AddElement(GUIElement* const guiel)
{
	m_CreatedElements.AddTail(guiel, 0);
}

void MenuManager::DeleteElement(GUIElement* const guiel)
{
	if (guiel->GetParent())
	{
		guiel->GetParent()->RemoveChild(guiel);
//		guiel->SetParent(NULL);
	}
	m_CreatedElements.DeleteElement(guiel);
}

void MenuManager::AddMenu(Menu*m)
{
	if (m)
		m_Menus.AddTail(m, m->GetID());
}

void MenuManager::RemoveMenu(Menu*m)
{
	ListElement<Menu>* el = m_Menus.GetElement(m);
	if(el)
	{
		el->Unlink();
		delete el;
	}
}

void MenuManager::SetConsole(Console*console)
{
	delete m_Console;
	m_Console = console;
}

void MenuManager::SetupRenderTex(const bool AlphaTest, const bool AlphaBlend, const DXTexture* Texture)
{
	if(!m_Shader->IsCompiled())
		return;
	m_Device->SetVertexDeclaration(m_VDeclTex);
	m_Shader->SetTechnique("RenderTex");
	m_Shader->GetShader()->SetBool("AlphaTest", AlphaTest);
	m_Shader->GetShader()->SetBool("AlphaBlend", AlphaBlend);
	m_Shader->SetTexture("Texture", Texture);
	m_Shader->SetVector("Color", DXVector4(1,1,1,1));
}

void MenuManager::SetupRenderColor(const bool AlphaTest, const bool AlphaBlend, const D3DXCOLOR &Color)
{
	if(!m_Shader->IsCompiled())
		return;
	m_Device->SetVertexDeclaration(m_VDeclColor);
	m_Shader->SetTechnique("RenderColor");
	m_Shader->GetShader()->SetBool("AlphaTest", AlphaTest);
	m_Shader->GetShader()->SetBool("AlphaBlend", AlphaBlend);
	m_Shader->GetShader()->SetFloatArray("Color", Color,4);
}
