#include "ECanNetwork.h"
#include "XBox.h"
#include "Runtime.h"

void TECanNetwork::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;
	BoundCaption.SetBySize(Left,Top,Width, CH);
	Content.Set(Left, Top+CH, Right, Bottom);
	
	Color = aColor;
	
	BoxBoundCfg.Set(Left + 100, Top+56, Right -145, Bottom -60);
	
	CtlIndex = 0;
	TextHasClear =1;
	
	vtValue[0] = 0;
	vtValue[1] = 0;
	vtValue[2] = 0;
	vtValue[3] = 0;

	#undef CH
}

void TECanNetwork::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);
	
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
	
	RectFillRender(BoundCaption.Left, BoundCaption.Top, BoundCaption.Right, BoundCaption.Bottom, clMaroon);
	TextRender_string24(BoundCaption.Left+6, BoundCaption.Top +3, clNearWhite, clMaroon, "系统设置->组网配置"); 
	
	TextRender_string24(Content.Left+100, Content.Bottom -98, clNearBlack, Color, "1.左右键,数字键,删除键编辑内容");
	TextRender_string24(Content.Left+100, Content.Bottom -64, clNearBlack, Color, "2.上下键, TAB键切换编辑项,选中保存设定按钮按确认键保存"); 
}


void TECanNetwork::Show(void)
{
	DrawSelf();
}

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


void TECanNetwork::Check4Selected()
{
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	switch(CtlIndex){
		case 0:	aPanel->Edit[0].Selected = 1; break;
		case 1:	aPanel->Edit[1].Selected = 1; break;
		case 2:	aPanel->Btn[0].Selected = 1; break;
		case 3:	aPanel->Btn[1].Selected = 1; break;
		default:	CtlIndex = 0; aPanel->Edit[0].Selected = 1; break;
	}
}

void TECanNetwork::LoadSysCfg()
{
	vtValue[0] = MainCtl.fData.Split.NetworkMode;
	vtValue[1] = MainCtl.fData.Split.MyNum;
}

void TECanNetwork::DrawAllItems()
{
	if(vtValue[0] == 2){
		aPanel->Edit[0].Str.FromStr("区域机");
		aPanel->Btn[1].Caption.FromStr("向集中机请求日期时间更新");
		aPanel->Btn[1].Enable = 1;
	}else
	if(vtValue[0] == 1){
		aPanel->Edit[0].Str.FromStr("集中机");
		aPanel->Btn[1].Caption.FromStr("  同步各区域机日期时间  ");
		aPanel->Btn[1].Enable = 1;
	}else{
		aPanel->Edit[0].Str.FromStr("单机");
		aPanel->Btn[1].Caption.FromStr("  同步各区域机日期时间  ");
		aPanel->Btn[1].Enable = 0;
	}
	aPanel->Edit[0].Show();
	aPanel->Btn[1].Show();
	
	TMyString::sFrom2Dg(vtValue[1], aPanel->Edit[1].Str.Text);
	aPanel->Edit[1].SetPstTail();;
	aPanel->Edit[1].Show();
}

void TECanNetwork::WriteSysCfg()
{
	int iVal;
	ValOk = 1;
	iVal = aPanel->Edit[1].Str.ToInteger();
	if( (iVal <1) || (iVal > 64) ){
		ValOk = 0;
	}else{
		vtValue[1] = iVal;
	}
	
	if(ValOk){
		//Save To Mem Here
		Record.WriteMainSetMainCtl(vtValue[0], vtValue[1]);
	}
}

void TECanNetwork::sRecoverSetDefault()
{
	Record.WriteMainSetMainCtl(0, 1);
}

void TECanNetwork::CheckClearText()
{
	if(TextHasClear == 0){
		TextHasClear = 1;
		TextRender_string24(260, 82, clNearBlack, Color,	 "                       ");
	}	
}

