#ifndef _CEGOADMSCREEN_H_INCLUDED_
#define _CEGOADMSCREEN_H_INCLUDED_
///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoAdmScreen.h  
// ---------------
// Cego admin screen interface definition
//
// Design and Implementation by Bjoern Lemke
//
// (C)opyright 2000-2025 Bjoern Lemke
//
// INTERFACE MODULE
//
// Class: CegoAdmScreen
// 
// Description: Cursor based administration database administration frontend
//
// Status: CLEAN
//
///////////////////////////////////////////////////////////////////////////////

#include <ctype.h>
#include <string.h>

#include <lfcbase/Chain.h>
#include <lfcbase/ListT.h>
#include <lfcbase/SetT.h>
#include <lfcbase/Datetime.h>
#include <lfcbase/SigHandler.h>
#include <lfcbase/Screen.h>

#include "CegoAdminHandler.h"
#include "CegoXMLdef.h"

#define BP_HELP_TITLE "BufferPool Help"
#define BP_HELP_TEXT "\
Key-Left  - Pool A Info\n\
Key-Right - Pool B Info\n\
Esc       - Leave"

#define TPDB_HELP_TITLE "DB ThreadPool Help"
#define TPDB_HELP_TEXT "\
Key-Down - Select next thread\n\
Key-UP   - Seleted previous thread\n\
Enter    - Show last queries for selected thread\n\
c        - Show most expensive queris\n\
a        - Abort selected thread\n\
Esc      - Leave"

#define TS_HELP_TITLE "Tableset Help"
#define TS_HELP_TEXT "\
Key-Down - Select next tableset\n\
Key-UP   - Seleted previous tableset\n\
Enter    - Open tableset menu\n\
n        - New tableset\n\
Esc      - Leave"

#define USR_HELP_TITLE "User Help"
#define USR_HELP_TEXT "\
Key-Down - Select next user\n\
Key-UP   - Seleted previous user\n\
Enter    - Open user menu\n\
n        - New user\n\
Esc      - Leave"

#define RL_HELP_TITLE "User Help"
#define RL_HELP_TEXT "\
Key-Down - Select next role\n\
Key-UP   - Seleted previous role\n\
Enter    - Open role menu\n\
n        - New role\n\
Esc      - Leave"

#define PRM_HELP_TITLE "User Help"
#define PRM_HELP_TEXT "\
Key-Down - Select next permission\n\
Key-UP   - Seleted previous permission\n\
Enter    - Edit permission\n\
n        - New permission\n\
d        - Delete permission\n\
Esc      - Leave"

#define AL_HELP_TITLE "Archlog Help"
#define AL_HELP_TEXT "\
Key-Down - Select next archive location\n\
Key-UP   - Seleted previous archive location\n\
c        - Toggle archive mode\n\
d        - Delete archive location\n\
Esc      - Leave"


class CegoAdmScreen : public Screen
{
    
public:
    
    CegoAdmScreen(CegoAdminHandler *pAH);
    ~CegoAdmScreen();
    
private:
    
    ////////////////
    // Menu Panel //
    ////////////////

    class AdminMenu : public Screen::Menu {
    public:
    
	AdminMenu() : Screen::Menu() {
	    
	}
		
    private:
	

    };

    ///////////////////////
    // Buffer Pool Panel //
    ///////////////////////
    
    class BufferPool : public Screen::Attribute {
    public:
    
	BufferPool(CegoAdminHandler* pAH, Screen::Panel* pParent)
	    : Screen::Attribute(pParent, Attribute::OVERLAY) {
	    _pAH = pAH;
	    _pHelp = new Screen::Message(this);
	    _pHelp->setInfo(BP_HELP_TITLE, BP_HELP_TEXT, 3);
	}

       ~BufferPool() {	   
	   delete _pHelp;
	}
	
	
	bool doRefresh() { return true; }
	void refresh();

	Screen::Panel *onKey(unsigned c) {
	    return _pHelp;
	}
	
