#ifndef DIRECTPADCFG_H_
#define DIRECTPADCFG_H_

#include "gType.h" 
#include "GraphBase.h"
#include "my_string.h"
#include "MainCtl.h"
#include "Device.h"
#include "CtlPanel1.h"

//For Idle View or Edit
class TDirectPadCfg{
	public:
		static constexpr unsigned short hList[12] = {72,72,72,72,	72,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 = 5,
			
			.Panel.Active = 1,
			.Panel.ItemType = piMemo,
			.Panel.Left = wList[1] + 6,
			.Panel.Top = (36 + 32 +2) + hList[0] +6,
			.Panel.Width = 502,
			.Panel.Heigh = 260,
			.Panel.Right = wList[1] + 6 + 502 -1,
			.Panel.Bottom = (36 + 32 +2) + hList[0] +6 + 260 -1,
			
			.PanelSmall.Active = 1,
			.PanelSmall.Left = wList[1] + 6,
			.PanelSmall.Top = (36 + 32 +2) + hList[0] +6 + 260 -1 + 1,
			.PanelSmall.Width = 502,
			.PanelSmall.Heigh = 28,
			.PanelSmall.Right = wList[1] + 6 + 502 -1,
			.PanelSmall.Bottom = (36 + 32 +2) + hList[0] +6 + 260 -1 + 1 + 28 -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
		};
		enum TCmd{
			CmdEditDescp =1,
			CmdWriteUserCode =2,
			CmdWriteAreaCode =3,
			CmdWriteAssignType =4,
			CmdWriteCfg = 5,
			CmdWriteDescp
		};
	public:
		int IsShow;
		static class TItemList *sIList;
		class TItemList	*IList;
		class TCtlPanel1	*aPanel;
		int EnterMethod;
		int IsPermission;
		TCmd PermissionCmd;
		unsigned char ResultPst;
		int EdittingDescp;
		int IsExtInput;
		unsigned char PortNumValid;
		unsigned char AddrNumValid;
	
		struct{
			unsigned char InxMin;
			unsigned char Inx;
			void Reset(){
				InxMin = 0;
			}
		}Tag;
		struct{
			unsigned char IsPadDeActive;
			unsigned char IsCheckFault;
			unsigned char IsNodeUsing;
		}Rt;
		
		unsigned char Path0;
		unsigned char Path1;
		unsigned char Path0Valid;
		unsigned char Path1Valid;
		
		short GroupIndex;
		short SelectedLine;
		short SelectedIndex;
		short SelectedRow;
	
		u32 FixColColor;
		u32 FixColTextColor;
		u32 Color;
		u32 TextColor;
		u32 SelectedColor;
		u32 SelectedTextColor;
	
		int TabOrder;
		
		unsigned char ShowResultTick;
		unsigned char JustShowResult;
		
		unsigned short AreaNum;
		
	
		TPoint OwnerLeftTop; //ParrentLeftTop;
		TRect Bound;	
		TRect Content;
		
		struct{
			unsigned char P0;
			unsigned char Key;
			unsigned char EditVal[dDIRECTCTL_KEY_MAX_COUNT][5];
		}Loaded;
		
		struct{
			unsigned char EntryCount;
			unsigned char PageCount;
			unsigned char CurrentPage;
			char Text[16];
			void TextReset(){
				for(int x=0; x<16; x++)Text[x] =0;		
			}
		}List;
		
		unsigned char PadData[dDIRECTCTL_KEY_MAX_COUNT][5];
		
		union{
			unsigned char D8[256];
			unsigned short D16[128];
			unsigned int  D32[64];
		}ReadWriteData;
		char TextHasSave[36];
	private:
		void SetListColWidth(int w0, int w1, int w2, int w3, int w4,int w5);
		
	public:
		TDirectPadCfg(){};
		TDirectPadCfg(int aMode){
			Init(aMode);
		}
		void Init(int aMode);	
		
		void RenderBackGround(void);
		void RenderBackGround(unsigned int aClr);
		void DrawSelf(void);
		void Show(void);
		void DrawFixText();
		
		void DrawIsPadDeActive();
		void DrawUserCodeAssignAreaAssignType();
		void DrawIsNodeUsing();
		void DrawISCheckFault();
		
		void DrawPadBoardCfgList();
		
		void FullRedraw(int Prm);

		static void sDelayMs(unsigned int aMs);
		void DrawState();
		void Redraw4DescpEdit();
		void ReLoadAndDrawDescp();
		void InitPanel();
		void ClearSelected();
		void Check4Selected();
		
		void PermissionCmdReturnDraw();
		void PermissionCmdGO();
		
		void sRestoreDescp(int aP0, int aP1, char *p);
		
		void SetPath(unsigned char aCtlNum, unsigned char Path0, unsigned char Path1);
	
		void WriteDescp2Flash();
		static void sWrite2Flash(unsigned char aPath0);
		static void sWriteUserCodeAssignType(unsigned char aPath0);
		static void sWriteAreaNum();
		void WriteUserCode2Flash(unsigned char aPath0, unsigned char aPath1, TUserCode aUc);
		void WriteAssignType2Flash(unsigned char aPath0, unsigned char aPath1, unsigned char aAssignType);
		void Write2Flash(unsigned char aPath0, unsigned char aPath1);
		void WriteAreaNum2Flash(unsigned char aPath0, unsigned char aAddr, unsigned short aNum);
		
		void ReadDirectPadCfg(unsigned char aPath0);
		
		void LoadCfg4Edit(unsigned char aPath0, unsigned aPath1);

		TGuiMsgReturn KeyIn(unsigned char aKey);
		
		static void sRecoverSetDefault();
		
		void Hide();
		
		void Task1000Ms();
		
		int ExtRequst(unsigned char Prm);
};












#endif
