#include "RuntimeData.h"
#include "CircuitState.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390

static const unsigned char stCmd[8] = {221,222,223,224,225,226,227,228};
static const unsigned char stPrm[8] = {0,1,2,3,4,5,6,7};

void TCircuitState::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(140,78,660,410);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("回路运行状态-本机", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	CtlIndex = 0;
	CtlLevel = 0;
	IsShowing = 0;
	
	for(i=0; i<12; i++){
		RecData[i] = 0;
		RecDataUpdateFlag[i] =0;
	}	
}

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

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

void TCircuitState::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 TCircuitState::DrawCaption2(void)
{	
	TMyString::sFromStr("xxxxxxx", SText.Text);
	//SText.Show();
}

void TCircuitState::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
	IsShowing = 1;
}

void TCircuitState::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 TCircuitState::SetAndShowCaption(unsigned char aType)
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
}

//______________________________________________________________________________________________________________________________

void TCircuitState::Check4Selected(void)
{
	unsigned int i;
	for(i=0; i<3;i++)aPanel->Edit[i].Selected = 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;
		default:
			break;
	}		
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TCircuitState::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 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(aPanel->Edit[0].Selected){
			aPanel->Edit[0].KeyIn(aKey);
			aPanel->Edit[0].ReDraw();
			CheckPathDev();
		}else
		if(aPanel->Edit[1].Selected){
			aPanel->Edit[1].KeyIn(aKey);
			aPanel->Edit[1].ReDraw();
			CheckPathDev();
		}else
		if(aPanel->Edit[2].Selected){
			aPanel->Edit[2].KeyIn(aKey);
			aPanel->Edit[2].ReDraw();
			CheckPathDev();
		}		
	}
	
	if(aKey == VK_UP){
		if(CtlIndex >0){
			CtlIndex--;
			Check4Selected();
			aPanel->ReDrawItems();
			PanelCanvasOut();
		}
	}else
	if(aKey == VK_DOWN){
		if(CtlIndex <2){
			CtlIndex++;
			Check4Selected();
			aPanel->ReDrawItems();
			PanelCanvasOut();
		}
	}else
	if(aKey == VK_TAB){

	}else
	if(aKey == VK_F2){
		SendCmd(0);
	}else
	if(aKey == VK_EXECUTE){
		//Save Data Here
		SendCmd(1);
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

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

void TCircuitState::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i;
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	aPanel->Caption.Text[0] = 0;
	
	aPanel->SText[0].Init(2,6,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(2,44,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(2,82,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(2,120,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(2,158,60,30,x,y,clNearBlack,clFrmFace);
	
	aPanel->SText[5].Init(200,6,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(200,44,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[7].Init(200,82,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[8].Init(200,120,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[9].Init(200,158,60,30,x,y,clNearBlack,clFrmFace);
	
	aPanel->SText[10].Init(16,190,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[11].Init(16,236,60,30,x,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr("机器号", aPanel->SText[0].Text);
	TMyString::sFromStr("回路号", aPanel->SText[1].Text);
	TMyString::sFromStr("地址号", aPanel->SText[2].Text);
	TMyString::sFromStr("类型", aPanel->SText[3].Text);
	TMyString::sFromStr("注释", aPanel->SText[4].Text);
	
	TMyString::sFromStr("机器号状态", aPanel->SText[5].Text);
	TMyString::sFromStr("Circuit", aPanel->SText[6].Text);
	TMyString::sFromStr("Addr", aPanel->SText[7].Text);
	TMyString::sFromStr("dType", aPanel->SText[8].Text);
	TMyString::sFromStr("Descp", aPanel->SText[9].Text);
	
	
	TMyString::sFromStr("1.按确认键启动目标", aPanel->SText[10].Text);
	TMyString::sFromStr("2.按设置键停止目标", aPanel->SText[11].Text);
	
	for(i=0; i<12;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	
	aPanel->Edit[0].Init(100,6,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(100,44,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(100,82,60,30,x,y,clNearBlack,clNearWhite);

	for(i=0; i<3;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(8);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.Clear();
		aPanel->Edit[i].Pst = -1;
	}
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[0].Str.FromStr("01");
	aPanel->Edit[0].Pst = 1;
	aPanel->Edit[0].Color = clMoneyGreen;
	aPanel->Edit[1].SetMaxLen(2);
	aPanel->Edit[1].Str.FromStr("01");
	aPanel->Edit[1].Pst = 1;
	aPanel->Edit[2].SetMaxLen(3);
	aPanel->Edit[2].Str.FromStr("001");
	aPanel->Edit[2].Pst = 2;
	
	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	PanelCanvasOut();
	
	CheckPathDev();
}

void TCircuitState::CheckPathDev()
{
	unsigned char cVal;
	int iVal;
	int P0Found = 0;
	int P1Found = 0;
	unsigned char aCn, aP0, aP1;
	unsigned int aAddr,i;
	
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	Path.pNum = cVal;
	
	iVal = aPanel->Edit[1].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	Path.P0 = cVal;
	
	iVal = aPanel->Edit[2].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	Path.P1 = cVal;
	
	for(i=0; i<32; i++){
		aPanel->SText[8].Text[i] = 0;
	}
	
	if(Path.pNum == MainCtl.fData.Split.MyNum){
		if(MainCtl.fData.Split.NetworkMode == 1){
			aPanel->SText[4].SetText("本集中机",24);
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			aPanel->SText[4].SetText("本区域机",24);
		}else{
			aPanel->SText[4].SetText("本机",24);
		}
		
		if( (Path.P0 >0) && (Path.P0 <= dPORT_MAX_COUNT) ){
			if(MainCtl.fData.Split.ExistTablePort[Path.P0 -1]){
				aPanel->SText[5].SetText(" ",24);
				P0Found = 1;
			}else{
				aPanel->SText[5].SetText("回路未注册",24);
				aPanel->SText[6].SetText(" ",24);
				aPanel->SText[7].SetText(" ",24);
				aPanel->SText[8].SetText(" ",24);
			}
		}else{
			aPanel->SText[5].SetText("回路号超出范围",24);
			aPanel->SText[6].SetText(" ",24);
			aPanel->SText[7].SetText(" ",24);
			aPanel->SText[8].SetText(" ",24);
		}
		
		if(P0Found){
			if( (Path.P1 >0) && (Path.P1 <= 251) ){
				if(MainCtl.fData.Split.ExistTablePort[Path.P1 -1]){
					aPanel->SText[6].SetText(" ",24);
					P1Found = 1;
				}else{
					aPanel->SText[6].SetText("地址未注册",24);
				}
			}else{
				aPanel->SText[6].SetText("地址号超出范围",24);
			}
		}
		
		if(P1Found){
			Path.dType = Port[Path.P0-1].dTypeTable[Path.P1-1];
			TMyString::sFromStr(StrTypeShortName[Path.dType], aPanel->SText[7].Text);
			aP0 = Path.P0;
			aP1 = Path.P1;
			if(aP0 && aP1){
				if(aP0 < dPORT_MAX_COUNT){
					aP0--;
					aP1--;
				}else
				if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
					aP0 = aP0 - dADDR_DIRECTPAD_START;
					aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1 - 1);
					aP0 = 40;
				}
				if(aP0 <= dPORT_MAX_COUNT){
					aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
					for(i=0; i<32; i++){
						aPanel->SText[8].Text[i] = *(volatile unsigned char *)(aAddr + i);
					}
					aPanel->SText[8].Text[32] = '\0';
				}
			}
		}else{
			TMyString::sFromStr(" ", aPanel->SText[7].Text);
		}
	}else{
		if(MainCtl.fData.Split.NetworkMode == 1){
			aPanel->SText[4].SetText(" ",24);
			aPanel->SText[5].SetText(" ",24);
			aPanel->SText[6].SetText(" ",24);
			aPanel->SText[7].SetText(" ",24);
			aPanel->SText[8].SetText(" ",24);
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			aPanel->SText[4].SetText("本机为区域机,不能操作",24);
			aPanel->SText[5].SetText(" ",24);
			aPanel->SText[6].SetText(" ",24);
			aPanel->SText[7].SetText(" ",24);
			aPanel->SText[8].SetText(" ",24);
		}else{
			aPanel->SText[4].SetText("本机为单机,不能操作  ",24);
			aPanel->SText[5].SetText(" ",24);
			aPanel->SText[6].SetText(" ",24);
			aPanel->SText[7].SetText(" ",24);
			aPanel->SText[8].SetText(" ",24);
		}
		
		if(MainCtl.fData.Split.NetworkMode == 1){
			if( (Path.pNum) && (Path.pNum <= dNETWORK_MACHINE_COUNT) ){
				aCn = Path.pNum -1;
				aP0 = Path.P0;
				aP1 = Path.P1;
				if(aP0 && aP1){
					if(aP0 < dPORT_MAX_COUNT){
						aP0--;
					}else
					if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
						aP0 = aP0 - dADDR_DIRECTPAD_START;
						aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
						aP0 = 40;
					}
					if(aP0 <= dPORT_MAX_COUNT){
						aAddr = dAddrSdRam_DescpNw + (aCn*409600)+(aP0*8192) + (aP1*32);
						for(i=0; i<32; i++){
							aPanel->SText[8].Text[i] = *(volatile unsigned char *)(aAddr + i);
						}
						aPanel->SText[8].Text[32] = '\0';
					}
				}
			}
		}
	}
	aPanel->SText[4].Show();
	aPanel->SText[5].Show();
	aPanel->SText[6].Show();
	aPanel->SText[7].Show();
	aPanel->SText[8].Show();
}

void TCircuitState::SendCmd(unsigned int IsStart)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	if(IsStart){
		aIde.BitF.Cmd = cmAutoStart;
	}else{
		aIde.BitF.Cmd = cmAutoStop;
	}
	
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	Path.pNum = cVal;
	
	iVal = aPanel->Edit[1].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	Path.P0 = cVal;
	
	iVal = aPanel->Edit[2].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	Path.P1 = cVal;
	

	
	
	
	
		Len = 5;
		
		Data[0] = 0;
		
		//Path 0
		iVal = aPanel->Edit[0].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[1] = cVal;
		
		//Cmd 1
		iVal = aPanel->Edit[7].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		aIde.BitF.Cmd = cVal;
		
		//Cmd 2
		iVal = aPanel->Edit[8].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[3] = cVal;
		
		//Prm 
		iVal = aPanel->Edit[9].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[4] = 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);
}

int TCircuitState::ExtRequst(unsigned char Prm)
{	
	IsShowing = 0;
	return 0;
}







