#include "RuntimeData.h"
#include "LanguageCfg.h"
#include "DevMask.h"

#define dInteval	88

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

static constexpr short svGridX[10] = {
6, 
6 + (dInteval * 1) ,
6 + (dInteval * 2) ,
6 + (dInteval * 3) ,
6 + (dInteval * 4) ,
6 + (dInteval * 5) ,
6 + (dInteval * 6) ,
6 + (dInteval * 7) ,
6 + (dInteval * 8) ,
6 + (dInteval * 9) 
};

void TLanguageCfg::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "本机->语言/Language");
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +10, clNearWhite,  "语言/Language");
		TextRender_string24(svGridX[0] +30, Lv.Grid.Top[0] +46, clNearWhite,  "中文");
		TextRender_string24(svGridX[1] +30, Lv.Grid.Top[0] +46, clNearWhite,  "英文/Language");
		TRoundRect::sDrawBySize(svGridX[0] +6, Lv.Grid.Top[1] + 14, 76, 36, 7, clNearWhite, "保存", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Local->Language/语言");
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +10, clNearWhite,  "Language/语言");
		TextRender_string24(svGridX[0] +30, Lv.Grid.Top[0] +46, clNearWhite,  "CN");
		TextRender_string24(svGridX[1] +30, Lv.Grid.Top[0] +46, clNearWhite,  "English/英文");
		TRoundRect::sDrawBySize(svGridX[0] +6, Lv.Grid.Top[1] + 14, 76, 36, 7, clNearWhite, "Save", 14, clNearBlack);
	}
}

void TLanguageCfg::Init()
{
	Tag.Group = 0;
	Tag.Inx0 = 0;
	Tag.Inx1 = 0;
	IsPermission =0;
	
	SettingEnCn = 0;
	
	SaveResult = 0;
}

void TLanguageCfg::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TLanguageCfg::RenderBackGround(unsigned int aClr)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, aClr);
}

void TLanguageCfg::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y, y2, clr;
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);	
}

void TLanguageCfg::DrawEnCn()
{
	if(SettingEnCn == 0){
		TRoundSquare::sDrawBySize(svGridX[0] +2, Lv.Grid.Top[0] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
		TRoundSquare::sDrawBySize(svGridX[1] +2, Lv.Grid.Top[0] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}else{
		TRoundSquare::sDrawBySize(svGridX[0] +2, Lv.Grid.Top[0] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
		TRoundSquare::sDrawBySize(svGridX[1] +2, Lv.Grid.Top[0] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}	
}

void TLanguageCfg::DrawSelf(void)
{
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);

	DrawFixText();
	LoadSysCfg();
	DrawEnCn();
}

void TLanguageCfg::Show()
{
	RenderBackGround();
	DrawSelf();
	DrawSelected(1);
}

void TLanguageCfg::DrawTip()
{
	
}

void TLanguageCfg::FullRedraw(int Prm)
{
	if(Prm == 0){
		Show();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		DrawSelf();
		DrawSelected(1);
		
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			RectFillRender(Lv.Grid.Left[2] + 180, Lv.Grid.Top[2] + 20, 440, 160, ContentColor);
		}
	}
}

void TLanguageCfg::PermissionCmdReturnDraw()
{
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	RectFillRender(Lv.Grid.Left[2] + 180, Lv.Grid.Top[2] + 20, Lv.Grid.Left[2] + 180 + 440, Lv.Grid.Top[2] + 20 + 180, ContentColor);
	DrawFixText();
	DrawEnCn();
}

void TLanguageCfg::PermissionCmdGO()
{
	//Save Data Here
	GoResult();
}
			
void TLanguageCfg::ShowSaveResult()
{
	char *tText;
	if(SaveResult == 0){
		TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ContentColor, "                ");
	}else
	if(SaveResult < 0){
		if(LanguageEnCn==0)tText=" 保存失败 "; else tText="Failed To Save";
		TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ContentColor, tText);
	}else{
		if(LanguageEnCn==0)tText=" 保存成功 "; else tText="Saved OK";
		TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ContentColor, tText);
	}
}

