//---------------------------------------------------------------------------
#ifndef FrmMainFH
#define FrmMainFH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Grids.hpp>
#include <ImgList.hpp>
#include <ToolWin.hpp>
#include <psapi.h>
#include <tlhelp32.h>
#include "IdAntiFreeze.hpp"
#include "IdAntiFreezeBase.hpp"
#include "IdBaseComponent.hpp"
#include "IdComponent.hpp"
#include "IdUDPBase.hpp"
#include "IdUDPServer.hpp"
//---------------------------------------------------------------------------
#include "AppGlobalF.h"
#include "CDSCtlrF.h"
#include "DbmThreadF.h"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
class TFrmMain : public TForm
{
__published:	// IDE-managed Components
	TTimer *tmrAppState;
    TToolBar *tobMenu;
    TToolButton *tbConnect;
    TToolButton *tbSeparator1;
    TToolButton *tbDisconnect;
    TToolButton *tbSeparator2;
    TToolButton *tbOptions;
    TToolButton *tbSeparator3;
    TPanel *plBottom;
    TPanel *plStateAction;
    TPanel *plStateComm;
    TPanel *plStateDb;
    TPanel *plTime;
    TPanel *Panel1;
    TPanel *pnlLog;
    TPanel *pnlController;
    TSplitter *Splitter1;
    TPanel *Panel16;
    TPanel *plThread;
    TPanel *plMemory;
    TPanel *plGdi;
    TPanel *plHandle;
    TPanel *Panel2;
    TImageList *ImageList1;
    TIdUDPServer *IdUDPCenterServer;
    TPanel *plStateLog;
    TPanel *plStateDbm;
    TPanel *plStateJob;
    TTimer *tmrProvide;
    TPanel *plRemain;
    TTimer *tmrRunProvide;

	void __fastcall FormCreate(TObject *Sender);
	void __fastcall FormShow(TObject *Sender);
	void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
	void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
	void __fastcall tmrAppStateTimer(TObject *Sender);

	void __fastcall tbConnectClick(TObject *Sender);
	void __fastcall tbDisconnectClick(TObject *Sender);
	void __fastcall tbOptionsClick(TObject *Sender);
    void __fastcall Panel16DblClick(TObject *Sender);
    void __fastcall plStateDbDblClick(TObject *Sender);
    void __fastcall plStateActionDblClick(TObject *Sender);
    void __fastcall plTimeDblClick(TObject *Sender);
    void __fastcall tmrProvideTimer(TObject *Sender);
    void __fastcall plStateCommDblClick(TObject *Sender);
    void __fastcall tmrRunProvideTimer(TObject *Sender);

private:	// User declarations
    bool            FServerRun;     //서버 기동/정지
    DWORD           FTickState;     //주기체크 틱
    DWORD           FProvideRemain;

	char            m_tTimer[15];   /* Traffic Timer */

	char            m_szOnOffTimer[15];   /* ON/OFF Timer */
	char            m_szTraffTimer[15];   /* Traffic Timer */

	TDateTime       m_dtChkDbAive;          // 데이터베이스 연결 체크 타이머
	TDateTime       m_dtChkVmsJob;          // VMS 작업 및 주기작업
	TDateTime       m_dtProvider;           // 정보제공 타이머

    int             FUnitSttsHsMin;
    int             FCtlrSttsHsMin;

    int             FTimeSyncDay;
    int             FTimeSyncMin;

    int             FCur1MinComm;
    int             FCur1MinRun;
    int             FCur5Min;
    bool            FOneMinCycle;

    int             FCurrDay;
    int             FCurrHour;
    int             FCurrMin;
    int             FCurrSec;
    int             FCurrTimeSec;
    bool            FFirstRunning;

    void __fastcall OnAppException(TObject *Sender, Exception *exception);

	bool __fastcall InitApplication();
	void __fastcall CommClose();

    void __fastcall AbnormalTerminate(char* AMsg);
    bool __fastcall JobThreadStart();
    void __fastcall JobThreadCheck();
    void __fastcall JobThreadStop();

    void __fastcall AutoClose();

	void __fastcall TerminateApplication();
	void __fastcall UpdateProcessStatePanel();
	void __fastcall CheckCommState();

    void __fastcall SendVmsTrafficProvide(int AType);
	void __fastcall SendTerminal();

	int __fastcall SendVmsThreadMessage(BYTE *AVmsId, IPC_MSG_DATA *ARcvData, BYTE AOpCode, char *AData);

	bool __fastcall CheckBasisTime(char *p, int Cycle, int Basis);

    void __fastcall SendTimeSync();
	void __fastcall CheckProvideTimeout();
	bool __fastcall CheckResource(size_t MaxMemory, DWORD MaxHandle, DWORD MaxThread, DWORD MaxGdi);

public:		// User declarations
	__fastcall TFrmMain(TComponent* Owner);

    size_t FMemSize;

	void __fastcall OnWMUserMessage(TMessage &Msg);
	void __fastcall OnWMDbThreadMessage(TMessage &Msg);
    void __fastcall OnUdpDataRecvMessage(TMessage &Msg);
    void __fastcall OnTcpThreadMessage(TMessage &Msg);

	BEGIN_MESSAGE_MAP
		MESSAGE_HANDLER(WM_USER,                 TMessage, OnWMUserMessage)
		MESSAGE_HANDLER(WM_DB_THREAD,            TMessage, OnWMDbThreadMessage)
        MESSAGE_HANDLER(WM_CENTERCOMM_DATA_RECV, TMessage, OnUdpDataRecvMessage)
        MESSAGE_HANDLER(WM_TCP_THREAD,           TMessage, OnTcpThreadMessage)
	END_MESSAGE_MAP(TForm)

};
//---------------------------------------------------------------------------
extern PACKAGE TFrmMain *FrmMain;
//---------------------------------------------------------------------------
#endif
