#include "PanelSelfCheck.h"

#include "RuntimeData2.h"
#include "Runtime.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define dfColor_NoExist		clSilver
#define dfColor_WaitCheck	clNearBlack
#define dfColor_OnCheck		clMedOrange
#define dfColor_CheckDone	clMedGreen

#define dfLine1_Top	100
#define dfLine2_Top	299
#define dfLine3_Top	349

#define dfMaster_Top	180
#define dfSlaver_Top	100
#define dfBtm		349

static const short svGridMasterX	[10]	= {10, 130, 220, 340, 430,	550, 496, 572,  648, 724};
static const short svGridSlaveX	[10] 	= {10, 130, 220, 340, 430,	550, 496, 572,  648, 724};
static const short svGridMasterY	[10] 	= {135, 165, 195, 225, 255,	285, 315, 345,  375, 405};
static const short svGridSlaveY	[10] 	= {105, 135, 165, 195, 225,	255, 285, 315,  345, 375};

static const char ccTip  [10] [64]={
	//"网络设置" 1111_  1=allow 0=NotAllow S=BySet
	"本机为网络主机,发送自检命令到从机",
	"正在检查备电",
	"",
	"正在检查回路",
	"正在检查总线盘",
	"正在检查直线盘",
	"正在检查回路母板",
	"正在检查通讯板",
	"正在检查记忆单元",
	"正在查询网络从机:",
};

static const char ccState  [16] [64]={
	//"网络设置" 1111_  1=allow 0=NotAllow S=BySet
	"接收到从机回复信息",
	"正在检查回路",
	"正在检查总线盘",
	"正在检查直线盘",
	"正在检查回路母板",
	"正在检查通讯板",
	"正在检查记忆单元",
};

void TPanelSelfCheck::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);
	Content.Set(Left, Top+CH, Right, Bottom);
	
	SText.Init	(519,		Top,		280,   CH, 0, 0, clWhite, clMaroon);
	STextTip.Init(519,	Top + 40,	280,   CH, 0, 0, clWhite, clMoneyGreen);
	Color = aColor;
	#undef CH

	AskingEnter = 1;
	DoIndex = 0;
	CheckDone = 0;
	Checking = 0;
	
	Caption.Border.Color = clGray;
	
	vEdit.Init(666,140,80,36,Left,Top,1,clTeal);
	vEdit.Visible = 0;
	vEdit.Enable = 1;
	vEdit.Selected = 1;
	
}

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

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

void TPanelSelfCheck::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);	
}

void TPanelSelfCheck::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);
	
	RenderContent();
}

void TPanelSelfCheck::DrawCaption(void)
{	
	Caption.Show();
	DrawCaption1();
}

void TPanelSelfCheck::DrawCaption1(void)
{	
	if(MainCtl.fData.Split.IsMaster)
		SText.Show();
}

void TPanelSelfCheck::DrawStateIdf_0(void)
{
	//正常 离线 报警 故障 屏蔽 重码 污染 输入开 输出开
	#define GS	20
	#define TS	60	
	
	unsigned int aLeft, aTop, aTopR;
	
	aTop = Content.Top +4;
	aTopR = Content.Top +6;	
	aLeft = Content.Left + 20;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, dfColor_NoExist);
	aLeft += GS;
	TextRender_string24(aLeft,aTop,clNearBlack, "不存在");	
	aLeft += TS + 30;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, dfColor_WaitCheck);
	aLeft += GS;
	TextRender_string24(aLeft,aTop,clNearBlack, "待检");	
	aLeft += TS;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, dfColor_OnCheck);
	aLeft += GS;
	TextRender_string24(aLeft,aTop,clNearBlack, "在检");	
	aLeft += TS;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, dfColor_CheckDone);
	aLeft += GS;
	TextRender_string24(aLeft,aTop,clNearBlack, "完成");	
	aLeft += TS;
	
	#undef GS
	#undef TS	
}

void TPanelSelfCheck::DrawOngoGrid(void)
{
	//正常 离线 报警 故障 屏蔽 重码 污染 输入开 输出开
	#define GS	20
	#define TS	60	
	
	unsigned int i, aLeft, aTop, aClr;
	
	aTop = Content.Top +30;
	aLeft = Content.Left + 6;
	
	TextRender_string24(aLeft,aTop,clNearBlack, "控制器1-64");	

	aLeft += 130;
	for(i=0; i<64; i++){
		if(Machine[i].Exist){
			if(Machine[i].CheckState == 0){
				aClr = dfColor_WaitCheck;
			}else
			if(Machine[i].CheckState == 1){
				aClr = dfColor_OnCheck;
			}else
			if(Machine[i].CheckState == 2){
				aClr = dfColor_CheckDone;
			}
		}else{
			aClr = dfColor_NoExist;
		}
		RectFillRender( aLeft, aTop, aLeft + 9, aTop + 22, aClr);
		aLeft = aLeft + 10;
	}
	
	#undef GS
	#undef TS	
}

