#include "CanNetworkMask.h"
#include "ProtoC2C.h"
#include "Runtime.h"
#include "server.h"

#define dDescpOffSet	0x1000


#define dAddrSdRam_TypeList		(dAddrSdRam_Commom)
#define dAddrSdRam_DescpGotList	(dAddrSdRam_Commom	+ 256)
#define dAddrSdRam_DescpContent	(dAddrSdRam_Commom	+ 1024)
#define dAddrSdRam_ExistTable		(dAddrSdRam_Commom	+ 0x80000)

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

const int xSingle = 160;
const int xSlave = 270;
const int xMaster = 404;
const int xMask = 20;
const int xUnMask = 130;

extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
}

void TCanNetworkMask::Init()
{
	IsShowing = 0;
	IsOnGoing = 0;
	LoadingIndex = 0;
	CtlIndex = 0;
	Done = 0;
	
	IsPermission =0;
	
	NwMode = MainCtl.fData.Split.NetworkMode;
	
	Start2EndNumValid = 1;
}

void TCanNetworkMask::sDelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TCanNetworkMask::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

void TCanNetworkMask::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "局域网->网络屏蔽");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "本机号:");
		if(MainCtl.fData.Split.NetworkMode == 1){
			TextRender_string24(Lv.Grid.Left[0] + 126, Lv.Grid.Top[0] +6, clNearWhite, "集中机");
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			TextRender_string24(Lv.Grid.Left[0] + 126, Lv.Grid.Top[0] +6, clNearWhite, "区域机");
			TextRender_string24(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] +6, clNearWhite, "集中机号:");
			TextDigitRender2_24(Lv.Grid.Left[0] + 516, Lv.Grid.Top[0] +6, clNearWhite, ProtoC2C.MasterNum);
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 126, Lv.Grid.Top[0] +6, clNearWhite, "单机  ");
		}
			TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +40, clNearWhite, "网络机器数:");
		
		TextRender_string24(Lv.Grid.Left[0] + 176, Lv.Grid.Top[0] +40, clNearWhite, "台");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "网络屏蔽号段");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +40, clNearWhite, "机器号");
		TRoundRect::sDrawBySize(xMask, 	Lv.Grid.Top[2] + 76, 76, 30, 7, clNearWhite, "屏蔽", 14, clNearBlack);
		TRoundRect::sDrawBySize(xUnMask, 	Lv.Grid.Top[2] + 76, 124, 30, 7, clNearWhite, "取消屏蔽", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Local Network->Blocking Net Ctrl Unit");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Local:");
		if(MainCtl.fData.Split.NetworkMode == 1){
			TextRender_string24(Lv.Grid.Left[0] + 126, Lv.Grid.Top[0] +6, clNearWhite, " Central  ");
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			TextRender_string24(Lv.Grid.Left[0] + 126, Lv.Grid.Top[0] +6, clNearWhite, " Regional ");
			TextRender_string24(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] +6, clNearWhite, " CentralID");
			TextDigitRender2_24(Lv.Grid.Left[0] + 516, Lv.Grid.Top[0] +6, clNearWhite, ProtoC2C.MasterNum);
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 126, Lv.Grid.Top[0] +6, clNearWhite, " Stand Alone");
		}
			TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +40, clNearWhite, "On Network");
		
		TextRender_string24(Lv.Grid.Left[0] + 176, Lv.Grid.Top[0] +40, clNearWhite, "Pcs,");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "Blocking ID Range");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +40, clNearWhite, "CtrlID");
		TRoundRect::sDrawBySize(xMask, 	Lv.Grid.Top[2] + 76, 76, 30, 7, clNearWhite, "Mask", 14, clNearBlack);
		TRoundRect::sDrawBySize(xUnMask, 	Lv.Grid.Top[2] + 76, 124, 30, 7, clNearWhite, " Clear  ", 14, clNearBlack);
	}
}

