#ifndef LINKAGEPROGRAM_H_
#define LINKAGEPROGRAM_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"


   /* int64 nHeaderBytes;                          // header bytes of the original 
    int64 nDataBytes;                            // data bytes of the original 
    int64 nTotalBytes;                           // total bytes of the original 
    int64 nTotalBytes;                           // total bytes of the file
    int64 nTotalBlocks;                             // the total number blocks
    int nLengthMS;                                  // the length in milliseconds
    int nAverageBitrate;                            // the kbps (i.e. 637 kpbs)
    int nDecompressedBitrate;                       // the kbps of the decompressed audio (i.e. 1440 kpbs)
    int nJunkHeaderBytes;                           // used for, etc.
    int nSeekTableElements;                         // the number of elements in the seek table(s)
    int nMD5Invalid;                                // whether the is valid*/

class TLinkageProgram{
	public:
		static constexpr unsigned short hList[12] = {36,32,300,150,	32,32,32,32,	32,32,32,32};
		static constexpr unsigned short wList[12] = {800,800,800,800,	800,800,800,800,	800,800,800,800};
		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 = 3,
			
			.Panel.Active = 0,
			.Panel.ItemType = piMemo,
			.PanelSmall.Active = 0,
		};
	public:
		int IsShow;
		unsigned char IsPermission;
		unsigned char PermissionCmd;
	
		class TEdit		EEntry;
	
		class TEdit 	EuCode;
		class TEdit 	EdType;
		class TEdit 	ELogic;
		class TEdit		EDelay;
		class TEdit		EInnerCount;
		class TEdit		EInCount;
		
		TStaticText STextDType;
		
		unsigned char IsSaveEn;
		unsigned char IsDeleteEn;
		unsigned char EnterMethod;
		unsigned char ShouldBeLoad;
		unsigned char ShouldBeClear;
		unsigned char ParCntHasChanged;
		unsigned char tAvalible;
		unsigned char tLogic;
		unsigned char IsInput;
		unsigned char IsMask;
		unsigned char IsSelfActive;
		unsigned char ParCnt;
		unsigned char DoStop;
		unsigned char InCount;
		char tEntry[12][4][24];
		char EntryChar[24];

		u32 Color;
	
		unsigned char ItemsIndex;
		unsigned char EditIndex;
		unsigned char OnEditing;
		unsigned char CharIndex;
		unsigned char IsInputSymbol;
		unsigned char IsProgram;
	
		char PathText[64];
		unsigned char CtlNum;
		unsigned char Path0;
		unsigned char Path1;
		
		unsigned char IsViewList;
		int InputCount;
		int InputPageCount;
		int InputPageIndex;
		unsigned char CurrentPath0;
		
		int EditingLinkageIndex;
		int ExpCount;
		int IsNew;
		unsigned char ExistTabel[dLINKAGE_MAX_COUNT];
		
		union{
			unsigned char D8[256];
			unsigned int  D32[64];
		}ReadWriteData;
		unsigned char ActX;
		unsigned char Inx;
		unsigned char eX, eY;
		unsigned char eWhat;
		unsigned char IsWhowingTip;
		unsigned char EntryState;
	public:
		TLinkageProgram(){};
		void Init();
		void RenderBackGround(void);
		void RenderBackGround(unsigned int aClr);
		void RenderContent(void);
		void DrawSelf(void);
		void DrawCaption(void);
		void DrawCaption2();
		void Show();
		void FullRedraw(int Prm);
		void DelayUs(unsigned int aUs);
		void DelayMs(unsigned int aMs);
		void PaintExp();
		void DrawInputListXX();
		void ShowTip(int aTip);
		void ShowOneGrid(unsigned int x, unsigned int y);
		void ShowAllGrid();
		void RedrawEEdit();
		void RenderGridBack();
		void DrawSelectedGrid(unsigned int oldX, unsigned int oldY, unsigned int NewX, unsigned int NewY);
		void DrawFixText();
			
		void TryDecValue(int IsForce);
		void TryIncValue(int IsForce);
		void ClearSelected(void);	
		void Check4Selected();
		void CheckEWhatWhoActive();
		void InitPanel();
		void DrawGridLine();	
		void DrawInputTip();
		void DrawTip();
		unsigned char LoadInputList(unsigned char Path0);
		int TrySearchStartPort();
		int SearchPort(unsigned char aP0);
		int TrySearchPrePort();
		int TrySearchNextPort();
			
		void LoadCfg();
		TGuiMsgReturn KeyIn(unsigned char aKey);
			
		void LoadExpression();
		void SetEditStart();
		int TryRestoreFromEdit();
		void RestoreFromEdit();
			
		void LoadInputList();
		
		void IdfButtonEn();
		
		void ShowEntryState();
		void FindNextExp();
		int NewEntry();
		void WriteEntry();
		void ReScanExpViaRam();
		void WaitChipIdle(unsigned int aCnt);
		void LoadExpCountFromRam();
		void LoadExpression2Ram();
		void LoadExpression2Ram(unsigned int aInx);
		void LoadExpression2RamFor4K(unsigned int aInx);
		void LoadExpression2Edit(unsigned int aInx);
		void Erase4Index(unsigned int aInx);
		void Write4Index(unsigned int aInx);
		void DeleteEntry();
		
		void ShowItemEdit();
		void RedrawItemEdit();
		void RedrawParCnt();
		void ClearPanelSelected();
		void ClearGridSelected();
		
		void LoadExpVoid();
		void LoadExp(unsigned int aExp);
		void RestoreExp(unsigned int aExp);
		
		void GetLnkMask(unsigned int aExp);
		void SetLnkMask(unsigned int aExp);
		void GetLnkSelfActive(unsigned int aExp);
		void GetLnkDoStop(unsigned int aExp);
		void SetLnkDoStop(unsigned int aExp);
		void SetLnkSelfActive(unsigned int aExp);
		
		void CheckUserCodeUniquely(int IsInput);
		
		void DrawActiveByStartStop();
		void DrawIsMask();
		void DrawIsSelfActive();
		
		void PermissionCmdGO();
		void PermissionCmdReturnDraw();
		
		void Skip2Entry(unsigned int aEntry);
		void EntryClear();
		int CheckUserCodeOk();
		int CheckTypeOk();
		void CheckShowDevType();
		
		
		int ExtRequst(unsigned char Prm){IsShow =0; return 0;}
};



































#endif
