#include "Gui.h"

class TImageList ImageList;
class TItemList	ItemList;

class TMenu MainMenu;

//Screen Top Panel ____________________________________________________________________________________
class TPanel 		PanelOnTop;
class TStaticText 	STextCaption; 
class TIcon			IconTop;
class TStaticText 	STextNewMsg; 

//Screen Bottom Panel __________________________________________________________________________________
class TPanel 		PanelOnBottom;
class TStaticText 	STextAdjust;
class TStaticText 	STextAllowAuto; 
class TStaticText 	STextMaintain;
class TStaticText 	STextTimeShow;

class TIcon		IconAdjust;
class TIcon		IconAllowAuto;
class TIcon		IconMaintain;
//______________________________________________________________________________________________________

class TCtlPanel1 CommonPanel;
class TCtlPanelLite	CtlPanelLite;

//______________________________________________________________________________________________________

class TListBoxForm 	ListBoxForm	 ;
class TListViewDevice 	ListViewDevice;
class TDevProperty	DevProperty;

class TLiteIconOverView		LiteIconOverViewDev;
class TLiteListViewMask		LiteListViewMask;
class TRegister			Register;
class TRegisterManual		RegisterManual;
class TDesignPrmSet		DesignPrmSet;
//class TRegisterAdd		RegisterAdd;
//class TRegisterAddRemove	RegisterAddRemove;
//class TIoPadDefine		IoPadDefine;
class TQueryMsg			QueryMsg;
class TQueryMessageFire		QueryMessageFire;
class TQueryMsgLinkage		QueryMsgLinkage;
class TQueryMsgSv			QueryMsgSv;
class TQueryMsgFault		QueryMsgFault;
class TQueryMsgOperate		QueryMsgOperate;
class TDevMask			DevMask;
class TXBox				aRadioGroup;
class TXBox				aRadioGroup2;
class TPrinterSet			PrinterSet;
//class TUserSet			UserSet;

class TEventShow			EventShow;
class TPageIdle			PageIdle;
class TPermissionSet		PermissionSet;
class TPrintInf			PrintInf;
class TDateTimeCfg		DateTimeCfg;
//class TECanNetwork		ECanNetwork;
//class TQueryCfg			QueryCfg;
class TPasswdChange		PasswdChange;
class TCircuitState		CircuitState;
class TRootDevVoltage		RootDevVoltage;
class TMessageBox			MessageBox;
//class TDetectorCurve		DetectorCurve;
class TCircuitCfg			CircuitCfg;
//class TEpMonitor			EpMonitor;
class TDuplicateCheck		DuplicateCheck;
//class TAddrAssign			AddrAssign;
class TLiteSelfCheck		LiteSelfCheck;
class TProjectSet			ProjectSet;
class TBusPadLnk			BusPadLnk;
class TLinkageProgram		LinkageProgram;
class TDirectPadCfg		DirectPadCfg;
//class TDisplayPadCfg		DisplayPadCfg;
class TfNetworkReg		fNetworkReg;
//class TfNetworkRegManual	fNetworkRegManual;
//class TfNetworkRegRemove	fNetworkRegRemove;
class TuInportExport		uInportExport;
class TOnlineCtrl			OnlineCtrl;
class TGraphDevCfg		GraphDevCfg;
class THwInf			HwInf;
class TFwUpgrade			FwUpgrade;
class TDevAuthorize		DevAuthorize;
class TAuthorizeState		AuthorizeState;
class TaBefore			aBefore;
class TMaintain			Maintain;
class TAllow			Allow;
class TBroadcastCfg		BroadcastCfg;
class TScreenSaveCfg		ScreenSaveCfg;
class TCleanUp			CleanUp;
class TAboutUs			AboutUs;
class TCheckNormal		CheckNormal;
class TDescpWrite			DescpWrite;
class TPmsIntervene		PmsIntervene;
class TPmsIntervene		PmsIntervene4Gui;
class TDevStartStop		DevStartStop;
//class  TPowerSuplyCfg		PowerSuplyCfg;
class  TPowerSuplyInf		PowerSuplyInf;
class  TDeviceSetup		DeviceSetup;
class TLanguageCfg		LanguageCfg;