    private:

	Screen::Message* _pHelp;
	CegoAdminHandler* _pAH;
    };
    
    ///////////////////////
    // Thread Pool Panel //
    ///////////////////////
    
    class ThreadPool : public Screen::Select {
    public:
    
	ThreadPool(CegoAdminHandler* pAH, Screen::Panel* pParent, const Chain& title);
	Screen::Panel* onSelect(unsigned selected);	

    private:
	
	class DbPool : public Screen::Grid {
	public:
	
	    DbPool(CegoAdminHandler* pAH, Screen::Panel* pParent);
	    ~DbPool();
	    Screen::Panel* onSelect(unsigned rowNum);
	    Screen::Panel* onKey(unsigned c, unsigned rowNum);
	    
	    ListT< ListT<Chain> > getData() { return _threadTable; };
	    
	    bool doRefresh() { return true; }
	    void refresh();

        private:
	    
	    class ThreadQuery : public Screen::Grid {		
	    public:
		
		ThreadQuery(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~ThreadQuery();

		void setTitle(const Chain& title);
		void setHistory( ListT< ListT<Chain> >& queryHist);
		void setQueryFull( ListT<Chain>& queryFull);
		
		Screen::Panel* onSelect(unsigned rowNum);
		
		ListT< ListT<Chain> > getData() { return _queryTable; };
		
		bool doRefresh() { return false; }
		void refresh() {};
		
	    private:
		
		class QueryInfo : public Screen::Message {
	        public:
		    
		    QueryInfo(Screen::Panel* pParent) : Screen::Message(pParent) {	
		    }
		    
		    void setQuery(const Chain& title, const Chain& msg, unsigned numRow) {
			setInfo(title, msg, numRow);
		    }
	        };
		
	        QueryInfo* _pQueryInfo;
	        ListT< ListT<Chain> > _queryTable;
		
		ListT<Chain> _queryFull;		
		ListT<Chain> _querySchema;
		
		unsigned format(const Chain& query, Chain& formatted);
		CegoAdminHandler *_pAH;
	    };

	    class ThreadAbort : public Screen::Confirm {		
	    public:

		ThreadAbort(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~ThreadAbort();
		void setup( unsigned threadId);
		Screen::Panel* onConfirm();		
		Screen::Panel* onCancel() { return _pParent; }
	       	      
	    private:

		CegoAdminHandler *_pAH;
		unsigned _threadId;
	    };

	    void handleResult();

	    Screen::Message* _pHelp;
	    ThreadQuery* _pQuery;
	    ThreadAbort* _pAbort;
	    ListT< ListT<Chain> > _threadTable;
	    ListT<Chain> _threadSchema;
	    CegoAdminHandler* _pAH;
	};
	
	class AdmPool : public Screen::Grid {
	public:
	    
	    AdmPool(CegoAdminHandler* pAH, Screen::Panel* pParent);
	    ListT< ListT<Chain> > getData() { return _threadTable; };
	    bool doRefresh() { return true; }
	    void refresh();
	    	    
	private:	    
	 
	    ListT< ListT<Chain> > _threadTable;
	    ListT<Chain> _threadSchema;
	    CegoAdminHandler* _pAH;
	};
      
	CegoAdminHandler* _pAH;
	DbPool* _pDb;
	AdmPool* _pAdm;
    };

    ////////////////////
    // Tableset Panel //
    ////////////////////

    class TableSet : public Screen::Grid {
	
    public:
	
	TableSet(CegoAdminHandler* pAH, Screen::Panel* pParent, const Chain& title);	
	~TableSet();
	Screen::Panel* onSelect(unsigned selected);
	Screen::Panel* onKey(unsigned c, unsigned rowNUm);
	ListT< ListT<Chain> > getData() { return _tsTable; };

	void refresh();
	bool doRefresh() { return true; };
	
    private:
	
	class Add : public Screen::Form {
	public:
	    
	    Add(CegoAdminHandler* pAH, Screen::Panel* pParent, const Chain& title);	    
	    ~Add();