TGuiMsgReturn TECanNetwork::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned int GoWrite;
	unsigned char pNumOld;
	
	switch(aKey){
		case VK_DELETE: 
		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_LEFT:
		case  VK_RIGHT:
			if(CtlIndex == 0){
				if(vtValue[0] == 1){
					vtValue[0] = 2;
					aPanel->Edit[0].Str.FromStr("区域机");
				}else
				if(vtValue[0] == 2){
					vtValue[0] = 0;
					aPanel->Edit[0].Str.FromStr("单机");
				}else{
					vtValue[0] = 1;
					aPanel->Edit[0].Str.FromStr("集中机");
				}
				DrawAllItems();
			}else
			if(CtlIndex == 1){
				aPanel->Edit[1].KeyIn(aKey);
				aPanel->Edit[1].Show();
			}
			CheckClearText();
			break;
		case VK_UP:
			if(CtlIndex >0){
				CtlIndex--;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			CheckClearText();
			break;
		case VK_DOWN:
			if(CtlIndex <3){
				CtlIndex++;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			CheckClearText();
			break;
		case VK_TAB:
			if(CtlIndex <3){
				CtlIndex++;
				Check4Selected();
				aPanel->ReDrawItems();
			}else{
				CtlIndex = 0;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			CheckClearText();
			break;
		case VK_EXECUTE:
			//Save Data Here
			if(CtlIndex == 2){
				pNumOld = MainCtl.fData.Split.MyNum;
				WriteSysCfg();
				Record.ReadMainSet();
				if(  (ValOk) && (vtValue[1] == MainCtl.fData.Split.MyNum) && (vtValue[1] == MainCtl.fData.Split.MyNum)  ){
					Record.OperateAdd_SetCanNetwork(MainCtl.fData.Split.NetworkMode, pNumOld);
					TextHasClear = 0;
					TextRender_string24(260, 82, clNearBlack, Color, 	"        保存成功        ");
				}else{
					TextHasClear = 0;
					TextRender_string24(260, 82, clRed, 	 Color, 	 "保存失败,请检查配置内容");
				}
				LoadSysCfg();
				DrawAllItems();
			}else
			if(CtlIndex == 3){
				if(vtValue[0] ==1){
					//Set DateTime
					ProtoC2C.DateTimeSet();
				}else
				if(vtValue[0] ==2){
					//Req DateTime
					ProtoC2C.DateTimeReq();
				}
			}
			break;
		case VK_RETURN:
			aMsg = guiMsgReturn;
			break;
	}
	
	return aMsg;
}


void TECanNetwork::InitPanel(void)
{
	int x,y, aW,aH,i;
	
	x = Content.Left;
	y = Content.Top;
	aW = 400;
	aH = 260;
	
	aPanel->Init(100,10,aW,aH,x,y,Color, bvRaised);
	
	aPanel->Caption.Init(101,11,aW-3,32,x,y,clFrmFace,Color);
	aPanel->Caption.Text[0] = 0;
	
	x = aPanel->Bound.Left;
	y = aPanel->Bound.Top;
	
	aPanel->SText[0].Init(130,40,60,30,x,y,clNearBlack,Color);
	aPanel->SText[1].Init(130,80,60,30,x,y,clNearBlack,Color);

	TMyString::sFromStr("本机模式:", aPanel->SText[0].Text);
	TMyString::sFromStr("本机地址:", aPanel->SText[1].Text);

	aPanel->Edit[0].Init(248,40,300,30,x,y,1,clNearWhite);
	aPanel->Edit[1].Init(248,80,300,30,x,y,1,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[1].SetMaxLen(2);
	
	aPanel->Btn[0].Init(248,120,300,38,x,y,1,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("        保存设定");
	aPanel->Btn[0].SelectedColor = clBlue;
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[0].Visible = 1;
	
	aPanel->Btn[1].Init(248,160,300,38,x,y,1,clFrmFace);
	aPanel->Btn[1].Caption.FromStr("xxxxxxxx");
	aPanel->Btn[1].SelectedColor = clBlue;
	aPanel->Btn[1].SetCaptionLeftSpacing(6);
	aPanel->Btn[1].Enable = 1;
	aPanel->Btn[1].Visible = 1;
	
	//aPanel->Caption.Visible = 1;
	
	//aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
}