class TRunModeCfg			RunModeCfg;
class TPadInf			PadInf;
class TLinkageList		LinkageList;
class TLnkFecBusCfg		LnkFecBusCfg;
class TAreaCfg			AreaCfg;
class TUserCodeSet		UserCodeSet;
class TUserCodeSetByArea	UserCodeSetByArea;
class TCanNetworkInf		CanNetworkInf;
class TCanNetworkRegister	CanNetworkRegister;
class TCanNetworkMask		CanNetworkMask;
class TBlueTooth			BlueTooth;
class TWifiConn			WifiConn;
class TTransmiteDev		TransmiteDev;
class TClearRegistedInf		ClearRegistedInf;
class TClearLinkageCfg		ClearLinkageCfg;
class TClearDescp			ClearDescp;
class TRecoverAll			RecoverAll;
//class TSlaveRegByRemote		SlaveRegByRemote;
class TDevCount			DevCount;
class TTipPopup			TipPopup;
class TDevOutputCfg		DevOutputCfg;
class TDevSensitiveCfg		DevSensitiveCfg;
class TDevVaCfg 			DevVaCfg;
class TDisplayPadDescpDownload	 DisplayPadDescpDownload;

class TDialogStartup		DialogStartup;

class TCnInput			CnInput;


class TEdit Edit1;
class TButton Button1;

class THistoryMsg HistoryMsg;

class TImage	ImageLogo24;
class TImage	ImageLogo32;

int IsSelfChecking =0;

static char TextPwrBat[36];

const unsigned int ContentColor = clNearBlack;
const unsigned int CaptionColor = clDarkBlue;
const unsigned int ItemColor 	= clDarkkGray;
const unsigned int PanelColor = clGray;
const unsigned int SmallPanelColor = clDarkkGray;
const TContentGeometry ContentGeometry = {
	.Left = 0,
	.Top = 36,
	.Width = 800,
	.Heigh = 408,
	.Right = 800 - 0 -1,
	.Bottom = 408 + 36 -1,
};

void StateBarReflash(void)
{
	if(RunState == TRunModeCfg::RunModeAdjust){
		if(LanguageEnCn==0)STextAdjust.SetText("调试",24);
		else STextAdjust.SetText("TEST",24);
		//STextAdjust.Color = clMedOrange;
		STextAdjust.TextColor = clRed;
	}else
	if(RunState == TRunModeCfg::RunModeRun){
		if(LanguageEnCn==0)STextAdjust.SetText("运行",24);
		else STextAdjust.SetText("RUN ",24);
		//STextAdjust.Color = clFrmFace;
		STextAdjust.TextColor = clNearBlack;
	}else{
		RunState = TRunModeCfg::RunModeRun;
		if(LanguageEnCn==0)STextAdjust.SetText("运行",24);
		else STextAdjust.SetText("RUN ",24);
		STextAdjust.TextColor = clNearBlack;
		//STextAdjust.SetText(" ?? ",24);
		//STextAdjust.Color = clFrmFace;
		//STextAdjust.TextColor = clMedOrange;
	}
	IconAdjust.Show();
	STextAdjust.Show();
	
	if(AllowAuto){
		if(LanguageEnCn==0)STextAllowAuto.SetText("自动",24);
		else STextAllowAuto.SetText("AUTO",24);
		STextAllowAuto.TextColor = clNearBlack;
		IconAllowAuto.SetActive();
	}else{
		if(LanguageEnCn==0)STextAllowAuto.SetText("自动",24);
		else STextAllowAuto.SetText("AUTO",24);
		STextAllowAuto.TextColor = clWhiteGray;
		IconAllowAuto.SetDeActive();
	}
	STextAllowAuto.Show();
	IconAllowAuto.Show();
	
	StateBarReflashMaintain();
}