	    Screen::Panel* onConfirm(const ListT<Chain>& valList);
	    Screen::Panel* onCancel() { return _pParent;   }
	    
	private:
	    
	    CegoAdminHandler* _pAH;	    
	};

	class Action : public Screen::Select {
	public:
	    
	    Action(CegoAdminHandler* pAH, Screen::Panel* pParent);
	    ~Action();	    
	    void setTableSet(const Chain& tableSet, const Chain& tsState, const Chain& archMode);
	    Screen::Panel* onSelect(unsigned selected);	    
	    
	private:

	    class Edit : public Screen::Form {
	    public:
		
		Edit(CegoAdminHandler* pAH, Screen::Panel* pParent);	    
		~Edit();

		void setup(const Chain& tableSet);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
		Screen::Panel* onCancel() { return _pParent;   }
		
	    private:

		Chain _tableSet;
		CegoAdminHandler* _pAH;		
	    };
	    
	    class Archlog : public Screen::Grid {
	    public:
		
		Archlog(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Archlog();
		void setup(const Chain& tableSet, const Chain& archMode);
		Screen::Panel* onSelect(unsigned rowNum);
		Screen::Panel* onKey(unsigned c, unsigned rowNum);
		ListT< ListT<Chain> > getData();
		void refresh();
		bool doRefresh() { return true; };
		
	    private:
		
		class Add : public Screen::Form {
		public:
		    
		    Add(CegoAdminHandler* pAH, Screen::Panel* pParent);		
		    ~Add();
		    
		    void setup(const Chain& tableSet);
		    Screen::Panel* onConfirm(const ListT<Chain>& valList);			    
		    Screen::Panel* onCancel();
		    
		private:
		    
		    Chain _tableSet;
		    CegoAdminHandler* _pAH;
		};

		class Conf : public Screen::Form {
		public:
		    
		    Conf(CegoAdminHandler* pAH, Screen::Panel* pParent);		
		    ~Conf();
		    
		    void setup(const Chain& tableSet, const Chain& archMode);
		    Screen::Panel* onConfirm(const ListT<Chain>& valList);			    
		    Screen::Panel* onCancel();
		    
		private:
		    
		    Chain _tableSet;
		    CegoAdminHandler* _pAH;
		};

		class Delete : public Screen::Confirm {		    
		public:
		    
		    Delete(CegoAdminHandler* pAH, Screen::Panel* pParent);
		    ~Delete();
		    void setup(const Chain& tableSet, const Chain& al);
		    Screen::Panel* onConfirm();		
		    Screen::Panel* onCancel();
		    
		private:

		    Chain _tableSet;
		    Chain _al;
		    CegoAdminHandler *_pAH;
		};

		Add* _pAdd;
		Conf* _pConf;
		Delete* _pDelete;
		Screen::Message* _pHelp;
		
		Chain _tableSet;
		Chain _archMode;
		ListT< ListT<Chain> > _alTable;
		ListT<Chain> _alSchema;
		CegoAdminHandler* _pAH;
	    };
	    
	    class Backup : public Screen::Grid {
	    public:

		Backup(CegoAdminHandler* pAH, Screen::Panel* pParent);
		void setup(const Chain& tableSet, const Chain& tsState);
		Screen::Panel* onSelect(unsigned rowNum);
		Screen::Panel* onKey(unsigned c, unsigned rowNum);
		ListT< ListT<Chain> > getData();
		void refresh();
		
	    private:
		
		class Add : public Screen::Confirm {
		public:
		
		    Add(CegoAdminHandler* pAH, Screen::Panel* pParent);		    
		    void setup(const Chain& tableSet, const Chain& tsState);
		    Screen::Panel* onConfirm();
		    
		private:
		   
		    Chain _tableSet;
		    Chain _tsState;
		    
		    CegoAdminHandler* _pAH;
		};


