#include "Allow.h"
#include "Runtime.h"
#include "server.h"

void TAllow::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	CtlIndex = 1;
	IsShow = 0;
	IsPermission =0;
	Sta = 0;
	
	CtlNumSta = IsSelf;
}

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

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

void TAllow::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, "机器号");
		TextRender_string24(Lv.Grid.Left[0] + 90, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, "本机");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "控制状态");
		TextRender_string24(Lv.Grid.Left[1] + 104, Lv.Grid.Top[1] +40, clSilver, "按左右键更改值,按确定键执行");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Linkage->Auto ");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		TextRender_string24(Lv.Grid.Left[0] + 90, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, "Local");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Set Auto");
		TextRender_string24(Lv.Grid.Left[1] + 104, Lv.Grid.Top[1] +40, clSilver, "Press Key Left Or Key Right To Change,Key OK To Apply");	
	}
}

void TAllow::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);
	IList->Edit[0].Init(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +36,60,30,0,0,1,clNearWhite);
	IList->Edit[1].Init(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +36,90,30,0,0,1,clNearWhite);
	for(int i=0; i<2;i++){
		IList->Edit[i].Enable = 1;
		IList->Edit[i].Visible = 1;
		IList->Edit[i].SetMaxLen(2);
		IList->Edit[i].SetInputMaskEn();
		IList->Edit[i].Str.FromStr("01");
		IList->Edit[i].SetPstTail();
	}
	if(MainCtl.fData.Split.NetworkMode == 1){
		IList->Edit[0].Enable = 1;
	}else{
		IList->Edit[0].Enable = 0;
	}
	DrawFixText();
	IList->Edit[1].IsShowCursor = 0;
	LoadData();
	ShowAuto();
}

void TAllow::Show(void)
{	
	IsShow = 1;
	RenderBackGround();
	DrawSelf();
}

void TAllow::FullRedraw(int Prm)
{
	if(Prm == 0){
		Show();
		DrawSelf();
		DrawCtlNum();
		DrawCtlNumTip();
		ShowAuto();
		ShowSta();
	}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();
		IList->Edit[0].Show();
		IList->Edit[1].Show();
		DrawCtlNum();
		DrawCtlNumTip();
		ShowAuto();
		Check4Selected();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

//______________________________________________________________________________________________________________________________
void TAllow::ClearSelected(void)
{
	switch(CtlIndex){
		case 0:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 1:
			IList->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			break;
		default:

			break;
	}	
}

void TAllow::Check4Selected(void)
{
	switch(CtlIndex){
		case 0:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			break;
		case 1:
			IList->Edit[1].Selected = 1;
			IList->Edit[1].Show();
			break;
		default:

			break;
	}	
}

void TAllow::ShowAuto()
{
	if(LanguageEnCn==0){
		if(aAuto == 0){
			IList->Edit[1].Str.FromStr("手动");
		}else{
			IList->Edit[1].Str.FromStr("自动");
		}
	}else{
		if(aAuto == 0){
			IList->Edit[1].Str.FromStr("Manual");
		}else{
			IList->Edit[1].Str.FromStr("Auto");
		}
	}
	IList->Edit[1].Show();
}

void TAllow::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		AllowAuto = aAuto;
		AllowManual = aManual;
		ProcessUserSetManualState();
		Record.OperateAdd_AllowSet(MainCtl.fData.Split.MyNum, AllowAuto + AllowManual);
		LoadData();
		ExRecord.SetStateCodeAutoManual();
		FecBusCRT.SetStateCodeAutoManual();
		DrawCtlNum();
		ShowAuto();
		if( (AllowAuto == aAuto) && (AllowManual == aManual) ){
			ShowSta(1);
		}else{
			ShowSta(-1);
		}
	}else
	if(PermissionCmd == 2){
		ProtoC2C.TxMsg.Data.CarryByte = 255;
		ProtoC2C.TxMsg.Data.Priority =1;
		ProtoC2C.TxMsg.Data.Routing =0;
		ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd_SetSlaveAllow;
		ProtoC2C.TxMsg.Data.Len = 4;
		
		ProtoC2C.TxMsg.Data.Data[0] = 0;
		ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
		ProtoC2C.TxMsg.Data.Data[2] = CtlNum;
		ProtoC2C.TxMsg.Data.Data[3] = aAuto;
		
		ProtoC2C.oBufInsert();
		
		ShowSta(2);
	}
}

