#include "Menu.h"

CMenu::CMenu(IEngineCore *pEngineCore):
_pEngineCore(pEngineCore), _uiCounter(0), _bCanPress(true)
{
	_pEngineCore->AddProcedure(EPT_PROCESS, &_s_Process, this);
	_pEngineCore->AddProcedure(EPT_RENDER, &_s_Render, this);

	IRender *p_render;
	pEngineCore->GetSubSystem(ESS_RENDER, (IEngineSubSystem *&)p_render);
	p_render->GetRender2D(_pRender2D);

	pEngineCore->GetSubSystem(ESS_INPUT, (IEngineSubSystem *&)_pInput);

	IExtensionPlugin *ext_plugin;
	pEngineCore->GetPlugin("DGLE2_EXT",(IPlugin *&)ext_plugin);
	ext_plugin->GetGuiEngine(_pGUIEngine);

	_pMainFont = (IBitmapFont*)mapResources["MainFont"];
	_pBaseFont = (IBitmapFont*)mapResources["BaseFont"];
	_pGUIForms = (IGUIForms*)mapResources["Forms"];
	_pTexBg = (ITexture*)mapResources["TexBlockDiff"];
	_pTexCur = (ITexture*)mapResources["Cursor"];

	_pGUIForms->GetFormFromName("MainMenu", _pMainForm);
	_pGUIEngine->AddForm(_pMainForm);

	_pGUIForms->GetFormFromName("FormOptions", _pOptionsForm);
	_pGUIForms->GetFormFromName("FormHScore", _pHSForm);

	TPoint2 d;
	_pMainForm->GetDimension(d);

	TEngWindow win;
	_pEngineCore->GetCurrentWin(win);

	_uiResX = win.uiWidth;
	_uiResY = win.uiHeight;

	_pMainForm->SetPosition(TPoint2(_uiResX - d.x - 10, (_uiResY - d.y)/2));
	
	
	_pOptionsForm->GetDimension(d);
	_pOptionsForm->SetPosition(TPoint2((_uiResX - d.x)/2.f, (_uiResY - d.y)/2.f));

	_pHSForm->GetDimension(d);
	_pHSForm->SetPosition(TPoint2((_uiResX - d.x)/2.f, (_uiResY - d.y)/2.f));

	static bool first_start = true;

	if(first_start)
	{
		IDCButton *p_btn;
		_pMainForm->GetComponent("ButtonPlay", (IDCComponent*&)p_btn);
		p_btn->AddEventListner(GCE_ON_CLICK, &_s_StartClick);
		
		_pMainForm->GetComponent("ButtonQuit", (IDCComponent*&)p_btn);
		p_btn->AddEventListner(GCE_ON_CLICK, &_s_QuitClick);
		
		_pMainForm->GetComponent("ButtonOptions", (IDCComponent*&)p_btn);
		p_btn->AddEventListner(GCE_ON_CLICK, &_s_OptionsClick);

		_pMainForm->GetComponent("ButtonHighScore", (IDCComponent*&)p_btn);
		p_btn->AddEventListner(GCE_ON_CLICK, &_s_HSClick);
		
		_pOptionsForm->GetComponent("ButtonOk", (IDCComponent*&)p_btn);
		p_btn->AddEventListner(GCE_ON_CLICK, &_s_OkOptionsClick);

		_pHSForm->GetComponent("ButtonOk", (IDCComponent*&)p_btn);
		p_btn->AddEventListner(GCE_ON_CLICK, &_s_OkHSClick);

		IDCListBox *pl;
		_pHSForm->GetComponent("ListBox1", (IDCComponent*&)pl);

		pl->Clear();
		for(int i = 0; i < 100; i++)
			pl->AddItem((IntToStr(i+1) + " Unknown " + IntToStr(1000*100 - i*100)).c_str());

		//_pHSForm->GetComponent("Clear", (IDCComponent*&)p_btn);
		//p_btn->AddEventListner(GCE_ON_CLICK, &_s_HSClearClick);

		first_start = false;
	}
}

CMenu::~CMenu()
{
	_pEngineCore->RemoveProcedure(EPT_PROCESS, &_s_Process, this);
	_pEngineCore->RemoveProcedure(EPT_RENDER, &_s_Render, this);

	_pGUIEngine->RemoveForm(_pMainForm);
	//_pBtnStart->RemoveEventListner
}

void CMenu::_Process()
{
	_uiCounter++;
}

