
//---------------------------------------------------------------------------
#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 "AppGlobalF.h"
//#include "FrmMainCommF.h"
#include "VmsPasigProtocolF.h"


//---------------------------------------------------------------------------

typedef enum eEN_SEND_MSG
{
	SEND_MSG_NONE    = 0x00,     /* 없음 */
	SEND_MSG_PROCESS = 0x01,     /* 프로세스 */
	SEND_MSG_EQUIP   = 0x02      /* 시설물 */
} EN_SEND_MSG;

//---------------------------------------------------------------------------
class TFrmMain : public TForm
{
__published:	// IDE-managed Components
	TImageList *ilMenu;
	TTimer *tmrAppState;
    TImageList *ImageListScope;
    TImageList *ImglstTvMvps;
    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 *pnlVms;
    TSplitter *Splitter1;
    TPanel *Panel16;
    TPanel *plThread;
    TPanel *plMemory;
    TPanel *plGdi;
    TPanel *plHandle;
    TPanel *Panel2;
    TImageList *ImageList1;

    /*
    *  프로그램에 공통적으로 사용되는 폼이벤트
    */
	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);

private:	// User declarations
    int             m_bAction;      /* 동작 상태 */
    int             m_bComm;        /* 통신 상태 */
	int             m_bDB;          /* DB 상태 */
	BYTE            FSendMsg;       /* send message */

    bool            FServerRun;     //서버 기동/정지
    DWORD           FTickState;     //주기체크 틱

	char            m_oTimer[15];   /* ON/OFF Timer */
	char            m_tTimer[15];   /* Traffic Timer */
	TDateTime       m_rTimer;       /* timer for receiver */

    int             FCur5Min;
    int             FCurHour;
    int             FCurMin;
    int             FCurSec;
    bool            FOneMinCycle;

	DWORD   m_CommThreadID;     /* 통신 데이터 처리 스레드 ID */
	unsigned int m_CommHandle;  /* 통신 데이터 처리 스레드 핸들 */

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

    bool __fastcall StartJobThread();
    void __fastcall AutoClose();

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

	int __fastcall SendProcessState(void);
	void __fastcall SendInitial(void);
	void __fastcall SendTerminal(void);

public:
    int __fastcall SendDbThreadMessage(UINT Msg, int wParam, int lParam);
	int __fastcall SendDbThreadDataMessage(UINT Msg, void *pData, int iLen);
	int __fastcall SendVmsThreadMessage(BYTE *AVmsId, IPCUDPMESSAGESTRUCT *ARcvData, BYTE AOpCode, char *AData);

	void __fastcall ClientResponse(int Flag, CLIENT_COMMAND *pReq, int error, void *pData);
	bool __fastcall CheckBasisTime(char *p, int Cycle, int Basis);

	void __fastcall ProvideEvent(int Type, BYTE *pVmsId, BYTE Mode);
	void __fastcall CheckProvideSave();
	/* 101 Begin */
	bool __fastcall CheckResource(size_t MaxMemory, DWORD MaxHandle, DWORD MaxThread, DWORD MaxGdi);
	/* 101 End */

    IPC_LOG_MESSAGE FLogMsg;
    int LogWrite(int ALogKind, char *AFmt, ...);

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

    size_t FMemSize;

    void __fastcall LoadOldVmsInfo();
    void __fastcall LoadFtpVmsInfo();
    void __fastcall LoadFontVmsInfo();
    void __fastcall LoadBitmapVmsInfo();
    TVmsCtlr* __fastcall FindVms(AnsiString AVmsId);

    int __fastcall ChangePosition(TVmsFormObj *AVmsObj);
    int __fastcall MakeVmsDownloadForm();
    int __fastcall MakeVmsDatabaseForm(TVmsCtlr *AObj);

	void __fastcall OnWMUserMessage(TMessage &Msg);
    void __fastcall OnUdpDataRecvMessage(TMessage &Msg);
    void __fastcall OnVmsThreadMessage(TMessage &Msg);

	BEGIN_MESSAGE_MAP
		MESSAGE_HANDLER(WM_USER,           TMessage, OnWMUserMessage)
        MESSAGE_HANDLER(WM_VCOMMDATA_RECV, TMessage, OnUdpDataRecvMessage)
        MESSAGE_HANDLER(WM_VMS_THREAD,     TMessage, OnVmsThreadMessage)
	END_MESSAGE_MAP(TForm)

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