#include "ScreenSaveCfg.h"
#include "Runtime.h"

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

void TScreenSaveCfg::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "本机->屏保设置");
		TextRender_string24(Lv.Grid.Left[0] + 52, Lv.Grid.Top[0] +10, clNearWhite,  "屏保设置");
		TextRender_string24(Lv.Grid.Left[0] + 52, Lv.Grid.Top[0] +10, clNearWhite,  "屏保设置");
		TextRender_string24(Lv.Grid.Left[0] +52, Lv.Grid.Top[0] +50, clNearWhite,   "启用屏保");
		TextRender_string24(Lv.Grid.Left[0] +52, Lv.Grid.Top[0] +90, clNearWhite, ItemColor, "屏保时间");
		TextRender_string24(Lv.Grid.Left[0] +200, Lv.Grid.Top[0] +90, clNearWhite, ItemColor, "分钟");
		TextRender_string24(Lv.Grid.Left[1] +52, Lv.Grid.Top[1] +10, clNearWhite, "火警状态时禁止屏保");
		TextRender_string24(Lv.Grid.Left[1] +52, Lv.Grid.Top[1] +50, clNearWhite, "监管状态时禁止屏保");
		TextRender_string24(Lv.Grid.Left[1] +52, Lv.Grid.Top[1] +90, clNearWhite,  "故障状态时禁止屏保");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Local->Screen Safe Set");
		TextRender_string24(Lv.Grid.Left[0] + 52, Lv.Grid.Top[0] +10, clNearWhite,  "Setting");
		TextRender_string24(Lv.Grid.Left[0] +52, Lv.Grid.Top[0] +50, clNearWhite,   "Enable");
		TextRender_string24(Lv.Grid.Left[0] +52, Lv.Grid.Top[0] +90, clNearWhite, ItemColor, "Time ");
		TextRender_string24(Lv.Grid.Left[0] +200, Lv.Grid.Top[0] +90, clNearWhite, ItemColor, "Minute");
		TextRender_string24(Lv.Grid.Left[1] +52, Lv.Grid.Top[1] +10, clNearWhite, "Disable Screen Saver In FireAlarm");
		TextRender_string24(Lv.Grid.Left[1] +52, Lv.Grid.Top[1] +50, clNearWhite, "Disable Screen Saver In Supervision");
		TextRender_string24(Lv.Grid.Left[1] +52, Lv.Grid.Top[1] +90, clNearWhite, "Disable Screen Saver In Fault");
	}
}

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

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

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


void TScreenSaveCfg::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();
	
	//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[0] + 166, 100, 40, 7, clDeepBlue, "保存", 14, clNearWhite);
	vEdit.Init(Lv.Grid.Left[0] +148, Lv.Grid.Top[0] + 84, 50, 32, 0,0,1,ItemColor);
	vEdit.Str.Clear();
	vEdit.MaxLen = 2;
	vEdit.IsShowCursor = 1;
	vEdit.Enable = 1;
	vEdit.Visible = 1;
	
	vEdit.Str.FromUInt2Dg(Setting.Minute);
	
	Check4Selected();
	
	//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[1] + 166, 100, 40, 7, clDeepBlue, "保存", 14, clNearWhite);
	ShowSetting();
}

unsigned int TScreenSaveCfg::GetTextBkgColor(unsigned char aGroup, unsigned char aInx)
{
	unsigned char tInx;
	if(aGroup == 0)tInx = Tag.Inx0;
	else if(aGroup == 1)tInx = Tag.Inx1;
	if( (aGroup == Tag.Group) && (aInx == tInx) ){
		return clNearWhite;
	}else{
		return ItemColor;
	}
}

void TScreenSaveCfg::Show(void)
{	
	RenderBackGround();
	DrawSelf();
}

void TScreenSaveCfg::PermissionCmdReturnDraw()
{
	RectFillRender(Lv.Grid.Left[0] + 200 -10, Lv.Grid.Top[0] + 40 -10, Lv.Grid.Left[0] + 200 -10 + 400 + 20, Lv.Grid.Top[0] + 40 -10 + 140 + 20, ContentColor);

	for(int i=0; i<2; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);

	DrawFixText();
	
	Check4Selected();
	
	//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[1] + 166, 100, 40, 7, clDeepBlue, "保存", 14, clNearWhite);
	ShowSetting();
}

void TScreenSaveCfg::PermissionCmdGO()
{
	//Save Data Here
	SaveData();
}

