#include "RuntimeData.h"
#include "AddrAssign.h"

#define TextTopOffSet	6
#define ListLineCnt	10


void TAddrAssign::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+510,Top,286, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	RectPanel.Set(60,140,720,320);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("系统调试->指派终端设备地址", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	CtlIndex = 0;
	CtlMacIndex =0;
	ByWhat =0;
			
	OnActive = 0;
	IsFastCmd = 0;
}

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

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

void TAddrAssign::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 TAddrAssign::DrawCaption2(void)
{	
	TMyString::sFromStr("按TAB键切换写入方式", SText.Text);
	SText.Show();
}

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

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

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

//______________________________________________________________________________________________________________________________

void TAddrAssign::Check4Selected(void)
{
	unsigned int i;
	for(i=0; i<3;i++)aPanel->Edit[i].Selected = 0;
	for(i=11; i<14;i++)aPanel->Edit[i].Selected = 0;
	for(i=0; i<3;i++)aPanel->Btn[i].Selected = 0;
	if(ByWhat){
		switch(CtlMacIndex){
			case 0:
				aPanel->Edit[11].Selected = 1;
				break;
			case 1:
				aPanel->Edit[12].Selected = 1;
				break;
			case 2:
				aPanel->Edit[13].Selected = 1;
				break;
			case 3:
				aPanel->Btn[1].Selected = 1;
				break;
			case 4:
				aPanel->Btn[2].Selected = 1;
				break;
			default:

				break;
		}		
	}else{
		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->Btn[0].Selected = 1;
				break;
			default:

				break;
		}		
	}
}

void TAddrAssign::MacShow()
{	
	int x,y, pst,i;
	unsigned int tClr, bClr;
	char t;
	
	x = RectPanel.Left + 309;
	y = RectPanel.Top + 76;
	if(aPanel->Edit[13].Selected){
		bClr = clDeepBlue;
		tClr = clNearWhite;
	}else{
		bClr = clNearWhite;
		tClr = clNearBlack;
	}
	TEdit::sDrawSelf(x,y,339,36,bClr);
	x = x + 3;
	y = y + 6;
	for(i=0; i<7; i++){
		t = aPanel->Edit[13].Str.Text[i*3 +0];
		if(t){
			gui_write_EN2412(x, y, tClr, t);
		}else{
			gui_write_EN2412(x, y, tClr, 'x');
		}
		x +=12;
		t = aPanel->Edit[13].Str.Text[i*3 +1];
		if(t){
			gui_write_EN2412(x, y, tClr, t);
		}else{
			gui_write_EN2412(x, y, tClr, 'x');
		}
		x +=12;
		t = aPanel->Edit[13].Str.Text[i*3 +2];
		if(t){
			gui_write_EN2412(x, y, tClr, t);
		}else{
			gui_write_EN2412(x, y, tClr, 'x');
		}
		x +=12;
		if(i<6)gui_write_EN2412(x, y, tClr, '-');
		x +=12;
	}
	if(aPanel->Edit[13].Selected){
		pst = aPanel->Edit[13].Pst / 3 * 4;
		pst += aPanel->Edit[13].Pst % 3;
		x = RectPanel.Left + 309 + 3;
		x = (pst + 1) * 12 + x;
		VertLineRender(x,	y, 24,	tClr);
	}
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TAddrAssign::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	if(ByWhat){
		//Mac Type
		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(CtlMacIndex < 3){
				if(CtlMacIndex == 0){
					aPanel->Edit[11].KeyIn(aKey);
					aPanel->Edit[11].ReDraw();
				}else
				if(CtlMacIndex == 1){
					aPanel->Edit[12].KeyIn(aKey);
					aPanel->Edit[12].ReDraw();
				}else
				if(CtlMacIndex == 2){
					aPanel->Edit[13].KeyIn(aKey);
					MacShow();
				}
			}
			if(CtlMacIndex == 4){
				if(aKey == VK_RIGHT){
					CtlMacIndex++;
					Check4Selected();
					aPanel->ReDrawItems();
					MacShow();
				}
			}else
			if(CtlMacIndex == 5){
				if(aKey == VK_LEFT){
					CtlMacIndex--;
					Check4Selected();
					aPanel->ReDrawItems();
					MacShow();
				}
			}
		}
		
		if(aKey == VK_UP){
			if(CtlMacIndex >0){
				CtlMacIndex--;
				Check4Selected();
				aPanel->ReDrawItems();
				MacShow();
			}
		}else
		if(aKey == VK_DOWN){
			if(CtlMacIndex <4){
				CtlMacIndex++;
				Check4Selected();
				aPanel->ReDrawItems();
				MacShow();
			}
		}else
		if(aKey == VK_TAB){
			if(ByWhat){
				ByWhat =0;
			}else{
				ByWhat =1;
			}
			Check4Selected();
			aPanel->ReDrawItems();
			MacShow();
		}
		if(aKey == VK_EXECUTE){
			//Save Data Here
			SendCmd();
		}

		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}		
	}else{
		//Not Mac Type
		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 < 3){
				aPanel->Edit[CtlIndex].KeyIn(aKey);
				aPanel->Edit[CtlIndex].ReDraw();
			}
		}
		
		if(aKey == VK_UP){
			if(CtlIndex >0){
				CtlIndex--;
				Check4Selected();
				aPanel->ReDrawItems();
			}
		}else
		if(aKey == VK_DOWN){
			if(CtlIndex <3){
				CtlIndex++;
				Check4Selected();
				aPanel->ReDrawItems();
			}
		}else
		if(aKey == VK_TAB){
			if(ByWhat){
				ByWhat =0;
			}else{
				ByWhat =1;
			}
			Check4Selected();
			aPanel->ReDrawItems();
			MacShow();
		}
		if(aKey == VK_EXECUTE){
			//Save Data Here
			SendCmd();
		}

		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}
	}
	
	return aMsg;	
}

