#include "RuntimeData.h"
#include "EpMonitor.h"

#define dfDataLen	0x10000
#define dfDataPieceLen	0x2000

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};
static const unsigned char stHexNum[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

void TEpMonitor::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);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("系统调试->回路设备运行状态", Caption.Text);
	
	CtlIndex = 0;
	IsShowing = 0;
}

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

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

void TEpMonitor::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 TEpMonitor::DrawCaption2(void)
{	

}

void TEpMonitor::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
	IsShowing = 1;
	
	TextRender_string24(420, 320, clNearBlack, "1.按TAB键切换回路号与地址号编辑");
	TextRender_string24(420, 360, clNearBlack, "2.上下键加减1");
	TextRender_string24(420, 400, clNearBlack, "3.输入回路号地址号后按确认键");
}

void TEpMonitor::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		Caption.Show();
		DrawCaption2();
		IsShowing = 1;
		
		TextRender_string24(420, 320, clNearBlack, "1.按TAB键切换回路号与地址号编辑");
		TextRender_string24(420, 360, clNearBlack, "2.上下键加减1");
		TextRender_string24(420, 400, clNearBlack, "3.输入回路号地址号后按确认键");
		
		aPanel->ReDrawItems();
		RedrawState();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->ReDrawItems();
		RedrawState();
	}
}

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

//______________________________________________________________________________________________________________________________
void TEpMonitor::Check4Selected(void)
{
	unsigned int i;
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	switch(CtlIndex){
		case 0:
			aPanel->Edit[0].Selected = 1;
			break;
		case 1:
			aPanel->Edit[1].Selected = 1;
			break;
		default:

			break;
	}		
}

void TEpMonitor::Task1000Ms()
{
	if(IsShowing){
		RedrawState();
	}
}

void TEpMonitor::CheckPath(void)
{
	int aVal;
	aVal = aPanel->Edit[0].Str.ToInteger();
	Path0 = aVal -1;
	aVal = aPanel->Edit[1].Str.ToInteger();
	Path1 = aVal -1;
}

void TEpMonitor::RedrawState(void)
{
	int aP0, aType, aFire, aFault, aMask, HasState, HasWrote;
	aFire = 0;
	aFault = 0;
	aMask = 0;
	HasState = 0;
	
	aPanel->SText[11].TextClear();
	aPanel->SText[12].TextClear();
	aPanel->SText[13].TextClear();
	
	if(  (Path0>39) || (Path1>249)  ){
		aPanel->SText[11].Show();
		aPanel->SText[12].Show();
		aPanel->SText[13].Show();
		return;
	}
	
	if(MainCtl.fData.Split.ExistTablePort[Path0]){
		aType = Port[Path0].dTypeTable[Path1];
		TMyString::sFromStr(StrType[aType],aPanel->SText[11].Text);
		
		if(Port[Path0].StateTable[Path1] & dEpStateBitFire){
			aFire = 1;
			HasState = 1;
		}
		if(Port[Path0].StateTable[Path1] & dEpStateBitFault){
			aFault = 1;
			HasState = 1;
		}
		if(Port[Path0].StateTable[Path1] & dEpStateBitMask){
			aMask = 1;
			HasState = 1;
		}

		aPanel->SText[12].TextClear();
		HasWrote = 0;
		if(aFire){
			TMyString::sFromStr("报警",aPanel->SText[12].Text);
			HasWrote = 1;
		}
		if(aFault){
			if(HasWrote){
				TMyString::sAddOnStr("\/故障",aPanel->SText[12].Text);
			}else{
				TMyString::sFromStr("故障",aPanel->SText[12].Text);
			}
		}
		if(aMask){
			if(HasWrote){
				TMyString::sAddOnStr("\/屏蔽",aPanel->SText[12].Text);
			}else{
				TMyString::sFromStr("屏蔽",aPanel->SText[12].Text);
			}
		}
		if(HasState == 0){
			TMyString::sFromStr("正常运行          ",aPanel->SText[12].Text);
		}
		
		//aType = Port[Path0].dTypeTable[Path1];
		//TMyString::sFromStr(StrType[aType],aPanel->SText[11].Text);
	}
	
	aPanel->SText[11].Show();
	aPanel->SText[12].Show();
	aPanel->SText[13].Show();
}

void TEpMonitor::TryDec()
{
	int iVal;
	if(aPanel->Edit[0].Selected){
		iVal = aPanel->Edit[0].Str.ToInteger();
		iVal --;
		if(iVal <1)iVal = 1;
		aPanel->Edit[0].Str.FromInt(iVal);
		aPanel->Edit[0].SetPstTail();
		aPanel->Edit[0].Show();
	}else
	if(aPanel->Edit[1].Selected){
		iVal = aPanel->Edit[1].Str.ToInteger();
		iVal --;
		if(iVal <1)iVal = 1;
		aPanel->Edit[1].Str.FromInt(iVal);
		aPanel->Edit[1].SetPstTail();
		aPanel->Edit[1].Show();
	}
}

void TEpMonitor::TryInc()
{
	int iVal;
	if(aPanel->Edit[0].Selected){
		iVal = aPanel->Edit[0].Str.ToInteger();
		iVal ++;
		if(iVal > 40)iVal = 40;
		aPanel->Edit[0].Str.FromInt(iVal);
		aPanel->Edit[0].SetPstTail();
		aPanel->Edit[0].Show();
	}else
	if(aPanel->Edit[1].Selected){
		iVal = aPanel->Edit[1].Str.ToInteger();
		iVal ++;
		if(iVal >250)iVal = 250;
		aPanel->Edit[1].Str.FromInt(iVal);
		aPanel->Edit[1].SetPstTail();
		aPanel->Edit[1].Show();
	}	
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TEpMonitor::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	int aVal;
	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();
		}else
		if(aPanel->Edit[1].Selected){
			aPanel->Edit[1].KeyIn(aKey);
			aPanel->Edit[1].ReDraw();
		}
		CheckPath();
		RedrawState();
	}
	
	if(aKey == VK_UP){
		TryInc();
		CheckPath();
		RedrawState();
	}else
	if(aKey == VK_DOWN){
		TryDec();
		CheckPath();
		RedrawState();
	}else
	if(aKey == VK_TAB){
		if(CtlIndex){
			CtlIndex = 0;
		}else{
			CtlIndex = 1;
		}
		Check4Selected();
		aPanel->ReDrawItems();
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		Check4Selected();
		aPanel->ReDrawItems();
	}else
	if(aKey == VK_RETURN){
		IsShowing = 0;
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

void TEpMonitor::InitPanel(void)
{
	int x,y, aW,aH,i;
	
	x = Content.Left;
	y = Content.Top;
	aW = Content.Width;
	aH = 40;
	
	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(60,26,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(400,26,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(60,76,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(60,126,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(60,176,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);
	
	aPanel->SText[11].Init(160,76,400,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(160,126,400,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(160,176,400,30,x,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr("未知", aPanel->SText[11].Text);
	TMyString::sFromStr("未知", aPanel->SText[12].Text);
	TMyString::sFromStr("未知", aPanel->SText[13].Text);
	
	aPanel->Edit[0].Init(170,26,40,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(500,26,50,30,x,y,clNearBlack,clNearWhite);

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

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