void TScreenSaveCfg::FullRedraw(int Prm)
{
	if(Prm == 0){
		Show();
		ShowSaveResult();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		DrawSelf();
		ShowSaveResult();
		
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

//______________________________________________________________________________________________________________________________

void TScreenSaveCfg::Check4Selected(void)
{	
	TRoundSquare::sSelected(Lv.Grid.Left[0] + 16, Lv.Grid.Top[0] + 50, dWWidth, dWHeigh, 4, GetTextBkgColor(0,0));
	if( (Tag.Group == 0) && (Tag.Inx0 == 1) ){
		vEdit.Selected =1;
		vEdit.SetPstTail();
		vEdit.Show();
		vEdit.ShowCursor();
	}else{
		vEdit.Selected =0;
		vEdit.Show();
	}
	TRoundSquare::sSelected(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 10, dWWidth, dWHeigh, 4, GetTextBkgColor(1,0));
	TRoundSquare::sSelected(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 50, dWWidth, dWHeigh, 4, GetTextBkgColor(1,1));
	TRoundSquare::sSelected(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 90, dWWidth, dWHeigh, 4, GetTextBkgColor(1,2));
}

void TScreenSaveCfg::ShowSetting()
{
	if(Setting.OnOff){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] + 16, Lv.Grid.Top[0] + 50, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] + 16, Lv.Grid.Top[0] + 50, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Setting.PrvOnFire){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 10, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 10, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Setting.PrvOnSv){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 50, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 50, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Setting.PrvOnFault){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 90, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 16, Lv.Grid.Top[1] + 90, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TScreenSaveCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn MBoxMsg = guiMsgNone;
	int iVal;
	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{
		switch(aKey){
			case VK_DELETE: 
			case  VK_0:	
			case  VK_1:
			case  VK_2:
			case  VK_3:
			case  VK_4:
			case  VK_5:
			case  VK_6:	
			case  VK_7:
			case  VK_8:
			case  VK_9:
			case  VK_LEFT:
			case  VK_RIGHT:
				if( (Tag.Group == 0) && (Tag.Inx0 == 1) ){
					vEdit.KeyIn(aKey);
					vEdit.Show();
					vEdit.ShowCursor();
				}
				break;
			case VK_UP:
				if(Tag.Group == 0){
					if(Tag.Inx0 >0){
						Tag.Inx0--;
						Check4Selected();
					}
				}else
				if(Tag.Group == 1){
					if(Tag.Inx1 >0){
						Tag.Inx1--;
						Check4Selected();
					}else{
						Tag.Group = 0;
						Tag.Inx0 =1;
						Check4Selected();
					}
				}
				break;
			case VK_DOWN:
				if(Tag.Group == 0){
					if(Tag.Inx0 <1){
						Tag.Inx0++;
						Check4Selected();
					}else{
						Tag.Group = 1;
						Tag.Inx1 =0;
						Check4Selected();
					}
				}else
				if(Tag.Group == 1){
					if(Tag.Inx1 <2){
						Tag.Inx1++;
						Check4Selected();
					}
				}
				break;
			case VK_F1:
				if(Tag.Group == 0){
					if(Tag.Inx0 == 0){
						if(Setting.OnOff)Setting.OnOff = 0;
						else Setting.OnOff =1;
					}
				}else
				if(Tag.Group ==1){
					if(Tag.Inx1 == 0){
						if(Setting.PrvOnFire)Setting.PrvOnFire = 0;
						else Setting.PrvOnFire =1;
					}else
					if(Tag.Inx1 == 1){
						if(Setting.PrvOnSv)Setting.PrvOnSv = 0;
						else Setting.PrvOnSv =1;
					}else
					if(Tag.Inx1 == 2){
						if(Setting.PrvOnFault)Setting.PrvOnFault = 0;
						else Setting.PrvOnFault =1;
					}	
				}
				ShowSetting();
				break;
			case VK_EXECUTE:
				if(SystemPermission > 1){
					PermissionCmdGO();
				}else{
					IsPermission = 1;
					PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[0] + 200, Lv.Grid.Top[0] + 40, 400, 140, 2);
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			default:
				break;
		}	
	}
	if(VK_EXECUTE != aKey){
		ShowSaveResult(0);
	}
	return aMsg;	
}

void TScreenSaveCfg::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 TScreenSaveCfg::ShowSaveResult(int aNewResult)
{
	SaveResult = aNewResult;
	ShowSaveResult();
}

void TScreenSaveCfg::LoadData()
{
	Setting.OnOff = ScreenAndKeyLock.SaveOn;
	Setting.Minute = ScreenAndKeyLock.SaveTime;
	vEdit.Str.FromUInt2Dg(Setting.Minute);
	if(ScreenAndKeyLock.IsKeyLock & 0x01)	Setting.PrvOnFire = 1; 		else Setting.PrvOnFire = 0;
	if(ScreenAndKeyLock.IsKeyLock & 0x02)	Setting.PrvOnSv = 1; 		else Setting.PrvOnSv = 0;
	if(ScreenAndKeyLock.IsKeyLock & 0x04)	Setting.PrvOnFault = 1; 	else Setting.PrvOnFault = 0;
}

void TScreenSaveCfg::SaveData()
{
	int iVal;
	unsigned char tOnOff, SettingByte;
	unsigned char tMinute;
	if(Setting.OnOff)tOnOff = 1; else tOnOff = 0;
	iVal = vEdit.Str.ToInteger();
	if(iVal > 20)iVal = 20;
	if(iVal < 1)iVal = 1;
	tMinute = iVal;
	SettingByte = 0;
	if(Setting.PrvOnFire) SettingByte |= 0x01;
	if(Setting.PrvOnSv) SettingByte |= 0x02;
	if(Setting.PrvOnFault) SettingByte |= 0x04;
	
	Record.OperateAdd_ScreenKeyLock(tOnOff, tMinute, SettingByte, "****");
	Record.WriteScreenKeyLock(tOnOff, tMinute, SettingByte, "****");
	
	//Read Back
	//Record.ReadScreenSave();
	if(  (tOnOff == ScreenAndKeyLock.SaveOn) && (tMinute == ScreenAndKeyLock.SaveTime) && (SettingByte == ScreenAndKeyLock.IsKeyLock) ) {
		ShowSaveResult(1);
	}else{
		ShowSaveResult(-1);
		ScreenAndKeyLock.SaveOn = tOnOff;
		ScreenAndKeyLock.SaveTime = tMinute;
		ScreenAndKeyLock.IsKeyLock = SettingByte;
	}
	//Redraw
}

void TScreenSaveCfg::sRecoverSetDefault()
{
	Record.WriteScreenKeyLock(0, 20, 0, "****");
}