void TAddrAssign::PanelCanvasOut()
{
	VertLineRender(RectPanel.Left + 170,		RectPanel.Top+6, 			140,			clSilver);
}

void TAddrAssign::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(11,3,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(11,35,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(11,67,70,30,x,yt,clNearBlack,clFrmFace);
	
	aPanel->SText[11].Init(216,3,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(216,35,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(204,67,70,30,x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("回路号", aPanel->SText[0].Text);
	TMyString::sFromStr("原地址", aPanel->SText[1].Text);
	TMyString::sFromStr("新地址", aPanel->SText[2].Text);
	
	TMyString::sFromStr("回路号", aPanel->SText[11].Text);
	TMyString::sFromStr("  地址", aPanel->SText[12].Text);
	TMyString::sFromStr("MAC 6:0", aPanel->SText[13].Text);
	
	aPanel->Edit[0].Init(101,3,50,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(101,35,50,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(101,67,50,30,x,yt,clNearBlack,clNearWhite);
	
	aPanel->Edit[11].Init(310,3,50,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[12].Init(310,35,50,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[13].Init(310,67,50,30,x,yt,clNearBlack,clNearWhite);

	aPanel->Btn[0].Init(32,122,110,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[1].Init(310,122,110,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[2].Init(430,122,110,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[0].Caption.FromStr("更改地址");
	aPanel->Btn[1].Caption.FromStr("读取MAC");
	aPanel->Btn[2].Caption.FromStr("写入地址");

	
	for(i=0; i<3;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<3;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(2);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("01");
		aPanel->Edit[i].Pst = 1;
	}
	for(i=11; i<14;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(5);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("001");
		aPanel->Edit[i].Pst = 1;
	}
	aPanel->Edit[13].Visible = 0;
	aPanel->Edit[13].SetMaxLen(21);
	aPanel->Edit[13].Str.FromStr("001002003004005006007");
	aPanel->Edit[13].Pst = 20;
	
	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	PanelCanvasOut();
	
	MacShow();
}

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