		Add* _pAdd;
		Chain _tableSet;
		Chain _tsState;
		bool _doRefresh;
		ListT< ListT<Chain> > _buTable;
		ListT<Chain> _buSchema;
		CegoAdminHandler* _pAH;
	    };

	    class Restore : public Screen::Form {
	    public:
		
		Restore(CegoAdminHandler* pAH, Screen::Panel* pParent);		    
		void setup(const Chain& tableSet);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
		
	    private:
		
		Chain _tableSet;
		CegoAdminHandler* _pAH;
	    };


	    class Recover : public Screen::Form {
	    public:
		
		Recover(CegoAdminHandler* pAH, Screen::Panel* pParent);		    
		void setup(const Chain& tableSet);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
		
	    private:
		
		Chain _tableSet;
		CegoAdminHandler* _pAH;
	    };

	    
	    
	    class FilePanel : public Screen::Grid {
	    public:
		
		FilePanel(CegoAdminHandler* pAH, Screen::Panel* pParent);	   	    
		void setup(const Chain& tableSet);
		Screen::Panel* onSelect(unsigned rowNum) { return this;  } 
		Screen::Panel* onKey(unsigned c, unsigned rowNum);
		ListT< ListT<Chain> > getData() { return _fileTable; };
		void refresh();
		
	    private:
		
		class AddFilePanel : public Screen::Form {
		public:
		    
		    AddFilePanel(CegoAdminHandler* pAH, Screen::Panel* pParent);		    
		    void setup(const Chain& tableSet, const Chain& rootPath);
		    Screen::Panel* onConfirm(const ListT<Chain>& valList);
		    
		private:

		    Chain _rootPath;
		    Chain _tableSet;
		    CegoAdminHandler* _pAH;
		};
		
		AddFilePanel* _pAddFile;
		Chain _rootPath;
		Chain _tableSet;
		bool _doRefresh;
		ListT< ListT<Chain> > _fileTable;
		ListT<Chain> _fileSchema;
		CegoAdminHandler* _pAH;
	    };
	
 	    class XPort : public Screen::Form {
	    public:	    

		XPort(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~XPort();

		void setup(const Chain& tableSet, bool isExport);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
	 
	    private:

		bool _isExport;
		Chain _tableSet;
		CegoAdminHandler* _pAH;
	    };

	    class QueryCache : public Screen::Form {
	    public:	    

		QueryCache(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~QueryCache();

		void setup(const Chain& tableSet);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
	 
	    private:

		Chain _tableSet;
		CegoAdminHandler* _pAH;
	    };

	    class TableCache : public Screen::Form {
	    public:	    

		TableCache(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~TableCache();

		void setup(const Chain& tableSet);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
	 
	    private:

		Chain _tableSet;
		CegoAdminHandler* _pAH;
	    };

	    class Parameter : public Screen::Form {
	    public:	    

		Parameter(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Parameter();

		void setup(const Chain& tableSet);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
	 
	    private:

		Chain _tableSet;
		CegoAdminHandler* _pAH;
	    };
	    
	    class InfoPanel : public Screen::Attribute {
	    public:	    

		InfoPanel(CegoAdminHandler* pAH, Screen::Panel* pParent);
		void setup(const Chain& tableSet);	    
		void refresh();           
		bool doRefresh() { return true; };
		
	    private:
		
		Chain _tableSet;
		CegoAdminHandler* _pAH;	    
	    };

	    class Drop : public Screen::Confirm {
	    public:
		
		Drop(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Drop();
		void setup(const Chain& tableSet);	    
		Screen::Panel* onConfirm();
		
	    private:

		Chain _tableSet;
		CegoAdminHandler* _pAH;	    
	    };
	    
	    Chain _tableSet;
	    Chain _archMode;

	    
	    Edit* _pEdit;
	    FilePanel* _pFilePanel;
	    InfoPanel* _pInfoPanel;
	    
	    Archlog* _pArch;

	    Backup* _pBackup;
	    Restore* _pRestore;
	    Recover* _pRecover;
	    