void CMenu::_Render()
{
	_pRender2D->Begin2D();
	
	_pTexBg->Draw2D(0, 0, _uiResX, _uiResY);

	uint w, h;
	_pMainFont->SetScale(2.f);
	_pMainFont->GetTextDimensions(APP_CAPTION, w, h);
	_pMainFont->Draw2D(0 - w/2 + h/2, (_uiResY - w)/2 + w/2 - h/2, APP_CAPTION, TColor4(), 90);

	_pBaseFont->GetTextDimensions("(C)DRON  2011    http://dron.deeprosoft.com/    V 1.0  Beta", w, h);
	_pBaseFont->Draw2D((_uiResX - w)/2, _uiResY - h - 5, "(C)DRON  2011    http://dron.deeprosoft.com/    V 1.0  Beta");

	_pGUIEngine->Draw();

	TMouseStates ms;
	_pInput->GetMouseStates(ms);
	_pRender2D->DrawSpriteA(_pTexCur, TPoint2(ms.iX, ms.iY), TPoint2(20, 20), _uiCounter/2 % 11);

	_pRender2D->End2D();
}

void CMenu::_StartClick()
{
	if(!_bCanPress)
		return;

	StartScene(GS_GAME);
}

void CMenu::_OptionsClick()
{
	if(!_bCanPress)
		return;
	_bCanPress = false;

	IDCComboBox *p_cmbx;
	_pOptionsForm->GetComponent("ComboBoxRes", (IDCComponent*&)p_cmbx);

	switch(_uiResX*_uiResY)
	{
	case 800*600: p_cmbx->SetSelectedIndex(0); break;
	case 1024*768: p_cmbx->SetSelectedIndex(1); break;
	case 1280*1024: p_cmbx->SetSelectedIndex(2); break;
	case 1600*1200: p_cmbx->SetSelectedIndex(3); break;
	case 1280*720: p_cmbx->SetSelectedIndex(4); break;
	case 1366*768: p_cmbx->SetSelectedIndex(5); break;
	case 1280*800: p_cmbx->SetSelectedIndex(6); break;
	case 1440*900:p_cmbx->SetSelectedIndex(7); break;
	case 1680*1050:p_cmbx->SetSelectedIndex(8); break;
	case 1920*1080:p_cmbx->SetSelectedIndex(9); break;
	case 1920*1200:p_cmbx->SetSelectedIndex(10); break;
	}

	IDCCheckBox *p_chbx;
	_pOptionsForm->GetComponent("CheckBoxFscr", (IDCComponent*&)p_chbx);
	p_chbx->SetChecked(GetPrivateProfileIntA("System", "FScreen", 1, ini.c_str()));

	_pOptionsForm->GetComponent("CheckBoxQuality", (IDCComponent*&)p_chbx);
	p_chbx->SetChecked(GetPrivateProfileIntA("System", "HighDetail", 1, ini.c_str()));

	IDCTrackBar *p_pgrs;

	_pOptionsForm->GetComponent("TrackBarMusic", (IDCComponent*&)p_pgrs);
	p_pgrs->SetValue(GetPrivateProfileIntA("System", "MusicVolume", 80, ini.c_str()));

	_pOptionsForm->GetComponent("TrackBarSound", (IDCComponent*&)p_pgrs);
	p_pgrs->SetValue(GetPrivateProfileIntA("System", "SoundVolume", 100, ini.c_str()));

	_pGUIEngine->AddForm(_pOptionsForm);
}