void StateBarReflashMaintain(void)
{
	if(IsMaintainTip){
		if(LanguageEnCn==0)STextMaintain.SetText("维保",24);
		else STextMaintain.SetText("Maint",24);
		STextMaintain.TextColor = clRed;
	}else{
		STextMaintain.SetText("     ",24);
		STextMaintain.TextColor = clNearBlack;
	}
	STextMaintain.Show();
	LastMaintainTip = IsMaintainTip;
}

void StateBarUpDateBatIfForce(int IsForce)
{
	int x, dInt,dDec;
	
	if( (LiteSelfCheck.IsShowing == 0) || (IsForce) ){
		x = 284;
		if(PsBatDataAva){
			if(LanguageEnCn==0)TMyString::sFromStr("备电:", TextPwrBat);
			else TMyString::sFromStr("Batt:", TextPwrBat);
			dInt = PsBatData.Body.BatVoltage1.D16 / 1000;
			dDec = PsBatData.Body.BatVoltage1.D16 / 100 % 10;
			TMyString::sAddOn2Dg(dInt, TextPwrBat);
			TMyString::sAddOnChar('.', TextPwrBat);
			TMyString::sAddOn1Dg(dDec, TextPwrBat);
			TMyString::sAddOnStr("V " , TextPwrBat);
			
			dInt = PsBatData.Body.BatVoltage2.D16 / 1000;
			dDec = PsBatData.Body.BatVoltage2.D16 / 100 % 10;
			TMyString::sAddOn2Dg(dInt, TextPwrBat);
			TMyString::sAddOnChar('.', TextPwrBat);
			TMyString::sAddOn1Dg(dDec, TextPwrBat);
			TMyString::sAddOnStr("V " , TextPwrBat);
			
			TMyString::sAddOn3Dg(PsBatData.Body.BatEnerger1, TextPwrBat);
			TMyString::sAddOnStr("% ", TextPwrBat);

			TextRender_string24(x,450,clNearBlack, clFrmFace, TextPwrBat );
		}else{
			if(LanguageEnCn==0)	TMyString::sFromStr("备电: 无数据          ", TextPwrBat);
			else 				TMyString::sFromStr("Batt: No Data         ", TextPwrBat);
			TextRender_string24(x,450,clNearBlack, clFrmFace, TextPwrBat );
		}
	}
}

void ShowSelfCheckCtlNum()
{
	unsigned int i, cnt, x;
	char Text[8];
	x = 284;
	if(LanguageEnCn==0)	TMyString::sFromStr("自检区域机 ", TextPwrBat);
	else 				TMyString::sFromStr("Self-Check ", TextPwrBat);
	cnt = 0;
	for(i=0; i<17; i++){
		if(NwSelfCheckTick[i] ){
			if(cnt <2){
				cnt++;
				Text[0] = (i / 10) + '0';
				Text[1] = (i % 10) + '0';
				Text[2] = ' ';
				Text[3] = '\0';
				TMyString::sAddOnStr(Text, TextPwrBat);
			}else{
				TMyString::sAddOnStr("...", TextPwrBat);
				break;
			}
		}
	}
	TMyString::sFillFixLen(TextPwrBat,22);
	TextRender_string24(x,450,clNearBlack, clFrmFace, TextPwrBat );
}

void StateBar_Pwr_SelfCheck()
{
	static unsigned int ShowWhat =0;
	static unsigned int itv =0;
	if(MainCtl.fData.Split.NetworkMode == 1){
		if(NwIsSelfCheck){
			if(itv>1){
				if(ShowWhat == 0){
					ShowWhat = 1;
					ShowSelfCheckCtlNum();
				}else{
					ShowWhat = 0;
					StateBarUpDateBatIfForce(0);
				}
				itv =0;
			}
		}else{
			if(itv>1){
				itv = 0;
				if(ShowWhat){
					ShowWhat = 0;
					StateBarUpDateBatIfForce(1);
				}else{
					if(PsBatDataHasUpdate){
						PsBatDataHasUpdate = 0;
						StateBarUpDateBatIfForce(0);
					}
				}
			}
		}
	}else{
		ShowWhat = 0;	
		if(itv>1){
			itv = 0;
			if(PsBatDataHasUpdate){
				PsBatDataHasUpdate = 0;
				StateBarUpDateBatIfForce(0);
			}
		}
	}
	itv++;
}