void TCanNetworkMask::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawFixText();
	TextDigitRender2_24(Lv.Grid.Left[0] + 100, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
	TextDigitRender2_24(Lv.Grid.Left[0] + 146, Lv.Grid.Top[0] +40, clNearWhite, ProtoC2C.SlaveCount +1);
	DrawHadMask();
	TextRender_string24(Lv.Grid.Left[2] + 124, Lv.Grid.Top[2] +40, clNearWhite, "-");
	IList->EditCtlNum.Init(Lv.Grid.Left[2] + 84, Lv.Grid.Top[2] +36, 36, 32, 0,0, 1, clNearWhite);
	IList->EditCtlNum.SetMaxLen(2);
	IList->EditCtlNum.TextClear();
	IList->EditCtlNum.SetPstTail();
	IList->EditAddrNum.Init(Lv.Grid.Left[2] + 136, Lv.Grid.Top[2] +36, 36, 32, 0,0, 1, clNearWhite);
	IList->EditAddrNum.SetMaxLen(2);
	IList->EditAddrNum.TextClear();
	IList->EditAddrNum.SetPstTail();
	IList->EditCtlNum.Show();
	IList->EditAddrNum.Show();
}

void TCanNetworkMask::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	Check4Selected();
}

void TCanNetworkMask::FullRedraw(int Prm)
{	
	if(Prm == 0){
		IsShowing = 1;
		RenderBackGround();
		DrawSelf();

	}else	
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		DrawFixText();
		TextDigitRender2_24(Lv.Grid.Left[0] + 100, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
		TextDigitRender2_24(Lv.Grid.Left[0] + 146, Lv.Grid.Top[0] +40, clNearWhite, ProtoC2C.SlaveCount +1);
		TextRender_string24(Lv.Grid.Left[2] + 124, Lv.Grid.Top[2] +40, clNearWhite, "-");
		IList->EditCtlNum.Show();
		IList->EditAddrNum.Show();
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			Check4Selected();
		}
	}
}

//_____________________________________________________________________________________________________________________________
void TCanNetworkMask::DrawHadMask()
{
	if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[0] + 240, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 	"已屏蔽号段:");
	else TextRender_string24(Lv.Grid.Left[0] + 240, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 			"Blocking ID");
	if( (ProtoC2C.CtlMask.StartNum) && (ProtoC2C.CtlMask.EndNum) ){
		TextDigitRender2_24(Lv.Grid.Left[0] + 380, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, ProtoC2C.CtlMask.StartNum);
		TextRender_string24(Lv.Grid.Left[0] + 408, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, "-");
		TextDigitRender2_24(Lv.Grid.Left[0] + 426, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, ProtoC2C.CtlMask.EndNum);
	}else{
		TextRender_string24(Lv.Grid.Left[0] + 380, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, "00");
		TextRender_string24(Lv.Grid.Left[0] + 408, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, "-");
		TextRender_string24(Lv.Grid.Left[0] + 426, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, "00");
	}
}

void TCanNetworkMask::PermissionCmdReturnDraw()
{
	RectFillRender(0, Lv.Grid.Bottom[2] +2, 799, 440, ContentColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
}

void TCanNetworkMask::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Do Mask
		ProtoC2C.WriteMaskCtlNum(StartNum, EndNum);
		ProtoC2C.ReadMaskCtlNum();
		DrawHadMask();
	}else
	if(PermissionCmd == 2){
		//Do UnMask
		ProtoC2C.WriteMaskCtlNum(0, 0);
		ProtoC2C.ReadMaskCtlNum();
		DrawHadMask();
	}
}

void TCanNetworkMask::sRecoverSetDefault()
{
	ProtoC2C.WriteMaskCtlNum(0, 0);
	ProtoC2C.ReadMaskCtlNum();
}


