#include "Gui.h"
#include "DevProperty.h"

void TDevProperty::DrawFixTextHandPad()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "操作");
		unsigned int aClr;
		if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clNearWhite, "灯点", 14, aClr);
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +150, Lv.Grid.Top[2] + 36, 124, 30, 7, clNearWhite, "模拟报警", 14, aClr);
	}else{
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "OPER");
		unsigned int aClr;
		if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clNearWhite, "LED ", 14, aClr);
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +150, Lv.Grid.Top[2] + 36, 124, 30, 7, clNearWhite, "SimAlarm", 14, aClr);
	}
}

void TDevProperty::FullRedrawhtHandPad(int Prm)
{
	if(Prm == 0){
		for(int i=2; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
		ReDrawCommonInf();	
		DrawFixTextHandPad();
		DrawLightOnStateHandPad();
		RedrawPanelData();
		Redraw4HandPadSmallPanelData();
		if(EdittingDescp){
			CnInput.FullRedraw();
		}else{
			Check4Selected();
		}
	}else{
		for(int i=2; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
		ReDrawCommonInf();	
		DrawFixTextHandPad();
		DrawLightOnStateHandPad();
		RedrawPanelData();
		Redraw4HandPadSmallPanelData();
		if(EdittingDescp){
			CnInput.FullRedraw();
		}else{
			Check4Selected();
		}		
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TDevProperty::InitHtHandPad()
{
	PanelIsUsingText = 1;
	for(int i=2; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	DrawFixTextHandPad();
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	RectFillRender(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 10 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);
	IList->Edit[0].Init(Lv.Grid.Left[2] +116, Lv.Grid.Top[2] + 4, 60,30,0,0,1,clNearBlack);
	IList->Edit[0].Str.Clear();
	
	DrawLightOnStateHandPad();
}

void TDevProperty::EditDescpReturnHtHandPad()
{
	PanelIsUsingText = 1;
	for(int i=2; i<4; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	DrawFixTextHandPad();
	DrawLightOnStateHandPad();
	ReDrawPanel();
}

void TDevProperty::Redraw4HandPadNewLoad()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawLightOnStateHandPad();
}

void TDevProperty::Redraw4HandPadSmallPanelData()
{
	RectFillRender(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 10 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);

	TMyString::sClear(TextDraw);
	TMyString::sFrom2Dg(RuntimeData.voltage % 100, TextDraw);
	TMyString::sAddOnChar('.', TextDraw);
	TMyString::sAddOn1Dg(0, TextDraw);
	TMyString::sAddOnChar('V', TextDraw);
	TextRender_string24(Lv.PanelSmall.Left + 32, Lv.PanelSmall.Top + 4, clNearWhite, SmallPanelColor, TextDraw);
	
	DrawMac();
}

void TDevProperty::ClearSelectedHtHandPad()
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			break;
		case 2:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			break;
		case 3:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 0;
			IList->EditUserCode.Show();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 0;
			IList->EditAreaAssign.Show();
			break;
		case 6:
			//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clNearWhite, "灯点", 14, clNearBlack);
			DrawLightOnStateHandPad(-1);
			break;
		case 7:
			unsigned int aClr;
			if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
			if(LanguageEnCn==0)tText="模拟报警"; else tText="SimAlarm";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +150, Lv.Grid.Top[2] + 36, 124, 30, 7, clNearWhite, tText, 14, aClr);
			break;
	}
}
void TDevProperty::Check4SelectedHtHandPad()
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 2:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 3:
			IList->EditDescpShow.Selected = 1;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 1;
			IList->EditAreaAssign.SetPstTail();
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 6:
			//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clDeepBlue, "灯点", 14, clNearWhite);
			DrawLightOnStateHandPad();
			break;
		case 7:
			unsigned int aClr;
			if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearWhite;else	aClr = clSilver;
			if(LanguageEnCn==0)tText="模拟报警"; else tText="SimAlarm";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +150, Lv.Grid.Top[2] + 36, 124, 30, 7, clDeepBlue, tText, 14, aClr);
			break;
	}	
}

TGuiMsgReturn TDevProperty::KeyInHtHandPad(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	switch(aKey){
		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_DELETE:
		case VK_LEFT:
		case VK_RIGHT:
			if(Tag.Inx < 6){
				KeyInCommonEdit(aKey);
			}else{
				if(Tag.Inx == 6){
					//Light Up
					if(aKey == VK_RIGHT){
						ClearSelectedHtHandPad();
						Tag.Inx = 7;
						Check4SelectedHtHandPad();
					}
				}else
				if(Tag.Inx == 7){
					//Sim Alram
					if(aKey == VK_LEFT){
						ClearSelectedHtHandPad();
						Tag.Inx = 6;
						Check4SelectedHtHandPad();
					}
				}
			}
			break;
		case VK_UP:
			if(PortNumValid && AddrNumValid){
				ClearSelectedHtHandPad();
				Tag.Inx > Tag.InxMin ? Tag.Inx-- : Tag.Inx = 7;
				Check4SelectedHtHandPad();
			}
			break;
		case VK_DOWN:
			if(PortNumValid && AddrNumValid){
				ClearSelectedHtHandPad();
				Tag.Inx < 7 ? Tag.Inx++ : Tag.Inx = Tag.InxMin;
				Check4SelectedHtHandPad();
			}
			break;
		case VK_TAB:
			//PanelIsUsingText = PanelIsUsingText ? 0 : 1;
			break;
		case dfKEY_SETUP:
			break;
		case dfKEY_QUERY:
			PanelDrawData.DesertTextHex = PanelDrawData.DesertTextHex ? 0: 1;
			break;
		case dfKEY_ENCN:
			SendReqMac();
			break;
		case VK_EXECUTE:
			if(PortNumValid && AddrNumValid){
				if(Tag.Inx <6){
					KeyInCommonEnter(aKey);
				}else{
					if(Tag.Inx == 6){
						//Light Up
						if(RunState == TRunModeCfg::RunModeAdjust){
							SendCmdLightOn();
							DrawLightOnStateHandPad();
						}
					}else
					if(Tag.Inx == 7){
						//Sim Alram
						if(RunState == TRunModeCfg::RunModeAdjust){
							PermissionCmd = pcSimAlram; //pcSimAlram,pcSimStart,pcSimInputActive,
							if(SystemPermission >1){
								PermissionCmdGO();
							}else{
								IsPermission = 1;
								PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
							}
						}
					}
				}
			}
			break;
		case VK_RETURN:
			IsShow = 0;
			RealTimeSendCmdAbort();
			aMsg = guiMsgReturn;
			break;
	}
	
	
	return aMsg;
	
}

void TDevProperty::DrawLightOnStateHandPad(int aPrm)
{
	unsigned char aP0, aP1;
	char *tText;
	unsigned int TextColor, bClr;
	if( (Path0) && (Path0<=dPORT_MAX_COUNT) && (Path1) ){
		aP0 = Path0 -1;
		aP1 = Path1 -1;
		if( (Tag.Inx == 6) && (aPrm != -1) ){
			TextColor = clNearWhite;
			bClr = clDeepBlue;
		}else{
			if(RunState == TRunModeCfg::RunModeAdjust)TextColor = clNearBlack;else	TextColor = clSilver;
			bClr = clNearWhite;
		}
		if(Port[aP0].HasSetLightOn[aP1]){
			TextColor = clRed;
		}
		if(LanguageEnCn == 0)tText = "灯点"; else tText = "LED ";
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, bClr, tText, 14, TextColor);
	}	
}

void TDevProperty::Task1000MsHandPad()
{
	Redraw4HandPadSmallPanelData();
}




