#include "RuntimeData.h"
#include "LiteSelfCheck.h"

void TLiteSelfCheck::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	(429,		Top,		370,   CH, 0, 0, clWhite, clMaroon);
	
	Content.Set(Left, 	Top+CH, 	Right, 	Bottom);
	
	Color = aColor;
	#undef CH

	Caption.Border.Color = clGray;

	IsShowing = 0;
	CtlIndex = 0;
	IsCheckWhat =0;
	Seq =0;
	AlramCountDown =0;
	
	IsGo = 0;
	
	CheckCmdHasSend = 0;

}

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

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

void TLiteSelfCheck::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);
	
	RenderBackGround();
}


void TLiteSelfCheck::DrawCaption2(void)
{	
	//TMyString::sFromStr("TAB键在编辑与类型列表之间切换", SText.Text);
	//SText.Show();
}

void TLiteSelfCheck::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	if(LanguageEnCn==0)TMyString::sFromStr("自检-本机", Caption.Text);
	else TMyString::sFromStr("Local Self-Check", Caption.Text);
	Caption.Show();
	
	Tick = 0;
	Seq =0;
	CheckCmdHasSend = 0;
	IsGo =1;
	IsSelfChecking = 1;
}

void TLiteSelfCheck::ShowCaption()
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
	Caption.Show();
}

//______________________________________________________________________________________________________________________________
void TLiteSelfCheck::Check4Selected(void)
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;

	if(CtlIndex == 0){
		aPanel->Btn[0].Selected = 1;
	}else
	if(CtlIndex == 1){
		aPanel->Btn[1].Selected = 1;
	}
}

void TLiteSelfCheck::ShowMsg(int p)
{
	if(LanguageEnCn==0){
		if(p==0){
			TMyString::sFromStr("自检开始", Text);
		}else{
			TMyString::sFromStr("自检完成", Text);
		}
	}else{
		if(p==0){
			TMyString::sFromStr("Self-Check Begins", Text);
		}else{
			TMyString::sFromStr("Self-Check Completed", Text);
		}
	}
	RectFillRender(4, 34, 796, 436, clNearWhite);
	TextRender_string24(360, 140, clNearBlack, clNearWhite, Text); 
}

//_____________________________________________________________________________________________________________________________

