#ifndef REGISTER_H_
#define REGISTER_H_


#include "gType.h" 
#include "GraphBase.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "my_string.h"
#include "Device.h"
#include "EpDevice.h"
#include "StaticText.h"
#include "EditML.h"
#include "CtlPanel1.h"

#include "Record.h"

#define nrCtl		0
#define nrShowDetails	1 

class TRegister{
	public:
		static constexpr unsigned short hList[12] = {72,108,154,28,	26,72,34,34,	34,34,34,34};
		static constexpr unsigned short wList[12] = {800,290,290,290,	290,290,290,290,	290,290,290,290};
		static constexpr TLv Lv = {
			.Caption.Left = 0,
			.Caption.Top = 36,
			.Caption.Width = 800,
			.Caption.Heigh = 32,
			.Caption.Right = 800 -0 -1,
			.Caption.Bottom = 36 + 32,
			
			.Grid.Left = {0,0,0,0,	0,0,0,0,	0,0,0,0},
			.Grid.Right = {wList[0]-1,wList[1]-1,wList[2]-1,wList[3]-1,	wList[4]-1,wList[5]-1,wList[6]-1,wList[7]-1,	wList[8]-1,wList[9]-1,wList[10]-1,wList[11]-1},
			.Grid.Top[0] = (36 + 32 +2),
			.Grid.Top[1] = (36 + 32 +2) + (2 + hList[0]),
			.Grid.Top[2] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]),
			.Grid.Top[3] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]),
			.Grid.Top[4] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]),
			.Grid.Top[5] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]),
			.Grid.Top[6] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]),
			.Grid.Top[7] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]),
			.Grid.Top[8] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]),
			.Grid.Top[9] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + (2 + hList[8]),
			.Grid.Top[10] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + (2 + hList[8]) + (2 + hList[9]),
			.Grid.Top[11] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + (2 + hList[8]) + (2 + hList[9]) + (2 + hList[10]),
				
			.Grid.Bottom[0] = (36 + 32 +2) + hList[0],
			.Grid.Bottom[1] = (36 + 32 +2) + (2 + hList[0]) + hList[1],
			.Grid.Bottom[2] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + hList[2],
			.Grid.Bottom[3] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + hList[3],
			.Grid.Bottom[4] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + hList[4],
			.Grid.Bottom[5] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + hList[5],
			.Grid.Bottom[6] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + hList[6],
			.Grid.Bottom[7] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + hList[7],
			.Grid.Bottom[8] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + hList[8],
			.Grid.Bottom[9] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + (2 + hList[8]) + hList[9],
			.Grid.Bottom[10] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + (2 + hList[8]) + (2 + hList[9]) + hList[10],
			.Grid.Bottom[11] = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + (2 + hList[4]) + (2 + hList[5]) + (2 + hList[6]) + (2 + hList[7]) + (2 + hList[8]) + (2 + hList[9]) + (2 + hList[10]) + hList[11],
			
			.Grid.UsingCount = 4,
			
			.Panel.Active = 1,
			.Panel.ItemType = piMemo,
			.Panel.Left = wList[1] + 6,
			.Panel.Top = (36 + 32 +2) + hList[0] +6,
			.Panel.Width = 502,
			.Panel.Heigh = 220,
			.Panel.Right = wList[1] + 6 + 502 -1,
			.Panel.Bottom = (36 + 32 +2) + hList[0] +6 + 220 -1,
			
			.PanelSmall.Active = 1,
			.PanelSmall.Left = wList[1] + 6,
			.PanelSmall.Top = (36 + 32 +2) + hList[0] +6 + 220 -1 + 1,
			.PanelSmall.Width = 502,
			.PanelSmall.Heigh = 72,
			.PanelSmall.Right = wList[1] + 6 + 502 -1,
			.PanelSmall.Bottom = (36 + 32 +2) + hList[0] +6 + 220 -1 + 1 + 72 -1,
		};
		static constexpr TRect ModuleRectLv3 = {
			.Top = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]),
			.Bottom = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + hList[3] + 30, 
			
		};
		static constexpr TRect ModuleRectLv4 = {
			.Top = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + 30,
			.Bottom = (36 + 32 +2) + (2 + hList[0]) + (2 + hList[1]) + (2 + hList[2]) + (2 + hList[3]) + hList[4], 
		};
		static const unsigned int LvGridHighBottom3 = 102;
		static constexpr unsigned char dTypeList[256] = {
			dgTYPE_SMOKE_DETECTOR,
			dgTYPE_TEMPE_DETECTOR,
			dgType_VAALRAM,
		};
		static constexpr unsigned char dSmokeSensGrade_MarkValue[8] = {
			//100, 75, 50, 25
			0, 100, 75, 50, 25, 0, 0, 0
		};
		
		static constexpr unsigned char dNetDevRegCmdSeq[4][8] = {
				"REMOTE\0",
				"REGIST\0",
				"TRY\0",
				"START\0",
		};


	public:
		static class TItemList	*sIList;
		class TItemList	*IList;
	
		struct{
			unsigned char InxMin;
			unsigned char InxMax;
			unsigned char Inx;
			void Reset(){
				Inx = 0;
				InxMin = 0;
				InxMax =2;
			}
		}Tag;
	
		class TThisMainCtl *aMc;
		
		unsigned char IsPermission;
		unsigned char PermissionCmd;
	
		unsigned char IsOnShow;
		unsigned char IsOnGo;
		unsigned char IsOverWrite;
		unsigned char IsOnNetwork;
		unsigned char HasDrawDetailOnFinish;
		//int SpendTime;
	
		unsigned char ProgressVal[40];
		unsigned char SearchCompleted[40];
		unsigned char HasRpy[40];
		unsigned char HasQuery[40];
		unsigned char TransmiteCompleted[40];
		unsigned char ItemsCount[40];
		unsigned char DelayCheckTick;
		unsigned char IsTransmiting;
		
		struct{
			unsigned short All;
			unsigned short Smoke;
			unsigned short Tempe;
			unsigned short HandReport;
			unsigned short HydKp;
			unsigned short ModuleInput;
			unsigned short ModuleInOut;
			unsigned short ModuleOutput;
			unsigned short Va;
			unsigned short FDisplay;
			unsigned short SmokeTempe;
			unsigned short LINKAGE_POWERSUPLY;
			unsigned short cOther;
			void Reset(){
				All =0;
				Smoke =0;
				Tempe =0;
				HandReport =0;
				HydKp =0;
				ModuleInput =0;
				ModuleInOut =0;
				ModuleOutput =0;
				Va =0;
				FDisplay =0;
				SmokeTempe =0;
				LINKAGE_POWERSUPLY = 0;
				cOther =0;
			}
		}Count;
		
		union{
			unsigned int D32[16];
			char Body[64];
		}Text;
		unsigned char Data[8];
		unsigned char RecData[8];
		unsigned char RegWhat;
		unsigned char ReqPath;
		unsigned char IsQuerying;
		unsigned char QueryNum;
		unsigned char QueryNumRpy;
		unsigned char QueryTimeOut;
		unsigned char RdyToShowTick;
		unsigned char TotalProgress;
		unsigned char HasDone;
		unsigned char ResetHasSend;
		unsigned char IsTimeOutReturn;
		unsigned char PortNumOnGoing;
		
		int WastTimeSec;
		
		unsigned int  fAddr;
		unsigned char FlashPageData[256];
		
		int IsDevRegisting;
		
		struct{
			unsigned char CtlNum;
			unsigned char RegWhat;
			unsigned char TryStart;
			unsigned char ProgressVal;
			unsigned char PortCount;
			unsigned char HasDone;
			unsigned char SlaveState;
			unsigned char ProgressHasUpdate;
			unsigned char PortCountHasUpdate;
			unsigned char EpCountHasUpdate;
			unsigned char HadTransited2Master;
			unsigned char MasterShowDetailTick;
			unsigned char StartSeqIdf;
			unsigned char Wait4SendFinishedTick;
			unsigned char MasterHasRecCount;
			unsigned char Completed;
			unsigned char ReSendEpCountTick;
			
			void Reset(){
				CtlNum =0;
				RegWhat =0;
				TryStart =0;
				ProgressVal =0;
				PortCount =0;
				HasDone =0;
				SlaveState =0;
				ProgressHasUpdate =0;
				PortCountHasUpdate =0;
				EpCountHasUpdate =0;
				HadTransited2Master =0;
				MasterShowDetailTick =0;
				StartSeqIdf = 0;
				Wait4SendFinishedTick = 0;
				MasterHasRecCount =0;
				Completed =0;
				ReSendEpCountTick = 0;
			}
		}Nw;
		struct{
			unsigned char Exist[4];
		}Psv;
	public:
		TRegister(){
			IsOnShow = 0;
			IsOnGo = 0;
		};
		void Init();
		void RenderBackGround(void);

		void DrawSelf(void);
		void DrawCommonByExReturn();
		void Show();
		void FullRedraw(int Prm);
		void ShowPortCount();
		void ShowDetail(int aForce =0);
			
		static void sDelayMs(unsigned int aMs);
			
		void DrawProgressAll();
		void DrawProgress100();
		void DrawDoneAll();
		void DrawFixText();
			
		void PermissionCmdReturnDraw();
		void PermissionCmdGO();
		
		void ClearSelected(void);
		void Check4Selected();
		void InitDataBuf();
		void GetEpDevCount();

		TGuiMsgReturn KeyIn(unsigned char aKey);
			
		void ClearProgressVal();
		void ClearCircuitTranmiteData(unsigned char aCc);
		void SendCmdAll();
		void SendCmdOne(unsigned char aCc);
		void SendCmdQuery(unsigned char aCc);
		void SendCmdAbort();
		
		void PushRegisterData(unsigned char aCc, unsigned char aInx);
		void KeyPadUartPushData(unsigned char aCmd, unsigned char *pData);
		
		void iePushOneMsgReport(TRecordCommon aIm, unsigned char aCmd);
		
		
		
		unsigned char GetItemsCount(unsigned char aCc);
		int CheckIsDone();
		void LoadToMainCtl();
		void SaveToFlash();
		
		int Task1000Ms();
		
		
		void Show4Slave();
		
		void MasterStartByRemote(unsigned char aCtlNum, unsigned char aPath);
		void MasterCanNetGetPortCount(unsigned char aCnt);
		void MasterCanNetGetProgressVal(unsigned char aVal);
		void MasterCanNetGetEpCount(unsigned char aDType, unsigned char LowByte, unsigned char HiByte);
		void MasterCanNetGetEpCountFinished();
		void MasterCanNetSendStartSeq(unsigned char aCtlNum, unsigned char aPath);
		void MasterCanNetGetStartState();
		
		void MasterCanNetShowTryStart();
		void MasterCanNetShowStart();
		void MasterCanNetShowProgress();
		void MasterCanNetShowItems();
		
		void MasterSendCmdReqEpCount();
		void MasterSendCmdEpCountCorrect();
		
		void SlaveReplyEpCountTransmited();
		void SlaveRpyEpCount();
		
		unsigned char SlaveGetStartSeq(unsigned char *pData);
		void SlaveRegInit(unsigned char aPath);
		void SlaveReplyProgress(unsigned char aProg);
		void SlaveReplyPortCount( unsigned char aCnt);
		void SlaveReplyEpCount(unsigned char aDType, unsigned short aCnt);
		void SlaveReplyAllEpCount();
		void SlaveReplyStartState(unsigned char aSta);
		void SlaveModeTask1000Ms();
		int SlaveCheckIsDone();
		
		int CompCount();
		void SlaveRecMasterGotCompleted();
		
		static void sWriteOnePort(unsigned char aPort);
		static void sWritePortList();
		
		int ExtRequst(unsigned char Prm);
};



































#endif