	    XPort* _pXPort;
	    Drop* _pDrop;
	    QueryCache* _pQueryCache;
	    TableCache* _pTableCache;
	    Parameter* _pParameter;
	    
	    CegoAdminHandler* _pAH;
	};
	
	Add* _pAdd;
	Action* _pAction;
	Screen::Message* _pHelp;
	
	ListT<Chain> _tsSchema;
	ListT< ListT<Chain> > _tsTable;
	CegoAdminHandler* _pAH;	      
    };

    
    /////////////////
    // User Panels //
    /////////////////
    
    class User : public Screen::Grid {
    public:
	
	User(CegoAdminHandler* pAH, Screen::Panel* pParent);
	~User();

	Screen::Panel* onSelect(unsigned selectedRow);
	Screen::Panel* onKey(unsigned c, unsigned rowNum);
	ListT< ListT<Chain> > getData();
	void refresh();
	bool doRefresh() { return true; };

    private:

	SetT<Chain> roleString2Set(const Chain& roleString);
	    
	class Add : public Screen::Form {
	public:
	    
	    Add(CegoAdminHandler* pAH, Screen::Panel* pParent);	    
	    ~Add();
	    Screen::Panel* onConfirm(const ListT<Chain>& valList);
	    Screen::Panel* onCancel();
	    
	private:
	    
	    CegoAdminHandler* _pAH;	    
	};

	class Action : public Screen::Select {
	public:
	    
	    Action(CegoAdminHandler* pAH, Screen::Panel* pParent);	
	    ~Action();	
	    void setup(const Chain& user, const SetT<Chain>& userRoleSet, bool isTrace);
	    Screen::Panel* onSelect(unsigned selected);
	    
	private:
		
	    class Passwd : public Screen::Form {
	    public:
		
		Passwd(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Passwd();
		void setUser(const Chain& user);		
		Screen::Panel* onConfirm(const ListT<Chain>& valList);		
		Screen::Panel* onCancel();
		
	    private:
		
		Chain _user;
		CegoAdminHandler* _pAH;		
	    };

	    class Assign : public Screen::Form {
	    public:
		
		Assign(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Assign();

		void setup(const Chain& user, const SetT<Chain> roleSet, bool isAssign);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
		
	    private:
		
		bool _isAssign;
		Chain _user;
		CegoAdminHandler* _pAH;
	    };

	    class Delete : public Screen::Confirm {		
	    public:

		Delete(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Delete();
		void setup(const Chain& role);
		Screen::Panel* onConfirm();		
	       	      
	    private:

		Chain _user;
		CegoAdminHandler *_pAH;
	    };

	    bool _isTrace;
	    Chain _user;
	    SetT<Chain> _userRoleSet;
	    Delete* _pDelete;
	    Assign* _pAssign;
	    Passwd* _pPwd;
	    CegoAdminHandler* _pAH;
	};
	
	CegoAdminHandler* _pAH;
	Add* _pAdd;
	Action* _pAction;
	Screen::Message* _pHelp;
	
	ListT< ListT<Chain> > _userTable;
	ListT<Chain> _userSchema;
    };

    

    /////////////////
    // Role Panels //
    /////////////////

    class Role : public Screen::Grid {
    public:

	Role(CegoAdminHandler* pAH, Screen::Panel* pParent);
	~Role();
	Screen::Panel* onSelect(unsigned selectedRow);
	Screen::Panel* onKey(unsigned c, unsigned rowNum);
	ListT< ListT<Chain> > getData();
	void refresh();

    private :

	class Add : public Screen::Form {
	public:
	    
	    Add(CegoAdminHandler* pAH, Screen::Panel* pParent);
	    ~Add();
	    Screen::Panel* onConfirm(const ListT<Chain>& valList);	
	    Screen::Panel* onCancel();
	    
	private:

	    CegoAdminHandler* _pAH;	    
	};
		
	class Action : public Screen::Select {
	public:
	    