void TLiteSelfCheck::ClearOutText()
{
	TMyString::sClear(aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP0_LcdRed()
{
	TMyString::sFromStr("       LCD红色        ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP1_LcdGreen()
{
	TMyString::sFromStr("       LCD绿色       ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP2_LcdBlue()
{
	TMyString::sFromStr("       LCD蓝色      ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP3_PanelLedAllDarking()
{
	TMyString::sFromStr(" 面板指示灯全部熄灭 ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP4_PanelLedAllLighting()
{
	TMyString::sFromStr(" 面板指示灯全部点亮 ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP5_PanelLedSeq()
{
	TMyString::sFromStr(" 面板指示灯依次点亮 ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP6_BusPadLedDarkAll()
{
	TMyString::sFromStr("多线盘指示灯全部熄灭", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP7_BusPadLedLightAll()
{
	TMyString::sFromStr("总线盘指示灯全部点亮", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}


void TLiteSelfCheck::CheckCP8_DirectPadLedDarkAll()
{
	TMyString::sFromStr("多线盘指示灯全部熄灭", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP9_DirectPadLedLightAll()
{
	TMyString::sFromStr("多线盘指示灯全部点亮", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckCP10_Speaker()
{
	TMyString::sFromStr("      扬声器发声    ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::CheckFireAlram()
{
	TMyString::sFromStr("       警报声       ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::ForceStopAll()
{
	TMyString::sFromStr("      全部停止      ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::DoneAll()
{
	TMyString::sFromStr("        完成        ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TLiteSelfCheck::iRun()
{
	unsigned int m;
	m = Tick % 5;
	switch(m){
		case 0:
			RectFillRender(0, 0, 799, 479, clRed);
			break;
		case 1:
			RectFillRender(0, 0, 799, 479, clGreen);
			break;
		case 2:
			RectFillRender(0, 0, 799, 479, clBlue);
			break;
		case 3:
			RectFillRender(0, 0, 799, 479, clBlack);
			break;
		case 4:
			RectFillRender(0, 0, 799, 479, clWhite);
			break;
		case 5:
			RectFillRender(0, 0, 799, 479, clRed);
			break;
		case 6:
			RectFillRender(0, 0, 799, 479, clGreen);
			break;
		case 7:
			RectFillRender(0, 0, 799, 479, clBlue);
			break;
		case 8:
			RectFillRender(0, 0, 799, 479, clBlack);
			break;
		case 9:
			RectFillRender(0, 0, 799, 479, clWhite);
			break;
		case 10:
			RectFillRender(0, 0, 799, 479, clAqua);
			break;
		case 11:
			RectFillRender(0, 0, 799, 479, clAqua);
			break;
		case 12:
			RectFillRender(0, 0, 799, 479, clWhite);
			break;
	}
}

void TLiteSelfCheck::Task1000Ms()
{
	if(IsShowing){
		if(IsGo){
			if(CheckCmdHasSend == 0){
				if(SelfCheckSendCmd(1) == 0){
					CheckCmdHasSend = 1;
					CtlCenter.IsSendSelfCheckToPad =1;
				}
			}
			if(CheckCmdHasSend){
				iRun();
				Tick++;
				if(Tick > 12){
					GuiRedrawBase();
					GuiRedraw(0);
					IsGo = 0;
					IsShowing = 0;
					IsSelfChecking = 0;
					FinishCallBack();
				}
			}
		}else{
			IsShowing = 0;
			FinishCallBack();
		}
	}
	return;
	
	
	if(IsShowing){
		if(IsCheckWhat == 1){
			switch(Seq){
				case 0:
					CheckCP0_LcdRed();
					Seq++;
					break;
				case 1:
					CheckCP0_LcdRed();
					Seq++;
					break;
				case 2:
					CheckCP1_LcdGreen();
					Seq++;
					break;
				case 3:
					CheckCP2_LcdBlue();
					Seq++;
					break;
				case 4:
					CheckCP3_PanelLedAllDarking();
					Seq++;
					break;
				case 5:
					CheckCP4_PanelLedAllLighting();
					Seq++;
					break;
				case 6:
					CheckCP3_PanelLedAllDarking();
					Seq++;
					break;
				case 7:
					CheckCP5_PanelLedSeq();
					switch(Tick){
						case 0:	break;
						case 1:	break;
						case 2:	break;
						case 3:	break;
						case 4:	break;
						case 5:	break;
						case 6:	break;
						case 7:	break;
						case 8:	break;
						case 9:	break;
						case 10:	break;
						case 11:	break;
						case 12:	break;
						case 13:	break;
						case 14:	break;
						case 15:	break;
						case 16:	break;
						case 17:	break;
						case 18:	break;
						case 19:	break;
						case 20:	break;
						case 21:	break;
						case 22:	break;
						case 23:	break;
					}
					Tick++;
					if(Tick > 6){
						Seq++;
					}
					break;
				case 8:
					CheckCP6_BusPadLedDarkAll();
					Seq++;
					break;
				case 9:
					CheckCP7_BusPadLedLightAll();
					Seq++;
					break;
				case 10:
					CheckCP8_DirectPadLedDarkAll();
					Seq++;
					break;
				case 11:
					CheckCP9_DirectPadLedLightAll();
					Seq++;
					break;
				case 12:
					CheckCP10_Speaker();
					Seq++;
					break;
				case 13:
					Seq++;
					break;
				case 14:
					Seq++;
					break;
				case 15:
					DoneAll();
					IsCheckWhat =0;
					FinishCallBack();
					break;
				default:
					DoneAll();
					IsCheckWhat =0;
					FinishCallBack();
					break;
							
			}
		}else
		if(IsCheckWhat == 2){
			if(AlramCountDown){
				AlramCountDown--;
				TextDigitRender2Right24(260, 245 ,clNearBlack, clFrmFace, AlramCountDown);
			}else{
				DoneAll();
				RectFillRender(260, 245, 283, 268, clFrmFace);
				IsCheckWhat = 0;
				FinishCallBack();
			}
		}
	}
}

TGuiMsgReturn TLiteSelfCheck::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	TDMix aFullPath;

	switch(aKey){
		case VK_0:
		case VK_1:
		case VK_2:
		case VK_3:
		case VK_4:
		case VK_5:
		case VK_6:
		case VK_7:
		case VK_8:
		case VK_9:
		case VK_DELETE:

			break;
		case VK_LEFT:

			break;
		case VK_RIGHT:

			break;
		case VK_UP:
			if(IsCheckWhat == 0){
				//ClearOutText();
				if(CtlIndex >0){
					CtlIndex--;
				//	Check4Selected();
				//	aPanel->ReDrawItems();
				}
			}
			break;
		case VK_DOWN:
			if(IsCheckWhat == 0){
				//ClearOutText();
				if(CtlIndex <1){
					CtlIndex++;
				//	Check4Selected();
				//	aPanel->ReDrawItems();
				}
			}
			break;
		case VK_TAB:

			break;
		case dfKEY_SETUP:

			break;
		case dfKEY_QUERY:

			break;
		case VK_EXECUTE:
			//Do Check Here
			if(IsCheckWhat == 0){
				if(CtlIndex == 0){
				//	ClearOutText();
					IsCheckWhat =1;
					Seq =0;
					Tick = 0;
				}else
				if(CtlIndex == 1){
				//	ClearOutText();
					IsCheckWhat =2;
					AlramCountDown =10;
				}
			}
			break;
		case VK_RETURN:
			if(IsGo){
				CheckCancel = 1;
			}else{
				IsGo =0;
				IsShowing = 0;
				IsSelfChecking = 0;
				//ForceStopAll();
				aMsg = guiMsgReturn;
			}
			break;
	}

	return aMsg;	
}

void TLiteSelfCheck::SendCmdClearAll()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearAllRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = 0xFe;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TLiteSelfCheck::SendCmdClearOnePort(unsigned char aCc)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearSinglePortRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TLiteSelfCheck::SendCmdClearMultAddr(unsigned char aCc, unsigned char sAddr, unsigned char eAddr, unsigned char aType)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearSinglePortRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	Data[3] = sAddr;
	Data[4] = eAddr;
	Data[5] = aType;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);	
}

void TLiteSelfCheck::SendCmdClearOneAddr(unsigned char aCc, unsigned char aAddr, unsigned char aType)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearSinglePortAddrRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	Data[3] = aAddr;
	Data[4] = aAddr;
	Data[5] = aType;
	
	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 TLiteSelfCheck::SelfCheckSendCmd(unsigned char SatrtStop)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =2;
	
	aIde.BitF.Cmd = cmSelfCheck;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = SatrtStop;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	return InnerCan.CmdSendCustomWithReturn(aIde, Data, Len);		
}

int TLiteSelfCheck::ExtRequst(unsigned char Prm)
{
	IsCheckWhat = 0;
	IsGo = 0;
	IsShowing = 0;
	IsSelfChecking = 0;
	return 0;
}