void StateBarUpDateBat(int Ava)
{
	PsBatDataHasUpdate = 1;
	PsBatDataAva = Ava;
	//StateBarUpDateBatIfForce(0);
}

void StateBarShow(void)
{
	PanelOnBottom.Show();
	StateBarReflash();
}

void GuiTopPanel_init(void)
{
	PanelOnTop.Init		(  0,   0, 800, 36,	0,   0, clFrmFace, bvRaised);
	IconTop.Init		(  650,   1,  32, 32, 	0,   0, 24, clFrmFace);
	STextCaption.Init		(  682,  2, 120, 32, 	0,   0, clNearBlack, clFrmFace);
	STextNewMsg.Init		(  300, 1, 499, 33, 	0,   0, clNearBlack, clFrmFace);
	
	IconTop.IconList = &ImageList;
	IconTop.iSize = 24;
	IconTop.ActiveImageIndex = 16;
	IconTop.DeActiveImageIndex = 16;
	IconTop.SetActive();

	ImageLogo24.Init(705, 6, FLASH_USER_PIC_LOGO24,90,24);
	ImageLogo32.Init(675, 2, FLASH_USER_PIC_LOGO32,122,32);
}

void GuiBottomPanel_init(void)
{
	//Screen Bottom Panel __________________________________________________________
	PanelOnBottom.Init		(  0,  443, 800,  37,	0,   0, clFrmFace, bvRaised);
	IconAdjust.Init			(  1,  6,  	 28,  28, 	0, 443, 24, clFrmFace);
		STextAdjust.Init		( 25,  3,    56,  33, 	0, 443, clNearBlack, clFrmFace);
	
	IconAllowAuto.Init		( 84,  6,    28,  28, 	0, 443, 24, clFrmFace);
		STextAllowAuto.Init	(112,  3,    56,  31, 	0, 443, clNearBlack, clFrmFace);
	
	//IconAllowManual.Init		(168,  6,    28,  28, 	0, 443, 24, clFrmFace);
	STextMaintain.Init	(196,  3,    56,  31, 	0, 443, clNearBlack, clFrmFace);
	
	STextTimeShow.Init		(564,  3,    235,  31, 	0, 443, clNearBlack, clFrmFace);
	
	IconAdjust.IconList = &ImageList;
	IconAdjust.iSize = 24;
	IconAdjust.ActiveImageIndex = 3;
	IconAdjust.DeActiveImageIndex = 11;
	IconAdjust.SetActive();
	
	IconAllowAuto.IconList = &ImageList;
	IconAllowAuto.iSize = 24;
	IconAllowAuto.ActiveImageIndex = dIcon24_Allow;
	IconAllowAuto.DeActiveImageIndex = dIcon24_NotAllow;
	IconAllowAuto.SetActive();
	
	//IconAllowManual.IconList = &ImageList;
	//IconAllowManual.iSize = 24;
	//IconAllowManual.ActiveImageIndex = dIcon24_Allow;
	//IconAllowManual.DeActiveImageIndex = dIcon24_NotAllow;
	//IconAllowManual.SetActive();
	//______________________________________________________________________________
	
}