	    Action(CegoAdminHandler* pAH, Screen::Panel* pParent);
	    ~Action();
	    void setRole(const Chain& role);	    
	    Screen::Panel* onSelect(unsigned selected);
	    
	private:

	    class Perm : public Screen::Grid {
	    public:

	        Perm(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Perm();
		void setup(const Chain& role);
		Screen::Panel* onSelect(unsigned rowNum);
		Screen::Panel* onKey(unsigned c, unsigned rowNum);
		ListT< ListT<Chain> > getData();
		void refresh();
		bool doRefresh() { return true; };
		
	    private:

		class Edit : public Screen::Form {		    
		public:
		    Edit(CegoAdminHandler* pAH, Panel* pParent);
		    ~Edit();

		    void clear();
		    void setup(const Chain& role);
		    Screen::Panel* onConfirm(const ListT<Chain>& valList);
		    Screen::Panel* onCancel();
		    
		private:
		    
		    CegoAdminHandler* _pAH;
		    Chain _role;
		};

		Edit* _pEdit;
		ListT< ListT<Chain> >  _permTable;
		Chain _role;
		CegoAdminHandler* _pAH;
		Screen::Message* _pHelp;
	    };

	    class Assign : public Screen::Form {
	    public:	    

		Assign(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Assign();

		void setup(const Chain& role, const ListT<Chain> userList);
		Screen::Panel* onConfirm(const ListT<Chain>& valList);
		
	    private:
		
		Chain _role;
		CegoAdminHandler* _pAH;
	    };

	    class Delete : public Screen::Confirm {
		
	    public:

		Delete(CegoAdminHandler* pAH, Screen::Panel* pParent);
		~Delete();
		void setup(const Chain& role);
		Screen::Panel* onConfirm();		
		Screen::Panel* onCancel();
	       	      
	    private:

		Chain _role;
		CegoAdminHandler *_pAH;

	    };

	    Chain _role;
	    Perm* _pPerm;
	    Delete* _pDelete;
	    Assign* _pAssign;
	    CegoAdminHandler* _pAH;
	};
		
	ListT< ListT<Chain> > _roleTable;
	ListT<Chain> _roleSchema;

	Add* _pAdd;
	Action* _pAction;
	Screen::Message* _pHelp;
	
	CegoAdminHandler* _pAH;
    };


    /////////////////
    // Setup Panel //
    /////////////////

    class Setup : public Screen::Select {
    public:

	Setup(CegoAdmScreen* pScreen, CegoAdminHandler* pAH, Screen::Panel* pParent, const Chain& title);
	~Setup();	
	Screen::Panel* onSelect(unsigned selected);
      
    private:
	
	class LogMng : public Screen::Form {
	public:
	
	    LogMng(CegoAdminHandler* pAH, Screen::Panel* pParent, const Chain& title);
	    Screen::Panel* onConfirm(const ListT<Chain>& valList);
	    void setup();
	private:
	    
	    CegoAdminHandler* _pAH;	    
	};
	
	class BackupMng : public Screen::Form {
	public:
	    
	    BackupMng(CegoAdminHandler* pAH, Screen::Panel* pParent, const Chain& title);
	    Screen::Panel* onConfirm(const ListT<Chain>& valList);
	    void setup();
	    
	private:
	    
	    CegoAdminHandler* _pAH;	    
	};

	class Refresh : public Screen::Select {
	public:
	    
	    Refresh(Screen* pScreen, Screen::Panel* pParent);
	    Screen::Panel* onSelect(unsigned selected);
	    
	private:
	    
	    Screen* _pScreen;
	};
	
	LogMng* _pLogMng;
	BackupMng* _pBackupMng;
	Refresh* _pRefresh;	
    };
    
    AdminMenu* _pMenu;
    BufferPool* _pBufferPool;
    ThreadPool* _pThreadPool;
    TableSet* _pTableSet;
    User* _pUser;
    Role* _pRole;   
    Setup* _pSetup;
    
    CegoAdminHandler* _pAH;
};
#endif