void TPanelSelfCheck::DrawFixText()
{
	const short *aL,*aT;
	int i;
	if(MainCtl.fData.Split.IsMaster){
		aL = svGridMasterX;
		aT = svGridMasterY;
	}else{
		aL = svGridSlaveX;
		aT = svGridSlaveY;
	}
	
	TextRender_string24(aL[0]+30,aT[0],clNearBlack, "登记数量");
	TextRender_string24(aL[2]+30,aT[0],clNearBlack, "离线数量");
	TextRender_string24(aL[4]+30,aT[0],clNearBlack, "故障数量");
	
	for(i=0; i<3; i++){
		TextRender_string24(aL[i*2],aT[1],clNearBlack, "备电");
		TextRender_string24(aL[i*2],aT[2],clNearBlack, "存储");
		TextRender_string24(aL[i*2],aT[3],clNearBlack, "通讯板");
		TextRender_string24(aL[i*2],aT[4],clNearBlack, "回路");
		TextRender_string24(aL[i*2],aT[5],clNearBlack, "总线盘");
		TextRender_string24(aL[i*2],aT[6],clNearBlack, "直线盘");
		TextRender_string24(aL[i*2],aT[7],clNearBlack, "回路母板");
		TextRender_string24(aL[i*2],aT[8],clNearBlack, "外联动卡");
		TextRender_string24(aL[i*2],aT[9],clNearBlack, "终端设备");
	}
}

void TPanelSelfCheck::DrawList(void)
{
	const short *aL,*aT;
	
	if(MainCtl.fData.Split.IsMaster){
		aL = svGridMasterX;
		aT = svGridMasterY;
	}else{
		aL = svGridSlaveX;
		aT = svGridSlaveY;
		ViewIndex = MainCtl.fData.Split.MyNum;
	}
	
	if(ViewIndex < 64){
		if(Machine[ViewIndex].CheckState == 2){
			TextDigitRender3Right24(aL[1], aT[1], clNearBlack, Color, Machine[ViewIndex].Registed.Bat);
			TextDigitRender3Right24(aL[1], aT[2], clNearBlack, Color, Machine[ViewIndex].Registed.Mem);
			TextDigitRender3Right24(aL[1], aT[3], clNearBlack, Color, Machine[ViewIndex].Registed.CommBoardCount);
			TextDigitRender3Right24(aL[1], aT[4], clNearBlack, Color, Machine[ViewIndex].Registed.CircultCount);
			TextDigitRender3Right24(aL[1], aT[5], clNearBlack, Color, Machine[ViewIndex].Registed.HandCtlCount);
			TextDigitRender3Right24(aL[1], aT[6], clNearBlack, Color, Machine[ViewIndex].Registed.DirectCtlCount);
			TextDigitRender3Right24(aL[1], aT[7], clNearBlack, Color, Machine[ViewIndex].Registed.MotherBoardCount);
			TextDigitRender3Right24(aL[1], aT[8], clNearBlack, Color, Machine[ViewIndex].Registed.ExLinkBoardCount);
			TextDigitRender3Right24(aL[1], aT[9], clNearBlack, Color, Machine[ViewIndex].Registed.EpDevCount);
			
			TextDigitRender3Right24(aL[3], aT[1], clNearBlack, Color, Machine[ViewIndex].Offline.Bat);
			TextDigitRender3Right24(aL[3], aT[2], clNearBlack, Color, Machine[ViewIndex].Offline.Mem);
			TextDigitRender3Right24(aL[3], aT[3], clNearBlack, Color, Machine[ViewIndex].Offline.CommBoardCount);
			TextDigitRender3Right24(aL[3], aT[4], clNearBlack, Color, Machine[ViewIndex].Offline.CircultCount);
			TextDigitRender3Right24(aL[3], aT[5], clNearBlack, Color, Machine[ViewIndex].Offline.HandCtlCount);
			TextDigitRender3Right24(aL[3], aT[6], clNearBlack, Color, Machine[ViewIndex].Offline.DirectCtlCount);
			TextDigitRender3Right24(aL[3], aT[7], clNearBlack, Color, Machine[ViewIndex].Offline.MotherBoardCount);
			TextDigitRender3Right24(aL[3], aT[8], clNearBlack, Color, Machine[ViewIndex].Offline.ExLinkBoardCount);
			TextDigitRender3Right24(aL[3], aT[9], clNearBlack, Color, Machine[ViewIndex].Offline.EpDevCount);
			
			TextDigitRender3Right24(aL[5], aT[1], clNearBlack, Color, Machine[ViewIndex].Fault.Bat);
			TextDigitRender3Right24(aL[5], aT[2], clNearBlack, Color, Machine[ViewIndex].Fault.Mem);
			TextDigitRender3Right24(aL[5], aT[3], clNearBlack, Color, Machine[ViewIndex].Fault.CommBoardCount);
			TextDigitRender3Right24(aL[5], aT[4], clNearBlack, Color, Machine[ViewIndex].Fault.CircultCount);
			TextDigitRender3Right24(aL[5], aT[5], clNearBlack, Color, Machine[ViewIndex].Fault.HandCtlCount);
			TextDigitRender3Right24(aL[5], aT[6], clNearBlack, Color, Machine[ViewIndex].Fault.DirectCtlCount);
			TextDigitRender3Right24(aL[5], aT[7], clNearBlack, Color, Machine[ViewIndex].Fault.MotherBoardCount);
			TextDigitRender3Right24(aL[5], aT[8], clNearBlack, Color, Machine[ViewIndex].Fault.ExLinkBoardCount);
			TextDigitRender3Right24(aL[5], aT[9], clNearBlack, Color, Machine[ViewIndex].Fault.EpDevCount);
		}		
	}
}