void GuiStart(void)
{
	//Init____________________________________________________________________________________________________________________________
	//DescpBuf.Init();
	
	//Screen Top Panel _____________________________________________________________
	GuiTopPanel_init();
	//Screen Bottom Panel _____________________________________________________________
	GuiBottomPanel_init();
	
	//Menu__________________________________________________________________________
	MainMenu.Init(0,36,799,442);	
	MainMenu.IconList = &ImageList;
	MainMenu.LoadPermission(3);
	
	
	ListViewDevice.Init() ;
	
	LiteIconOverViewDev.Init(0, 36, 800, 406, 0, 0, clNearWhite);
	
	//IoPadDefine.Init(0, 36, 800, 406, 0, 0, clNearWhite);

	PageIdle.Init(0, 36, 800, 406, 0, 0, clMedGray);
	QueryMsg.Init(0, 36, 800, 406, 0, 0, clMoneyGreen);

	//_____________________________________________________________________________
	ListBoxForm.Init(0, 36, 800, 406, 	0, 0, 1, clMoneyGreen) ;
	
	EventShow.Init(0, 36, 800, 406, 	0, 0, 1, clMoneyGreen) ;
	EventShow.TryGetMaskData(2,2024);

	Edit1.Init(6,6, 120,30, 0,0,1,clMoneyGreen);
	Button1.Init(6,200, 120,30,0,0, 2,clMoneyGreen);
	//________________________________________________________________________________________________________________________________
	//Screen Top
	PanelOnTop.Show();
	ImageLogo32.Render(800,480);
	
	PanelOnBottom.Show();
	
	StateBarReflash();
	IconAdjust.SetDeActive();
	IconAdjust.Show();
	IconAllowAuto.Show();
	//IconAllowManual.Show();
	
	STextAdjust.Show();
	STextAllowAuto.Show();
	STextMaintain.Show();
	
	TipPopup.Init();
	TipPopup.ClearTickOnKeyPress();
	
	DisplayPadDescpDownload.Init();
}

void EventShowOnShowCallBack()
{
	CheckNormal.Hide();
}

void GuiRedrawBase()
{
	//________________________________________________________________________________________________________________________________
	//Screen Top
	PanelOnTop.Show();
	ImageLogo32.Render(800,480);
	
	PanelOnBottom.Show();
	StateBarReflash();
	StateBarUpDateBatIfForce(1);
}

void GuiRedraw(int Prm)
{
	if(TipPopup.IsShow){
		TipPopup.Hide();
		TipPopup.ClearTickOnKeyPress();
		EventShow.MakeFastCheck();
		EventShow.MakeShow();
		if( EventShow.IsShowing ){
			GuiState = guiShowEvent;
			PageIdle.Hide();
			EventShow.MakeShow();
		}else{
			GuiState = guiIdle;
			EventShow.Hide();
			PageIdle.Show();
		}
	}else
	if(CheckNormal.IsShowing){
		CheckNormal.ReDraw();
	}else
	if(EventShow.IsShowing){
		GuiState = guiShowEvent;
		EventShow.MakeShow();
	}else
	if(MainMenu.OnAction){
		if(MainMenu.ActiveLevel < 2){
			MainMenu.FullRedraw();
		}else{
			MainMenu.FullRedrawFrm(Prm);
		}
	}else
	if(MainMenu.Active){
		MainMenu.FullRedraw();
	}else
	if(PageIdle.IsShowing){
		PageIdle.Show();
	}
}

int CheckIsRemoteRegFeasible()
{
	if(Register.IsOnGo)return 0;
	if(ClearRegistedInf.Eraser.OnGo)return 0;
	if(ClearLinkageCfg.Eraser.OnGo)return 0;
	if(ClearDescp.Eraser.OnGo)return 0;
	if(ProtoC2C.RemoteReq.Reg.IsRequsting)return -1;
	return 1;
}

void SlaveDoRemoteReg()
{
	SlaveInRemoteCtl = 0xB6B6B6B6;
	GeneralDev.LcdBlackLightSetOn();
	Register.Init();
	Register.Show4Slave();
	Register.Nw.CtlNum = MainCtl.fData.Split.MyNum;
	//Trige to Start Register
	Register.SlaveReplyStartState(1);
	Register.SlaveRegInit(ProtoC2C.RemoteReq.Reg.RegPath);
}

