#include "RuntimeData.h"
#include "EpDevState.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390

void TEpDevState::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	Caption.Init(Left,Top,Width, CH, 0, 0, clWhite, clMaroon);
	SText.Init(Left+358,Top,434, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	RectPanel.Set(100,82,700,428);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("终端调试-本机", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	CtlIndex = 0;
	FastIndex = 0;
			
	OnActive = 0;
	IsFastCmd = 0;
}

void TEpDevState::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

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

void TEpDevState::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
}


void TEpDevState::DrawCaption2(void)
{	
	TMyString::sFromStr("TAB键在快捷命令与定义命令之间切换", SText.Text);
	SText.Show();
}

void TEpDevState::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
}

void TEpDevState::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		Caption.Show();
		DrawCaption2();
		aPanel->Show();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->Show();
	}
}

void TEpDevState::SetAndShowCaption(unsigned char aType)
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
}

//______________________________________________________________________________________________________________________________

void TEpDevState::Check4Selected(void)
{
	unsigned int i;
	for(i=0; i<7;i++)aPanel->Btn[i].Selected = 0;
	for(i=0; i<10;i++)aPanel->Edit[i].Selected = 0;
	if(IsFastCmd == 0){
		switch(CtlIndex){
			case 0:
				aPanel->Edit[0].Selected = 1;
				break;
			case 1:
				aPanel->Edit[1].Selected = 1;
				break;
			case 2:
				aPanel->Edit[2].Selected = 1;
				break;
			case 3:
				aPanel->Edit[3].Selected = 1;
				break;
			case 4:
				aPanel->Edit[4].Selected = 1;
				break;
			case 5:
				aPanel->Edit[5].Selected = 1;
				break;
			case 6:
				aPanel->Edit[6].Selected = 1;
				break;
			case 7:
				aPanel->Edit[7].Selected = 1;
				break;
			case 8:
				aPanel->Edit[8].Selected = 1;
				break;
			case 9:
				aPanel->Edit[9].Selected = 1;
				break;

			default:

				break;
		}	
	}else{
		switch(FastIndex){
			case 0:
				aPanel->Btn[1].Selected = 1;
				break;
			case 1:
				aPanel->Btn[2].Selected = 1;
				break;
			case 2:
				aPanel->Btn[3].Selected = 1;
				break;
			case 3:
				aPanel->Btn[4].Selected = 1;
				break;
			case 4:
				aPanel->Btn[5].Selected = 1;
				break;
			case 5:
				aPanel->Btn[6].Selected = 1;
				break;
			case 6:
				aPanel->Btn[0].Selected = 1;
				break;
			default:
				break;
		}	
	}		
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TEpDevState::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	
	if(IsFastCmd == 0){
		if( 	(aKey == VK_DELETE) || 
			(aKey == VK_0) ||	
			(aKey == VK_1) ||
			(aKey == VK_2) ||
			(aKey == VK_3) ||
			(aKey == VK_4) ||
			(aKey == VK_5) ||
			(aKey == VK_6) ||	
			(aKey == VK_7) ||
			(aKey == VK_8) ||
			(aKey == VK_9) ||
			(aKey == VK_LEFT) ||
			(aKey == VK_RIGHT)
						){
			if(CtlIndex < 10){
				aPanel->Edit[CtlIndex].KeyIn(aKey);
				aPanel->Edit[CtlIndex].ReDraw();
			}
		}
		
		if(aKey == VK_UP){
			if(CtlIndex >0){
				CtlIndex--;
				if(CtlIndex == 5)CtlIndex=4;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}else{
				CtlIndex = 9;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}
		}else
		if(aKey == VK_DOWN){
			if(CtlIndex <9){
				CtlIndex++;
				if(CtlIndex == 5)CtlIndex=6;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}else{
				CtlIndex = 0;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}
		}else
		if(aKey == VK_TAB){
			IsFastCmd = 1;
			Check4Selected();
			aPanel->ReDrawItems();
			PanelCanvasOut();
		}
		if(aKey == VK_EXECUTE){
			//Save Data Here
			SendCmd();
		}
	}else{
		if(aKey == VK_UP){
			if(FastIndex >2){
				FastIndex--;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}else{
				FastIndex =6;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}
		}else
		if(aKey == VK_DOWN){
			if(FastIndex <6){
				FastIndex++;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}else{
				FastIndex =2;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}
		}else
		if(aKey == VK_TAB){
			IsFastCmd = 0;
			Check4Selected();
			aPanel->ReDrawItems();
			PanelCanvasOut();
		}
		if(aKey == VK_EXECUTE){
			//Save Data Here
			SendFastCmd();
		}		
	}
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

void TEpDevState::PanelCanvasOut()
{
	//TXBox::sDrawBoxBorder(RectPanel.Left + 8, RectPanel.Top + 3, RectPanel.Left + 362, RectPanel.Top + 150, "自定义命令", 8, 1, 0, clFrmFace);
}

void TEpDevState::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	yt = y+6;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	
	aPanel->SText[0].Init(26,3,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(26,35,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(26,67,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(26,99,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(26,131,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(26,163,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(26,195,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[7].Init(26,227,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[8].Init(26,259,70,30,x,yt,clNearBlack,clFrmFace);
	
	aPanel->SText[9].Init(26,294,70,30,x,yt,clNearBlack,clFrmFace);	
	
	aPanel->SText[10].Init(430,3,70,30,x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("命令------CMD", aPanel->SText[0].Text);
	TMyString::sFromStr("控制字----D-0", aPanel->SText[1].Text);
	TMyString::sFromStr("机器-区号-D-1", aPanel->SText[2].Text);
	TMyString::sFromStr("回路-状态-D-2", aPanel->SText[3].Text);
	TMyString::sFromStr("地址------D-3", aPanel->SText[4].Text);
	TMyString::sFromStr("通道------D-4", aPanel->SText[5].Text);
	TMyString::sFromStr("类型------D-5", aPanel->SText[6].Text);
	TMyString::sFromStr("数值0-----D-6", aPanel->SText[7].Text);
	TMyString::sFromStr("数值1-----D-7", aPanel->SText[8].Text);
	
	TMyString::sFromStr("长度------DLC", aPanel->SText[9].Text);
	aPanel->SText[10].SetText(" ", 24);
	
	for(i=0; i<11;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	
	aPanel->Edit[0].Init(330,3,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(330,35,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(330,67,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[3].Init(330,99,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[4].Init(330,131,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[5].Init(330,163,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[6].Init(330,195,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[7].Init(330,227,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[8].Init(330,259,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[9].Init(330,294,70,30,x,yt,clNearBlack,clNearWhite);
	
	aPanel->Edit[13].Init(220,3,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[14].Init(220,35,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[15].Init(220,67,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[16].Init(220,99,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[17].Init(220,131,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[18].Init(220,163,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[19].Init(220,195,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[20].Init(220,227,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[21].Init(220,259,70,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[22].Init(220,294,70,30,x,yt,clNearBlack,clNearWhite);

	aPanel->Btn[0].Init(430,294,156,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("发送定义内容");
	
	aPanel->Btn[1].Init(430,45,156,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[2].Init(430,85,156,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[3].Init(430,125,156,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[4].Init(430,165,156,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[5].Init(430,205,156,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[6].Init(430,245,156,36,x,yt,clNearBlack,clFrmFace);
	
	
	aPanel->Btn[1].Caption.FromStr("15 手动启动");
	aPanel->Btn[2].Caption.FromStr("16 手动停止");
	aPanel->Btn[3].Caption.FromStr("18 启动");
	aPanel->Btn[4].Caption.FromStr("19 停止");
	aPanel->Btn[5].Caption.FromStr("25 声光启动");
	aPanel->Btn[6].Caption.FromStr("26 声光停止");
	
	
	for(i=0; i<7;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(2);
		aPanel->Btn[i].SelectedColor = clBlue;
	}
	for(i=0; i<9;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("001");
		aPanel->Edit[i].Pst = 2;
	}
	for(i=9; i<10;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(1);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("8");
		aPanel->Edit[i].Pst = 0;
	}
	for(i=13; i<23;i++){
		aPanel->Edit[i].Enable = 0;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("--");
		aPanel->Edit[i].Pst = 1;
	}
	
	aPanel->Edit[5].Enable = 0;
	aPanel->Edit[5].Str.FromStr("0");
	aPanel->Btn[1].Enable = 0;
	aPanel->Btn[2].Enable = 0;
	
	aPanel->Btn[6].SetCaptionLeftSpacing(0);
	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	PanelCanvasOut();
}

void TEpDevState::SendCmd()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len = aPanel->Edit[9].Str.ToInteger();
	if(Len >8)Len =8;
	
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aIde.BitF.Cmd = cVal;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	for(i=0; i<Len; i++){
		iVal = aPanel->Edit[i+1].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[i] = cVal;
	}
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.CmdSendCustom(aIde, Data, Len);
}

void TEpDevState::SendFastCmd()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	unsigned int FullPath;
	int iVal;
	unsigned char aCtlNum, aP0, aP1, adType;
	unsigned int CtlFound;
	
	if(FastIndex == 0){
		aIde.BitF.Cmd = 18;
		Len = 6;
	}else
	if(FastIndex == 1){
		aIde.BitF.Cmd = 19;
		Len = 6;
	}else
	if(FastIndex == 2){
		aIde.BitF.Cmd = 18;
		Len = 6;
	}else
	if(FastIndex == 3){
		aIde.BitF.Cmd = 19;
		Len = 6;
	}else
	if(FastIndex == 4){
		aIde.BitF.Cmd = 25;
		Len = 3;
	}else
	if(FastIndex == 5){
		aIde.BitF.Cmd = 26;
		Len = 2;
	}else
	if(FastIndex == 6){
		SendCmd();
	}
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	for(i=0; i<Len; i++){
		iVal = aPanel->Edit[i+1].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[i] = cVal;
	}
	
	aP0 = Data[3] -1;
	aP1 = Data[2] -1;
	aCtlNum = Data[1];
	if(aP0 <40){
		adType = Port[aP0].dTypeTable[aP1];
	}
	
	if(aIde.BitF.Cmd == 18){
		FullPath = Data[4];
		FullPath <<= 8;
		FullPath |= Data[3];
		FullPath <<= 8;
		FullPath |= Data[2];
		FullPath <<= 8;
		FullPath |= Data[1];
		
		aP0 = Data[3] -1;
		aP1 = Data[2] -1;
		if(aP0 < 40){
			Port[aP0].StateTable[aP1] &= ~dEpStateBitFbMiss;
			Port[aP0].StateTable[aP1] &= ~dEpStateBitDelayStart;
		}
		Record.OperateAdd_ManualStart(FullPath, etLinkageStart, 0x01);
		RtManualPushStartMsg(adType,1,FullPath);
	}else
	if(aIde.BitF.Cmd == 19){
		FullPath = Data[4];
		FullPath <<= 8;
		FullPath |= Data[3];
		FullPath <<= 8;
		FullPath |= Data[2];
		FullPath <<= 8;
		FullPath |= Data[1];
		aP0 = Data[3] -1;
		aP1 = Data[2] -1;
		if(aP0 < 40){
			Port[aP0].StateTable[aP1] &= ~dEpStateBitDelayStart;
		}
		Record.OperateAdd_ManualStartCancel(FullPath, etLinkageStartCancel, 0x01);
		//RtManualPushStartCancelMsg(1,FullPath);
	}else
	if(aIde.BitF.Cmd == 18){
		FullPath = Data[4];
		FullPath <<= 8;
		FullPath |= Data[3];
		FullPath <<= 8;
		FullPath |= Data[2];
		FullPath <<= 8;
		FullPath |= Data[1];
		
		aP0 = Data[3] -1;
		aP1 = Data[2] -1;
		if(aP0 < 40){
			Port[aP0].StateTable[aP1] &= ~dEpStateBitFbMiss;
		}
		Record.OperateAdd_ManualStart(FullPath, etLinkageStart, 0x01);
		RtManualPushStartMsg(adType,1,FullPath);
	}else
	if(aIde.BitF.Cmd == 19){
		FullPath = Data[4];
		FullPath <<= 8;
		FullPath |= Data[3];
		FullPath <<= 8;
		FullPath |= Data[2];
		FullPath <<= 8;
		FullPath |= Data[1];
		aP0 = Data[3] -1;
		aP1 = Data[2] -1;
		if(aP0 < 40){
			Port[aP0].StateTable[aP1] &= ~dEpStateBitDelayStart;
		}
		Record.OperateAdd_ManualStartCancel(FullPath, etLinkageStartCancel, 0x01);
		//RtManualPushStartCancelMsg(1,FullPath);
	}else
	if(aIde.BitF.Cmd == 25){
		FullPath = Data[4];
		FullPath <<= 8;
		FullPath |= Data[3];
		FullPath <<= 8;
		FullPath |= Data[2];
		FullPath <<= 8;
		FullPath |= Data[1];
		Record.OperateAdd_ManualStart(0x99999999, etLinkageStart, 0x02);
		RtManualPushStartMsg(255,2,FullPath);
	}else
	if(aIde.BitF.Cmd == 26){
		FullPath = Data[4];
		FullPath <<= 8;
		FullPath |= Data[3];
		FullPath <<= 8;
		FullPath |= Data[2];
		FullPath <<= 8;
		FullPath |= Data[1];
		Record.OperateAdd_ManualStartCancel(0x99999999, etLinkageStartCancel, 0x02);
		//RtManualPushStartCancelMsg(255,2,FullPath);
	}
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	if( aCtlNum == MainCtl.fData.Split.MyNum ){
		//InnerCan.CmdSendCustom(aIde, Data, Len);
		InnerCan.OBufFastInsert(1, aIde.BitF.Cmd, Data, Len);
		aPanel->SText[10].SetText("发送成功", 24);
	}else{
		if(MainCtl.fData.Split.NetworkMode == 1){
			CtlFound = 0;
			for(i=0; i<ProtoC2C.SlaveCount; i++){
				if(aCtlNum == ProtoC2C.SlaveList[i]){
					CtlFound = 1;
				}
			}
			if(CtlFound){
				ProtoC2C.MasterDoDevRemoteStart(aCtlNum,Data[3],Data[4],Data[6]);
				aPanel->SText[10].SetText("发送成功", 24);
			}else{
				aPanel->SText[10].SetText("发送失败", 24);
			}
		}else{
			aPanel->SText[10].SetText("发送失败", 24);
		}
	}
	aPanel->SText[10].Show();
}








