#include "fNetworkRegRemove.h"
#include "runtime.h"

#define dDescpOffSet	0x1000

void TfNetworkRegRemove::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;
	IsViewList = 0;
	CtlIndex = 1;
	Done = 0;
	
	Sta = 0;
}

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

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

void TfNetworkRegRemove::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 TfNetworkRegRemove::DrawCaption2(void)
{	
	TMyString::sFromStr("TAB键在面板与机器列表之间切换", SText.Text);
	SText.Show();
}

void TfNetworkRegRemove::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	ShowCaption();
	DrawCaption2();
	
	//TextRender_string24(42,200,clNearBlack,"设置键向左选择,查询键向右选择,上下键在添加栏与删除栏之间切换");
}

void TfNetworkRegRemove::FullRedraw(int Prm)
{	
	if(Prm == 0){
		IsShowing = 1;
		RenderBackGround();
		DrawSelf();
		ShowCaption();
		DrawCaption2();
		//TextRender_string24(20,200,clNearBlack,"设置键向左选择,查询键向右选择,上下键在添加栏与删除栏之间切换");
		
		aPanel->Show();
		ShowResult();
		//DrawList();
	}else
	if(Prm == 1){
		//RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		//TextRender_string24(20,200,clNearBlack,"设置键向左选择,查询键向右选择,上下键在添加栏与删除栏之间切换");
		
		aPanel->Show();
		ShowResult();
		//DrawList();
		
		//if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TfNetworkRegRemove::ShowCaption()
{
	TMyString::sFromStr("系统安装->清除组网配置",  Caption.Text);
	Caption.Show();
}

void TfNetworkRegRemove::SetPanelCaptionColor()
{
	if(IsViewList == 0){
		aPanel->Caption.Color = clDeepBlue;
		aPanel->Caption.TextColor = clNearWhite;
	}else{
		aPanel->Caption.Color = clMedGray;
		aPanel->Caption.TextColor = clNearWhite;
	}
}

//______________________________________________________________________________________________________________________________
void TfNetworkRegRemove::InitPanel(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	//ClearData();
	//LoadData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(60,3,676,310,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("清除组网配置", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->SText[0].Init(200,10,80,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(10,60,80,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(10,100,80,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(10,140,80,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(10,180,80,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(10,240,80,30,x,y,clNearBlack,clFrmFace);

	for(i=0; i<6; i++)
		aPanel->SText[i].TextColor = clNearBlack;
		aPanel->SText[i].SetText(" ", 24);

	aPanel->Show();
	ShowDetail();
	ShowResult(0);
	
	//Check4Selected();
	//aPanel->ReDrawItems();
	
	//RectFillRender(80,149,646,150,clGray);
	//DrawList();	
}

//_____________________________________________________________________________________________________________________________

void TfNetworkRegRemove::ClearData()
{
	int i,x;
	unsigned int aAddr;
	//Clear
	for(i=0; i<64; i++){
		aAddr = dAddrSdRam_Register  + i*4;	
		*(volatile unsigned int *)(aAddr) = 0;
	}
	for(i=0; i<64; i++){
		for(x=0; x<64; x++){
			aAddr = dAddrSdRam_Register  + dDescpOffSet + i*64 + x;	
			*(volatile unsigned int *)(aAddr) = 0;
		}
	}
}

void TfNetworkRegRemove::LoadData()
{
	int i,x;
	unsigned int aAddr;
	//Load Here
	Count = 0;
	
	//Clac
	PageCount = Count / 10;
	if(Count % 10){
		PageCount++;
	}
	PageIndex = 0;
}

void TfNetworkRegRemove::DrawList()
{
	int i, x, y, z;
	unsigned char aP0;
	unsigned int aClr, aAddr, wc;

	if(IsViewList)aClr = clBlue; else aClr = clDeepGray;

	RectFillRender		(13, 	239, 	786, 268, aClr);
	TextRender_string24	(18, 	241 ,	clNearWhite, 	"机器列表 数量:");
	TextDigitRender2Right24 (200,241 ,  clNearWhite, Count);
	
	RectFillRender(6,232,10,430,aClr);
	RectFillRender(6,232,792,236,aClr);
	RectFillRender(6,430,792,434,aClr);
	RectFillRender(788,232,792,434,aClr);
	
	RectFillRender(11,269,787,429,clMoneyGreen);
	
	i = PageIndex * 10;
	wc =0;
	for(; i<Count; i++){
		x = 10;
		y = wc * 32 + 196;
		
		aAddr = dAddrSdRam_Register  + i*4;	
		aP0 = *(volatile unsigned int *)(aAddr);
		
		TextDigitRender2_24 (x,	y, clBlack, i);
		TextRender_string24	(x+24,y, clBlack,".");
		TextDigitRender3_24	(x+36,y,clBlack, aP0);
		
		aAddr = dAddrSdRam_Register  + dDescpOffSet + i*64;
		for(z=0; z<64; z++){
			dText[z] = *(volatile unsigned char *)(aAddr);
			aAddr++;
		}
		TextRender_string24	(x+84,y,clBlack,dText);

		wc++;
		if(wc >= 10)break;
	}
	i = PageIndex * 10;
	TVScrollBar::sDrawByPoint(767, 270, 786, 428, aClr, clGray, Count, 14, i);
}

void TfNetworkRegRemove::ShowDetail()
{
	int i;
	if(MainCtl.fData.Split.NetworkMode == 1){
		aPanel->SText[1].SetText("本机为集中机", 24);
		aPanel->SText[2].SetText("本机机器号:", 24);
		TMyString::sAddOn2Dg(MainCtl.fData.Split.MyNum, aPanel->SText[2].Text);
		aPanel->SText[3].SetText("区域机号列表:", 24);
		aPanel->SText[4].SetText(" ", 24);
		for(i=0; i<ProtoC2C.SlaveCount; i++){
			TMyString::sAddOn2Dg(ProtoC2C.SlaveList[i], aPanel->SText[4].Text);
		}
		
		aPanel->SText[5].SetText("按确认键清除区域机列表", 24);
		ShowResult(1);
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		aPanel->SText[1].SetText("本机为区域机", 24);
		aPanel->SText[2].SetText("本机机器号:", 24);
		TMyString::sAddOn2Dg(MainCtl.fData.Split.MyNum, aPanel->SText[2].Text);
		aPanel->SText[3].SetText("挂载的集中机号:", 24);
		aPanel->SText[4].SetText(" ", 24);
		if(ProtoC2C.MasterNum)TMyString::sAddOn2Dg(ProtoC2C.MasterNum, aPanel->SText[4].Text);
		aPanel->SText[5].SetText("按确认键清除挂载集中机", 24);
		ShowResult(1);
	}else{
		aPanel->SText[1].SetText("本机为单机", 24);
		aPanel->SText[2].SetText("本机机器号:", 24);
		TMyString::sAddOn2Dg(MainCtl.fData.Split.MyNum, aPanel->SText[2].Text);
		aPanel->SText[3].SetText(" ", 24);
		aPanel->SText[4].SetText(" ", 24);
		aPanel->SText[5].SetText("无可用操作", 24);
		ShowResult(-1);
	}
	
	aPanel->Show();
}

void TfNetworkRegRemove::ShowResult()
{
	if(Sta == 0){
		aPanel->SText[0].SetText("        ",24);
	}else
	if(Sta == 1){
		aPanel->SText[0].SetText("清除成功",24);
	}else
	if(Sta == -1){
		aPanel->SText[0].SetText("操作失败",24);
	}
	aPanel->SText[0].Show();
}

void TfNetworkRegRemove::ShowResult(int aSta)
{
	Sta = aSta;
	ShowResult();
}

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

void TfNetworkRegRemove::Task1000Ms()
{
	if(IsShowing){

	}
}

void TfNetworkRegRemove::Check4Selected()
{
	aPanel->Btn[1].Selected = 0;
	aPanel->Btn[2].Selected = 0;
	aPanel->Edit[2].Selected = 0;

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

}

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

	switch(aKey){
		case VK_EXECUTE:
			if(  (MainCtl.fData.Split.NetworkMode == 1) || (MainCtl.fData.Split.NetworkMode == 2) ){
				ProtoC2C.MasterNum = 0;
				ProtoC2C.SlaveCount = 0;
				for(x=0; x<dNETWORK_MACHINE_COUNT; x++)ProtoC2C.SlaveList[x] =0;
				
				Record.SaveNwCfg(0,0,ProtoC2C.SlaveList);
				Record.ReadNwCfg();
				
				Record.OperateAdd_ClearNetworkCfg();
				ShowDetail();
			}
			break;
		case VK_RETURN:
			IsShowing = 0;
			aMsg = guiMsgReturn;
			break;
		default:
			ShowResult(0);
			break;
	}
	return aMsg;
	
	if(IsViewList){
		switch(aKey){
			case VK_UP:
				if(PageIndex >0){
					PageIndex--;
					DrawList();
				}
				break;
			case VK_DOWN:
				if(PageIndex <(PageCount-1)){
					PageIndex++;
					DrawList();
				}
				break;
			case VK_TAB:
				IsViewList = 0;
				DrawList();
				Check4Selected();
				SetPanelCaptionColor();
				aPanel->ReDrawItems();
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}		
	}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_DELETE:
				if(CtlIndex == 1){
					aPanel->Edit[2].KeyIn(aKey);
					aPanel->Edit[2].Show();
				}
				break;
			case VK_LEFT:
				if(CtlIndex == 0){
					CtlIndex = 1;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}else
				if(CtlIndex == 1){
					aPanel->Edit[2].KeyIn(aKey);
					aPanel->Edit[2].Show();
				}else
				if(CtlIndex == 2){
					CtlIndex--;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}
				break;
			case VK_RIGHT:
				if(CtlIndex == 1){
					if(aPanel->Edit[2].Pst > 0){
						CtlIndex++;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}else{
						aPanel->Edit[2].KeyIn(aKey);
						aPanel->Edit[2].Show();
					}
				}
				break;
			case VK_UP:
				if(CtlIndex == 1){
					CtlIndex = 0;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}else
				if(CtlIndex == 2){
					CtlIndex = 0;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex == 0){
					CtlIndex = 2;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}
				break;
			case dfKEY_SETUP:
				if(CtlIndex != 1){
					CtlIndex = 1;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}
				break;
			case dfKEY_QUERY:
				if(CtlIndex  == 1){
					CtlIndex++;
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}
				break;
			case VK_TAB:
				IsViewList = 1;
				DrawList();
				Check4Selected();
				SetPanelCaptionColor();
				aPanel->ReDrawItems();
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					IsViewList = 1;
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
				}else
				if(CtlIndex == 1){
					IsShowing = 0;
					aMsg = guiMsgReturn;
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}
	return aMsg;	
}

void TfNetworkRegRemove::SendfNetMsg(int Len)
{
	
}

void TfNetworkRegRemove::PushfNetMsg(unsigned char *data, int Len)
{
	
}

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

