#include "RuntimeData.h"
#include "PrinterSet.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 TPrinterSet::Init()
{
	IsPermission =0;
	Tag.Group = 0;
	Tag.Inx0 = 0;
	Tag.Inx1 = 0;
	
	SaveResult = 0;
}

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

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

void TPrinterSet::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 TPrinterSet::DrawOnOff()
{
	if(Sta.OnOff){
		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 TPrinterSet::DrawEvent()
{
	if(Sta.All){
		TRoundSquare::sDrawBySize(svGridX[0] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[0] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Fire){
		TRoundSquare::sDrawBySize(svGridX[1] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[1] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Lnk){
		TRoundSquare::sDrawBySize(svGridX[2] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[2] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Sv){
		TRoundSquare::sDrawBySize(svGridX[3] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[3] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Fault){
		TRoundSquare::sDrawBySize(svGridX[4] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[4] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Mask){
		TRoundSquare::sDrawBySize(svGridX[5] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[5] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Opa){
		TRoundSquare::sDrawBySize(svGridX[6] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[6] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
	if(Sta.Others){
		TRoundSquare::sDrawBySize(svGridX[7] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(svGridX[7] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
}

void TPrinterSet::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);

	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "本机->打印机");
		
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +10, clNearWhite,  "打印机使能");
		TextRender_string24(svGridX[0], Lv.Grid.Top[1] +10, clNearWhite,  "打印信息选择");
		
		
		TextRender_string24(svGridX[0] +28, Lv.Grid.Top[0] +46, clNearWhite,  "开启");
		TextRender_string24(svGridX[1] +28, Lv.Grid.Top[0] +46, clNearWhite,  "关闭");
		
		TextRender_string24(svGridX[0] +28, Lv.Grid.Top[1] +46, clNearWhite,  "全部");
		TextRender_string24(svGridX[1] +28, Lv.Grid.Top[1] +46, clNearWhite,  "报警");
		TextRender_string24(svGridX[2] +28, Lv.Grid.Top[1] +46, clNearWhite,  "联动");
		TextRender_string24(svGridX[3] +28, Lv.Grid.Top[1] +46, clNearWhite,  "监管");
		TextRender_string24(svGridX[4] +28, Lv.Grid.Top[1] +46, clNearWhite,  "故障");
		TextRender_string24(svGridX[5] +28, Lv.Grid.Top[1] +46, clNearWhite,  "屏蔽");
		TextRender_string24(svGridX[6] +28, Lv.Grid.Top[1] +46, clNearWhite,  "操作");
		TextRender_string24(svGridX[7] +28, Lv.Grid.Top[1] +46, clNearWhite,  "其他");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Local->Printer Setup");
		
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +10, clNearWhite,  "Printer Enable Set");
		TextRender_string24(svGridX[0], Lv.Grid.Top[1] +10, clNearWhite,  "Infomation Set");
		
		
		TextRender_string24(svGridX[0] +28, Lv.Grid.Top[0] +46, clNearWhite,  "On");
		TextRender_string24(svGridX[1] +28, Lv.Grid.Top[0] +46, clNearWhite,  "Off");
		
		TextRender_string24(svGridX[0] +28, Lv.Grid.Top[1] +46, clNearWhite,  "All");
		TextRender_string24(svGridX[1] +28, Lv.Grid.Top[1] +46, clNearWhite,  "Fire");
		TextRender_string24(svGridX[2] +28, Lv.Grid.Top[1] +46, clNearWhite,  "Link");
		TextRender_string24(svGridX[3] +28, Lv.Grid.Top[1] +46, clNearWhite,  "SUPV");
		TextRender_string24(svGridX[4] +28, Lv.Grid.Top[1] +46, clNearWhite,  "Flt.");
		TextRender_string24(svGridX[5] +28, Lv.Grid.Top[1] +46, clNearWhite,  "Mask");
		TextRender_string24(svGridX[6] +28, Lv.Grid.Top[1] +46, clNearWhite,  "OPER");
		TextRender_string24(svGridX[7] +28, Lv.Grid.Top[1] +46, clNearWhite,  "Oth.");		
	}
	
	DrawOnOff();
	DrawEvent();
}

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

void TPrinterSet::DrawTip()
{
	
}

void TPrinterSet::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();
	}
}

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

void TPrinterSet::PermissionCmdGO()
{
	if( GoResult()){
		LoadSysCfg();
	}
}

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

void TPrinterSet::LoadSysCfg()
{
	Sta.OnOff 	= PriterSetting.OnOff;
	Sta.All 	= PriterSetting.PrintSelected & (0x0001);
	Sta.Fire 	= PriterSetting.PrintSelected & (0x0002);
	Sta.Lnk 	= PriterSetting.PrintSelected & (0x0004);
	Sta.Sv 	= PriterSetting.PrintSelected & (0x0008);
	Sta.Fault 	= PriterSetting.PrintSelected & (0x0010);
	Sta.Mask 	= PriterSetting.PrintSelected & (0x0020);
	Sta.Opa 	= PriterSetting.PrintSelected & (0x0040);
	Sta.Others 	= PriterSetting.PrintSelected & (0x0080);
}

int TPrinterSet::GoResult()
{
	Sta.WriteByte = 0;
	if(Sta.All) 	Sta.WriteByte = 1;
	if(Sta.Fire) 	Sta.WriteByte |= 0x02;
	if(Sta.Lnk) 	Sta.WriteByte |= 0x04;
	if(Sta.Sv) 		Sta.WriteByte |= 0x08;
	if(Sta.Fault) 	Sta.WriteByte |= 0x10;
	if(Sta.Mask) 	Sta.WriteByte |= 0x20;
	if(Sta.Opa) 	Sta.WriteByte |= 0x40;
	if(Sta.Others) 	Sta.WriteByte |= 0x80;
	
	Record.WritePrinterState(Sta.OnOff);
	Record.WritePrinterEvent(Sta.WriteByte);	
	Record.ReadPrintSet();
	
	if(  (PriterSetting.OnOff == Sta.OnOff) && (PriterSetting.PrintSelected == Sta.WriteByte)  ){
		Record.OperateAdd_CfgPriter(PriterSetting.OnOff, PriterSetting.PrintSelected);
		ShowSaveResult(1);
	}else{
		ShowSaveResult(-1);
	}
	return 0;
}

void TPrinterSet::sRecoverSetDefault()
{
	Record.WritePrinterState(0x01);
	Record.WritePrinterEvent(0xFF);	
	Record.ReadPrintSet();	
}

void TPrinterSet::DrawSelected(int IsSelected)
{
	unsigned int tClr;
	tClr = IsSelected ? clNearWhite : ItemColor;
	if(Tag.Group == 0){
		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)
			TRoundSquare::sSelected(svGridX[0] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 1)
			TRoundSquare::sSelected(svGridX[1] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 2)
			TRoundSquare::sSelected(svGridX[2] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 3)
			TRoundSquare::sSelected(svGridX[3] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 4)
			TRoundSquare::sSelected(svGridX[4] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 5)
			TRoundSquare::sSelected(svGridX[5] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 6)
			TRoundSquare::sSelected(svGridX[6] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);
		else if(Tag.Inx1 == 7)
			TRoundSquare::sSelected(svGridX[7] +2, Lv.Grid.Top[1] + 46, dWWidth, dWHeigh, 4, tClr);	
	}
}

void TPrinterSet::ChangeValue()
{
	if(Tag.Group == 0){
		if(Tag.Inx0 == 0)
			Sta.OnOff = Sta.OnOff ? 0 : 1;
		else if(Tag.Inx0 == 1)
			Sta.OnOff = Sta.OnOff ? 0 : 1;	
	}else
	if(Tag.Group == 1){
		if(Tag.Inx1 == 0)
			Sta.All = Sta.All ? 0: 1;
		else if(Tag.Inx1 == 1)
			Sta.Fire = Sta.Fire ? 0: 1;
		else if(Tag.Inx1 == 2)
			Sta.Lnk = Sta.Lnk ? 0: 1;
		else if(Tag.Inx1 == 3)
			Sta.Sv = Sta.Sv ? 0: 1;
		else if(Tag.Inx1 == 4)
			Sta.Fault = Sta.Fault ? 0: 1;
		else if(Tag.Inx1 == 5)
			Sta.Mask = Sta.Mask ? 0: 1;
		else if(Tag.Inx1 == 6)
			Sta.Opa = Sta.Opa ? 0: 1;
		else if(Tag.Inx1 == 7)
			Sta.Others = Sta.Others ? 0: 1;	
	}
}

TGuiMsgReturn TPrinterSet::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();
			DrawOnOff();
			DrawEvent();
		}else
		if(aKey == VK_EXECUTE){
			//Save Data Here
			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;	
}


