void TCanNetworkMask::ClearSelected()
{
	char *tText;
	switch(CtlIndex){
		case 0:
			IList->EditCtlNum.Selected =0;
			IList->EditCtlNum.Show();
			break;
		case 1:
			IList->EditAddrNum.Selected =0;
			IList->EditAddrNum.Show();
			break;
		case 2:
			if(LanguageEnCn==0)tText="屏蔽"; else tText="Mask";
			TRoundRect::sDrawBySize(xMask, 	Lv.Grid.Top[2] + 76, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 3:	
			if(LanguageEnCn==0)tText="取消屏蔽"; else tText=" Clear  ";	
			TRoundRect::sDrawBySize(xUnMask, 	Lv.Grid.Top[2] + 76, 124, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
	}
}

void TCanNetworkMask::Check4Selected()
{
	char *tText;
	switch(CtlIndex){
		case 0:
			IList->EditCtlNum.Selected =1;
			IList->EditCtlNum.SetPstTail();
			IList->EditCtlNum.Show();
			break;
		case 1:
			IList->EditAddrNum.Selected =1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			break;
		case 2:
			if(LanguageEnCn==0)tText="屏蔽"; else tText="Mask";
			TRoundRect::sDrawBySize(xMask, 	Lv.Grid.Top[2] + 76, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 3:
			if(LanguageEnCn==0)tText="取消屏蔽"; else tText=" Clear  ";			
			TRoundRect::sDrawBySize(xUnMask, 	Lv.Grid.Top[2] + 76, 124, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
	}
}

int TCanNetworkMask::CheckStart2EndNumValid()
{
	if( (StartNum) && (EndNum) ){
		if( (StartNum <=dNETWORK_MACHINE_COUNT) && (EndNum <=dNETWORK_MACHINE_COUNT) && (StartNum <= EndNum) )
			Start2EndNumValid = 1;
		else
			Start2EndNumValid = 0;
	}else
	if( StartNum || EndNum){
		Start2EndNumValid = 0;
	}else{
		Start2EndNumValid = 1;
	}
	
	if(Start2EndNumValid){
		IList->EditCtlNum.SelectedColor = clBlue;
		IList->EditAddrNum.SelectedColor = clBlue;
		IList->EditCtlNum.Color = clNearWhite;
		IList->EditAddrNum.Color = clNearWhite;
	}else{
		IList->EditCtlNum.SelectedColor = clRed;
		IList->EditAddrNum.SelectedColor = clRed;
		IList->EditCtlNum.Color = clRed;
		IList->EditAddrNum.Color = clRed;
	}
	
	return Start2EndNumValid;
}


TGuiMsgReturn TCanNetworkMask::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned int aAddr, HadPrc;
	TDMix aFullPath;

	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}else
	if(IsOnGoing){
		switch(aKey){
			case VK_RETURN:
				IsShowing = 0;
				IsOnGoing = 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_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				HadPrc =0;
				if( (aKey == VK_LEFT) || (aKey == VK_RIGHT) ){
					if(CtlIndex == 0){
						if(aKey == VK_RIGHT){
							if(IList->EditCtlNum.Pst >= IList->EditCtlNum.Str.GetEndPst()){
								ClearSelected();
								CtlIndex++;
								Check4Selected();
								HadPrc =1;
							}
						}
					}else
					if(CtlIndex == 1){
						if(aKey == VK_LEFT){
							if(IList->EditAddrNum.Pst < 0){
								ClearSelected();
								CtlIndex--;
								Check4Selected();
								HadPrc =1;
							}
						}
					}else
					if(CtlIndex == 2){
						if(aKey == VK_RIGHT){
							ClearSelected();
							CtlIndex++;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 3){
						if(aKey == VK_LEFT){
							ClearSelected();
							CtlIndex--;
							Check4Selected();
							HadPrc =1;
						}
					}
				}
				if(HadPrc == 0){
					if(CtlIndex == 0){
						IList->EditCtlNum.KeyIn(aKey);
						int iVal;
						iVal = IList->EditCtlNum.Str.ToInteger();
						if( (iVal <0) || (iVal > dNETWORK_MACHINE_COUNT) ) StartNum = 99;
						else StartNum = iVal;
						CheckStart2EndNumValid();
						IList->EditCtlNum.Show();
						IList->EditAddrNum.Show();
					}else
					if(CtlIndex == 1){
						IList->EditAddrNum.KeyIn(aKey);
						int iVal;
						iVal = IList->EditAddrNum.Str.ToInteger();
						if( (iVal <0) || (iVal > dNETWORK_MACHINE_COUNT) ) EndNum = 99;
						else EndNum = iVal;
						CheckStart2EndNumValid();
						IList->EditCtlNum.Show();
						IList->EditAddrNum.Show();
					}
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					ClearSelected();
					CtlIndex--;
					Check4Selected();
				}else{
					if(Start2EndNumValid){
						ClearSelected();
						CtlIndex=3;
						Check4Selected();
					}
				}
				break;
			case VK_DOWN:
				if(CtlIndex <3){
					if(CtlIndex == 1){
						if(Start2EndNumValid){
							ClearSelected();
							CtlIndex++;
							Check4Selected();
						}
					}else{
						ClearSelected();
						CtlIndex++;
						Check4Selected();
					}
				}else{
					ClearSelected();
					CtlIndex=0;
					Check4Selected();
				}
				break;
			case VK_TAB:

				break;
			case VK_EXECUTE:
				if(CtlIndex == 2){
					//Do Mask 
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 3){
					//Do UnMask
					PermissionCmd = 2;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}


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