void TAllow::PermissionCmdReturnDraw()
{
	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);

	DrawFixText();
	
	IList->Edit[0].Show();
	IList->Edit[1].Show();
	Check4Selected();
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TAllow::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone, PmsRtnMsg;
	int iVal;
	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{
		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){
					IList->Edit[0].KeyIn(aKey);
					DrawCtlNumTip();
					IList->Edit[0].Show();
				}else
				if(CtlIndex == 1){
					if(aAuto){
						aAuto =0;
						aManual =2;
					}else{
						aAuto =1;
						aManual =0;
					}
					ShowAuto();
				}
				break;
			case VK_UP:
				if(MainCtl.fData.Split.NetworkMode == 1){
					if(CtlIndex >0){
						ClearSelected();
						CtlIndex --;
						Check4Selected();
					}
				}
				break;
			case VK_DOWN:
				if(CtlIndex <1){
					ClearSelected();
					CtlIndex++;
					Check4Selected();
				}
				break;	
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP
				if(CtlIndex == 1){
					if(aAuto){
						aAuto =0;
						aManual =2;
					}else{
						aAuto =1;
						aManual =0;
					}
					ShowAuto();
				}
				break;			
			case VK_EXECUTE:
				//Save Data Here
				if( (MainCtl.fData.Split.NetworkMode == 1) && (CtlNumSta != IsSelf) ){
					if(CtlNumSta == IsSlave){
						PermissionCmd = 2;
						if(SystemPermission > 1){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							ClearSelected();
							PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[2] + 200, Lv.Grid.Top[2] + 16, 400, 140, 2);
						}
					}
				}else{
					PermissionCmd = 1;
					if(SystemPermission > 1){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[2] + 200, Lv.Grid.Top[2] + 16, 400, 140, 2);
					}	
				}				
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
		if(VK_EXECUTE != aKey){
			ShowSta(0);
		}
	}
	
	return aMsg;	
}

void TAllow::InitPanel(void)
{
	LoadData();
	Check4Selected();
	DrawCtlNum();
	ShowAuto();
}

void TAllow::LoadData()
{
	aAuto = AllowAuto;
	aManual = AllowManual;
}

void TAllow::DrawCtlNum()
{
	IList->Edit[0].Str.FromUInt2Dg(MainCtl.fData.Split.MyNum);
	IList->Edit[0].Show();
}

void TAllow::DrawCtlNumTip()
{
	CtlNum = IList->Edit[0].Str.ToInteger();
	if( (CtlNum <1) || (CtlNum >dNETWORK_MACHINE_COUNT) ){
		CtlNumSta = IsNull;
		IList->Edit[0].SelectedColor = clRed; 
		if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 				"无效机器号     ");
		else TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 						"Ctrl ID Invalid");
	}else{
		IList->Edit[0].SelectedColor = clBlue;
		if(CtlNum == MainCtl.fData.Split.MyNum){
			CtlNumSta = IsSelf;
			if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[0] + 90, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 			"本机           ");
			else TextRender_string24(Lv.Grid.Left[0] + 90, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 					"Local          ");
		}else{
			if( (CtlNum <ProtoC2C.CtlMask.StartNum) || (CtlNum >ProtoC2C.CtlMask.EndNum) ){
				unsigned int IsInlist = 0;
				for(int i=0; i<ProtoC2C.SlaveCount; i++){
					if(CtlNum == ProtoC2C.SlaveList[i])IsInlist =1;
				}
				if(IsInlist){
					CtlNumSta = IsSlave;
					if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 	"已登记区域机   ");
					else TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 			"Registered Ctrl");
				}else{
					CtlNumSta = IsUnknown;
					if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 	"未登记区域机   ");
					else TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 			"Unregister Ctrl");
				}
			}else{
				CtlNumSta = IsMask;
				if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 		"已屏蔽的区域机 ");
				else TextRender_string24(Lv.Grid.Left[0] + 80, Lv.Grid.Top[0] +40, clNearWhite, ItemColor, 				"Ctrl Blocking  ");
			}
		}
	}
}

void TAllow::ShowSta()
{
	if(LanguageEnCn==0){
		if(Sta == 0){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "              ");
		}else
		if(Sta == 1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存成功      ");
		}else
		if(Sta == -1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存失败      ");
		}else
		if(Sta == 2){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "设置信息已发送");
		}
	}else{
		if(Sta == 0){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "              ");
		}else
		if(Sta == 1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Setting OK    ");
		}else
		if(Sta == -1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Failed To Set ");
		}else
		if(Sta == 2){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Transmited    ");
		}
	}
}

void TAllow::ExtChange(int ExtAuto)
{
	return;
	//if(this->IsShow){
	//	aAuto=ExtAuto;
	//	if(IsSelfChecking ==0 )
	//		ShowAuto();
	//}
}

void TAllow::ShowSta(int aSta)
{
	Sta = aSta;
	ShowSta();
}

void TAllow::sRecoverSetDefault()
{
	AllowAuto = 0;
	ProcessUserSetManualState();
}


