#include "RuntimeData.h"
#include "RootDevVoltage.h"

//Unit For Voltage	0.2V
//unit For Current	0.01A

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390

#define dfCMD_UpdateVoltage	220

static int xs[8] ={
	126, 208, 268, 331,     421, 515, 597, 700
};
static int xv[8] ={
	126, 230, 280, 343,     433, 543, 625, 700
};

void TRootDevVoltage::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(120,78,680,410);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("系统调试->根设备电压电流", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	TopIndex = 0;
}

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

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

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

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

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

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

//______________________________________________________________________________________________________________________________

void TRootDevVoltage::DrawItems(void)
{
	unsigned int i, times;
	unsigned int aP0;
	unsigned int pn,nn, aClr;
	int aTop = 128;
	times = 0;
	
	RectFillRender(aPanel->Content.Left, aPanel->Content.Top+8, aPanel->Content.Right, aPanel->Content.Bottom -46, clFrmFace);
	
	for(i=TopIndex; i<64; i++){
		if(times>5)break;
		if(i<40){
			aP0 = i+1;
			TMyString::sClear(Text[1]);
			TMyString::sClear(Text[2]);
			TMyString::sAddOn2Dg(aP0, Text[1]);
			if(Port[i].fData.Common.Id){
				aClr = clNearBlack;
			}else{
				aClr = clGray;
			}
			
			if(Port[i].fData.Common.Id){
				TMyString::sAddOn2Dg(aP0, Text[2]);
				pn = static_cast<unsigned int>(Port[i].fVoltage);
				nn = static_cast<unsigned int>(Port[i].fVoltage * 100);
				pn = pn %100;
				nn = nn %100;
				TMyString::sClear(Text[3]);
				TMyString::sAddOn2Dg(pn, Text[3]);
				TMyString::sAddOnStr(".", Text[3]);
				TMyString::sAddOn2Dg(nn, Text[3]);
				pn = static_cast<unsigned int>(Port[i].fCurrent);
				nn = static_cast<unsigned int>(Port[i].fCurrent * 1000);
				pn = pn %10;
				nn = nn %1000;
				TMyString::sClear(Text[4]);
				TMyString::sAddOn1Dg(pn, Text[4]);
				TMyString::sAddOnStr(".", Text[4]);
				TMyString::sAddOn3Dg(nn, Text[4]);
				TextRender_string24(xv[0],aTop,aClr, "  回路");
				TextRender_string24(xv[1],aTop,aClr, Text[1]);
				TextRender_string24(xv[2],aTop,aClr, Text[2]);
				TextRender_string24(xv[3],aTop,aClr, Text[3]);
				TextRender_string24(xv[4],aTop,aClr, Text[4]);
				pn = Port[i].fData.Port.ResistedCount;
				TMyString::sClear(Text[5]);
				TMyString::sAddOn3Dg(pn, Text[5]);
				TextRender_string24(xv[5],aTop,aClr, Text[5]);
				pn = Port[i].fData.Port.OnlineCount;
				TMyString::sClear(Text[6]);
				TMyString::sAddOn3Dg(pn, Text[6]);
				TextRender_string24(xv[6],aTop,aClr, Text[6]);
			}else{
				TMyString::sAddOn2Dg(aP0, Text[2]);
				TMyString::sFromStr("无数据", Text[3]);
				TextRender_string24(xv[0],aTop,aClr, "  回路");
				TextRender_string24(xv[1],aTop,aClr, Text[1]);
				TextRender_string24(xv[2],aTop,aClr, Text[2]);
				TextRender_string24(xv[3],aTop,aClr, Text[3]);
			}
		}else
		if(i<48){
			aP0 = i+1 - 40;
			TMyString::sClear(Text[1]);
			TMyString::sClear(Text[2]);
			TMyString::sAddOn2Dg(aP0, Text[1]);
			if(HandCtlBoard[i-40].fData.Common.Id){
				aClr = clNearBlack;
			}else{
				aClr = clGray;
			}
			
			if(HandCtlBoard[i-40].fData.Common.Id){
				TMyString::sAddOn2Dg(aP0 + 40, Text[2]);
				pn = static_cast<unsigned int>(HandCtlBoard[i-40].fVoltage);
				nn = static_cast<unsigned int>(HandCtlBoard[i-40].fVoltage*100);
				pn = pn %100;
				nn = nn %100;
				TMyString::sClear(Text[3]);
				TMyString::sAddOn2Dg(pn, Text[3]);
				TMyString::sAddOnStr(".", Text[3]);
				TMyString::sAddOn2Dg(nn, Text[3]);
				pn = static_cast<unsigned int>(HandCtlBoard[i-40].fCurrent);
				nn = static_cast<unsigned int>(HandCtlBoard[i-40].fCurrent*100);
				pn = pn %100;
				nn = nn %100;
				TMyString::sClear(Text[4]);
				TMyString::sAddOn2Dg(pn, Text[4]);
				TMyString::sAddOnStr(".", Text[4]);
				TMyString::sAddOn2Dg(nn, Text[4]);
				TextRender_string24(xv[0],aTop,aClr, "总线盘");
				TextRender_string24(xv[1],aTop,aClr, Text[1]);
				TextRender_string24(xv[2],aTop,aClr, Text[2]);
				TextRender_string24(xv[3],aTop,aClr, Text[3]);
				TextRender_string24(xv[4],aTop,aClr, Text[4]);
				pn = HandCtlBoard[i-40].fData.HandCtl.ResistedCount;
				TMyString::sClear(Text[5]);
				TMyString::sAddOn3Dg(pn, Text[5]);
				TextRender_string24(xv[5],aTop,aClr, Text[5]);
				pn = HandCtlBoard[i-40].fData.HandCtl.ResistedCount;
				TMyString::sClear(Text[6]);
				TMyString::sAddOn3Dg(pn, Text[6]);
				TextRender_string24(xv[6],aTop,aClr, Text[6]);
			}else{
				TMyString::sAddOn2Dg(aP0 + 40, Text[2]);
				TMyString::sFromStr("无数据", Text[3]);
				TextRender_string24(xv[0],aTop,aClr, "总线盘");
				TextRender_string24(xv[1],aTop,aClr, Text[1]);
				TextRender_string24(xv[2],aTop,aClr, Text[2]);
				TextRender_string24(xv[3],aTop,aClr, Text[3]);
			}				
		}else
		if(i<56){
			aP0 = i+1 - 48;
			TMyString::sClear(Text[1]);
			TMyString::sClear(Text[2]);
			TMyString::sAddOn2Dg(aP0, Text[1]);
			if(DirectCtlBoard[i-48].fData.Common.Id){
				aClr = clNearBlack;
			}else{
				aClr = clGray;
			}
			if(DirectCtlBoard[i-48].fData.Common.Id){
				TMyString::sAddOn2Dg(aP0 + 48, Text[2]);
				pn = static_cast<unsigned int>(DirectCtlBoard[i-48].fVoltage);
				nn = static_cast<unsigned int>(DirectCtlBoard[i-48].fVoltage * 100);
				pn = pn %100;
				nn = nn %100;
				TMyString::sClear(Text[3]);
				TMyString::sAddOn2Dg(pn, Text[3]);
				TMyString::sAddOnStr(".", Text[3]);
				TMyString::sAddOn2Dg(nn, Text[3]);
				pn = static_cast<unsigned int>(DirectCtlBoard[i-48].fCurrent);
				nn = static_cast<unsigned int>(DirectCtlBoard[i-48].fCurrent * 100);
				pn = pn %100;
				nn = nn %100;
				TMyString::sClear(Text[4]);
				TMyString::sAddOn2Dg(pn, Text[4]);
				TMyString::sAddOnStr(".", Text[4]);
				TMyString::sAddOn2Dg(nn, Text[4]);
				TextRender_string24(xv[0],aTop,aClr, "多线盘");
				TextRender_string24(xv[1],aTop,aClr, Text[1]);
				TextRender_string24(xv[2],aTop,aClr, Text[2]);
				TextRender_string24(xv[3],aTop,aClr, Text[3]);
				TextRender_string24(xv[4],aTop,aClr, Text[4]);
				pn = DirectCtlBoard[i-48].fData.DirectCtl.ResistedCount;
				TMyString::sClear(Text[5]);
				TMyString::sAddOn3Dg(pn, Text[5]);
				TextRender_string24(xv[5],aTop,aClr, Text[5]);
				pn = DirectCtlBoard[i-48].fData.DirectCtl.ResistedCount;
				TMyString::sClear(Text[6]);
				TMyString::sAddOn3Dg(pn, Text[6]);
				TextRender_string24(xv[6],aTop,aClr, Text[6]);
			}else{
				TMyString::sAddOn2Dg(aP0 + 48, Text[2]);
				TMyString::sFromStr("无数据", Text[3]);
				TextRender_string24(xv[0],aTop,aClr, "多线盘");
				TextRender_string24(xv[1],aTop,aClr, Text[1]);
				TextRender_string24(xv[2],aTop,aClr, Text[2]);
				TextRender_string24(xv[3],aTop,aClr, Text[3]);
			}				
		}
		aTop+=40;
		times++;
	}
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TRootDevVoltage::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)
					){
		
	}*/
	
	if(aKey == VK_LEFT){
		
	}else
	if(aKey == VK_RIGHT){
		
	}else
	if(aKey == VK_UP){
		if(TopIndex>0){
			TopIndex--;
			DrawItems();
		}
	}else
	if(aKey == VK_DOWN){
		if(TopIndex<60){
			TopIndex++;
			DrawItems();
		}
	}else
	if(aKey == VK_TAB){

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

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

void TRootDevVoltage::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(xs[0],10,80,30,0,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(xs[1],10,60,30,0,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(xs[2],10,60,30,0,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(xs[3],10,60,30,0,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(xs[4],10,60,30,0,y,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(xs[5],10,80,30,0,y,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(xs[6],10,80,30,0,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr(" 类型", aPanel->SText[0].Text);
	TMyString::sFromStr("序号", aPanel->SText[1].Text);
	TMyString::sFromStr("路径", aPanel->SText[2].Text);
	TMyString::sFromStr("电压(V)", aPanel->SText[3].Text);
	TMyString::sFromStr("电流(A)", aPanel->SText[4].Text);
	TMyString::sFromStr("登记数", aPanel->SText[5].Text);
	TMyString::sFromStr("在线数", aPanel->SText[6].Text);
	
	for(i=0; i<7;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[0].Visible = 1;
	}
	
	aPanel->Btn[0].Init(430,290,120,36,x,y,1,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("强制更新");
	
	for(i=0; i<1;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].Selected	= 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(10);
		aPanel->Btn[i].SelectedColor = clBlue;
	}
	
	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	aPanel->ReDrawItems();
	PanelCanvasOut();
	DrawItems();
}


void TRootDevVoltage::SendCmd()
{
	TCanHeaderID aIde;
	unsigned int Len,i;
	Len = 4;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 1;
	
	//Path 0
	Data[2] = 127;
	
	//Cmd 1
	aIde.BitF.Cmd = dfCMD_UpdateVoltage;
	
	//Prm 
	Data[3] = 255;
	
	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);
}