void TPanelSelfCheck::Show(void)
{
	DrawSelf();
	Caption.Enable = 1;
	
	LoadData();
	
	if(MainCtl.fData.Split.IsMaster){
		TMyString::sFromStr("组网系统自检-本机为网络主机", Caption.Text);
	}else{
		TMyString::sFromStr("本机系统自检", Caption.Text);
	}
	TMyString::sFromStr("选定机器号查看", SText.Text);
	DrawCaption();
	if(MainCtl.fData.Split.IsMaster){
		DrawStateIdf_0();
		DrawOngoGrid();
		TextRender_string24(vEdit.Bound.Left - 20,vEdit.Bound.Top - 30,clNearBlack, "控制器序号");
		vEdit.Str.FromInt(MainCtl.fData.Split.MyNum);
		vEdit.Show();
		HorizLineRender(10, 		svGridMasterY[0] - 6, 			780, 			clGray);
		VertLineRender(svGridMasterX[1] + 80,		svGridMasterY[0]-5,			308,		clGray);
		VertLineRender(svGridMasterX[3] + 80,		svGridMasterY[0]-5,			308,		clGray);
		VertLineRender(svGridMasterX[5] + 80,		svGridMasterY[0]-5,			308,		clGray);
	}else{
		VertLineRender(svGridMasterX[1] + 80,		svGridSlaveY[0],			350,		clGray);
		VertLineRender(svGridMasterX[3] + 80,		svGridSlaveY[0],			350,		clGray);
		VertLineRender(svGridMasterX[5] + 80,		svGridSlaveY[0],			350,		clGray);
	}
	
	DrawFixText();
	DrawList();	
}

void TPanelSelfCheck::ReDraw(void)
{
	Caption.Enable = 1;
	DrawCaption();
	if(MainCtl.fData.Split.IsMaster){
		DrawStateIdf_0();
		DrawOngoGrid();
	}
	
	DrawList();	
}

void TPanelSelfCheck::LoadData(void)
{
	int i;
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		Machine[i].Exist = MainCtl.fData.Split.ExistSlaveTabel[i];
	}
	if(  (MainCtl.fData.Split.MyNum >0) && (MainCtl.fData.Split.MyNum < 65) ){
		Machine[MainCtl.fData.Split.MyNum-1].Exist = 1;
	}
}	

TGuiMsgReturn TPanelSelfCheck::KeyIn(unsigned char aKey)
{
	int ind, i, F;
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn MBoxMsg = guiMsgNone;
	//Up or Down One Index___________________
	if(AskingEnter){
		MBoxMsg = MessageBox.KeyIn(aKey);
		if(MBoxMsg == guiMsgOkAndReturn){
			AskingEnter = 0;
			Checking = 1;
			Show();
			return guiMsgNone;
		}
		if(MBoxMsg == guiMsgReturn){
			return guiMsgReturn;
		}else{
			return guiMsgNone;
		}
	}else{
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				vEdit.KeyIn(aKey);	
				vEdit.Show();
				break;
			case VK_UP:
				F = 0;
				ind = vEdit.Str.ToInteger();
				for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
					ind--;
					if(ind < 1)ind = 64;
					if(Machine[ind-1].Exist){
						vEdit.Str.FromInt(ind);
						F = 1;
						break;
					}
				}
				if(F){	
					vEdit.Show();
				}
				break;
			case VK_DOWN:
				F = 0;
				ind = vEdit.Str.ToInteger();
				for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
					ind++;
					if(ind > 64)ind = 1;
					if(Machine[ind-1].Exist){
						vEdit.Str.FromInt(ind);
						F = 1;
						break;
					}
				}
				if(F){	
					vEdit.Show();
				}
				break;
			case VK_TAB:

				break;
			case VK_EXECUTE:
				break;
		}
	}
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	//____________________________________________________________________<- List View

	return aMsg;
}

void TPanelSelfCheck::ShowAskingEnter()
{
	MessageBox.Init(0, 36, 800, 406, 0, 0, clMoneyGreen) ;
	MessageBox.SetBtnOkCancel();
	MessageBox.SetReturnValue(guiMsgNone, guiMsgOkAndReturn, guiMsgReturn, guiMsgNone);
	MessageBox.Show();
	MessageBox.DoWait4EnterSelfCheck();
	AskingEnter = 1;
}

void TPanelSelfCheck::DoSequence()
{
	if(Checking == 1){
		//if(ccTip)
	}
}

void TPanelSelfCheck::Task1000Ms()
{
	
}