void CMenu::_OptionsOk()
{
	_bCanPress = true;
	_pGUIEngine->RemoveForm(_pOptionsForm);

	IDCComboBox *p_cmbx;
	_pOptionsForm->GetComponent("ComboBoxRes", (IDCComponent*&)p_cmbx);

	int idx;
	p_cmbx->GetSelectedIndex(idx);
	switch(idx)
	{
	case 0: WritePrivateProfileStringA("System", "ResX", "800", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "600", ini.c_str()); break;
	case 1: WritePrivateProfileStringA("System", "ResX", "1024", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "768", ini.c_str()); break;
	case 2: WritePrivateProfileStringA("System", "ResX", "1280", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "1024", ini.c_str()); break;
	case 3: WritePrivateProfileStringA("System", "ResX", "1600", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "1200", ini.c_str()); break;
	case 4: WritePrivateProfileStringA("System", "ResX", "1280", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "720", ini.c_str()); break;
	case 5: WritePrivateProfileStringA("System", "ResX", "1366", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "768", ini.c_str()); break;
	case 6: WritePrivateProfileStringA("System", "ResX", "1280", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "800", ini.c_str()); break;
	case 7: WritePrivateProfileStringA("System", "ResX", "1440", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "900", ini.c_str()); break;
	case 8: WritePrivateProfileStringA("System", "ResX", "1680", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "1050", ini.c_str()); break;
	case 9: WritePrivateProfileStringA("System", "ResX", "1920", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "1080", ini.c_str()); break;
	case 10: WritePrivateProfileStringA("System", "ResX", "1920", ini.c_str()); WritePrivateProfileStringA("System", "ResY", "1200", ini.c_str()); break;
	}

	bool b;
	IDCCheckBox *p_chbx;
	_pOptionsForm->GetComponent("CheckBoxFscr", (IDCComponent*&)p_chbx);
	p_chbx->GetChecked(b);
	WritePrivateProfileStringA("System", "FScreen", b ? "1" : "0", ini.c_str());

	_pOptionsForm->GetComponent("CheckBoxQuality", (IDCComponent*&)p_chbx);
	p_chbx->GetChecked(b);
	WritePrivateProfileStringA("System", "HighDetail", b ? "1" : "0", ini.c_str());
	if(!b)
	{
		WritePrivateProfileStringA("System", "PostProcess", "0", ini.c_str());
		WritePrivateProfileStringA("System", "MSAA", "1", ini.c_str());
	}
	else
		WritePrivateProfileStringA("System", "MSAA", "4", ini.c_str());

	IDCTrackBar *p_pgrs;

	_pOptionsForm->GetComponent("TrackBarMusic", (IDCComponent*&)p_pgrs);
	p_pgrs->GetValue(idx);

	WritePrivateProfileStringA("System", "MusicVolume", IntToStr(idx).c_str(), ini.c_str());

	_pOptionsForm->GetComponent("TrackBarSound", (IDCComponent*&)p_pgrs);
	p_pgrs->GetValue(idx);

	WritePrivateProfileStringA("System", "SoundVolume", IntToStr(idx).c_str(), ini.c_str());

	TEngWindow w;
	w.uiWidth = GetPrivateProfileIntA("System", "ResX", 1024, ini.c_str());
	w.uiHeight = GetPrivateProfileIntA("System", "ResY", 768, ini.c_str());
	w.bFullScreen = GetPrivateProfileIntA("System", "FScreen", 1, ini.c_str());
	w.bVSync = GetPrivateProfileIntA("System", "VSync", 0, ini.c_str());
	w.uiSamplesCount = GetPrivateProfileIntA("System", "MSAA", 4, ini.c_str());

	((IMusic*)mapResources["Music"])->SetVolume(GetPrivateProfileIntA("System", "MusicVolume", 80, ini.c_str()));

	ISound *p_snd;
	_pEngineCore->GetSubSystem(ESS_SOUND, (IEngineSubSystem *&)p_snd);

	p_snd->SetMasterVolume(GetPrivateProfileIntA("System", "SoundVolume", 100, ini.c_str()));

	_pEngineCore->ChangeWinMode(w);
	_pEngineCore->GetCurrentWin(w);


	_uiResX = w.uiWidth;
	_uiResY = w.uiHeight;

	TPoint2 d;
	_pMainForm->GetDimension(d);
	_pMainForm->SetPosition(TPoint2(_uiResX - d.x - 10, (_uiResY - d.y)/2));
	
	_pOptionsForm->GetDimension(d);
	_pOptionsForm->SetPosition(TPoint2((_uiResX - d.x)/2.f, (_uiResY - d.y)/2.f));

	_pHSForm->GetDimension(d);
	_pHSForm->SetPosition(TPoint2((_uiResX - d.x)/2.f, (_uiResY - d.y)/2.f));
}

void CMenu::_HS()
{
	if(!_bCanPress)
		return;
	_bCanPress = false;
	_pGUIEngine->AddForm(_pHSForm);
}

void CMenu::_HSClear()
{
	_bCanPress = true;
}

void CMenu::_OkHS()
{
	_bCanPress = true;
	_pGUIEngine->RemoveForm(_pHSForm);
}

void CALLBACK CMenu::_s_Process(void *pParametr)
{
	((CMenu*)pParametr)->_Process();
}

void CALLBACK CMenu::_s_Render(void *pParametr)
{
	((CMenu*)pParametr)->_Render();
}

extern CMenu *pMenu;

void CALLBACK /*CMenu::*/_s_StartClick(void *pSelf, void *pParametr)
{
	pMenu->_StartClick();
	//((CMenu*)pParametr)->_StartClick();
}

void CALLBACK _s_QuitClick(void *pSelf, void *pParametr)
{
	extern IEngineCore	*pEngineCore;
	pEngineCore->QuitEngine();
}

void CALLBACK  _s_OptionsClick(void *pSelf, void *pParametr)
{
	pMenu->_OptionsClick();

}

void CALLBACK  _s_OkOptionsClick(void *pSelf, void *pParametr)
{
	pMenu->_OptionsOk();

}

void CALLBACK _s_HSClick(void *pSelf, void *pParametr)
{
	pMenu->_HS();
}

void CALLBACK _s_HSClearClick(void *pSelf, void *pParametr)
{
	pMenu->_HSClear();
}

void CALLBACK _s_OkHSClick(void *pSelf, void *pParametr)
{
	pMenu->_OkHS();
}