void TLanguageCfg::ShowSaveResult(int aNewResult)
{
	SaveResult = aNewResult;
	ShowSaveResult();
}

void TLanguageCfg::LoadSysCfg()
{
	SettingEnCn = LanguageEnCn;
}

int TLanguageCfg::GoResult()
{
	Record.WriteLanguage(SettingEnCn);
	Record.ReadLanguage();
	if(SettingEnCn == LanguageEnCn ){
		ShowSaveResult(1);
	}else{
		ShowSaveResult(-1);
	}
	return 0;
}

void TLanguageCfg::sRecoverSetDefault()
{
	Record.WriteLanguage(0);
	Record.ReadLanguage();	
}

void TLanguageCfg::DrawSelected(int IsSelected)
{
	unsigned int tClr;
	char *tText;
	if(Tag.Group == 0){
		tClr = IsSelected ? clNearWhite : ItemColor;
		if(Tag.Inx0 == 0)
			TRoundSquare::sSelected(svGridX[0] +2, Lv.Grid.Top[0] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx0 == 1)
			TRoundSquare::sSelected(svGridX[1] +2, Lv.Grid.Top[0] + 46, dWWidth, dWHeigh, 4, tClr);	
	}else
	if(Tag.Group == 1){
		if(Tag.Inx1 == 0){
			if(LanguageEnCn==0) tText="保存"; else tText="Save";
			IsSelected ?
			TRoundRect::sDrawBySize(svGridX[0] +6 , Lv.Grid.Top[1] + 14, 76, 36, 7, clDeepBlue, tText, 14, clNearWhite):
			TRoundRect::sDrawBySize(svGridX[0] +6, Lv.Grid.Top[1] + 14, 76, 36, 7, clNearWhite, tText, 14, clNearBlack);
		}
	}
}

void TLanguageCfg::ChangeValue()
{
	if(Tag.Group == 0){
		if(Tag.Inx0 == 0)
			SettingEnCn = SettingEnCn ? 0 : 1;
		else if(Tag.Inx0 == 1)
			SettingEnCn = SettingEnCn ? 0 : 1;	
	}
}

TGuiMsgReturn TLanguageCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}else{
		if(aKey == VK_LEFT){
			if(Tag.Group == 0){
				if(Tag.Inx0 >0){
					DrawSelected(0);
					Tag.Inx0 --;
					DrawSelected(1);
				}
			}else
			if(Tag.Group == 1){
				if(Tag.Inx1 >0){
					DrawSelected(0);
					Tag.Inx1 --;
					DrawSelected(1);
				}
			}
		}else
		if(aKey == VK_RIGHT){
			if(Tag.Group == 0){
				if(Tag.Inx0 <1){
					DrawSelected(0);
					Tag.Inx0 ++;
					DrawSelected(1);
				}
			}else
			if(Tag.Group == 1){
				if(Tag.Inx1 <7){
					DrawSelected(0);
					Tag.Inx1 ++;
					DrawSelected(1);
				}
			}
		}else
		if(aKey == VK_UP){
			if(Tag.Group == 0){

			}else
			if(Tag.Group == 1){
				DrawSelected(0);
				Tag.Group = 0;
				if(Tag.Inx0 >1)Tag.Inx0 = 1;
				DrawSelected(1);
			}
		}else
		if(aKey == VK_DOWN){
			if(Tag.Group == 0){
				DrawSelected(0);
				Tag.Group = 1;
				if(Tag.Inx1 >7)Tag.Inx1 =7;
				DrawSelected(1);
			}else
			if(Tag.Group == 1){
			}
		}else
		if(aKey == VK_F1){
			ChangeValue();
			DrawEnCn();
		}else
		if(aKey == VK_EXECUTE){
			if(SystemPermission > 1){
				PermissionCmdGO();
			}else{
				IsPermission = 1;
				PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[2] + 200, Lv.Grid.Top[2] + 40, 400, 140, 2);
			}
		}else
		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}

		if( (aKey != VK_EXECUTE) && (aKey != VK_RETURN) ){
			if(SaveResult != 0)ShowSaveResult(0);
		}		
	}
	return aMsg;	
}



















