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

#ifndef AppGlobalFH
#define AppGlobalFH
//---------------------------------------------------------------------------
#include <vcl.h>
#include <assert.h>
#include <stdio.h>
#include <inifiles.hpp>

//---------------------------------------------------------------------------
#include "ITSLogF.h"
#include "ITSCommF.h"
#include "SysGlobalF.h"
#include "TVmsCtlrF.h"
#include "TVmsImageF.h"
#include "TCDSFormF.h"
#include "TCDSFontF.h"
#include "TVmsIfscF.h"
#include "VitzroCommF.h"
#include "VmsPasigProtocolF.h"

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

/*
******************************************************************************
* User Define
******************************************************************************
*/
//---------------------------------------------------------------------------
#define LINFO(args...)      LogWrite(eLOG_INFO,    ##args)
#define LDATA(args...)      LogWrite(eLOG_DATA,    ##args)
#define LERROR(args...)     LogWrite(eLOG_ERROR,   ##args)
#define LWARN(args...)      LogWrite(eLOG_WARNING, ##args)
#define LDEBUG(args...)     LogWrite(eLOG_DEBUG,   ##args)
#define LDETAIL(args...)    LogWrite(eLOG_DETAIL,  ##args)

#define SINFO(args...)      SysLogWrite(eLOG_INFO,    ##args)
#define SDATA(args...)      SysLogWrite(eLOG_DATA,    ##args)
#define SERROR(args...)     SysLogWrite(eLOG_ERROR,   ##args)
#define SWARN(args...)      SysLogWrite(eLOG_WARNING, ##args)
#define SDEBUG(args...)     SysLogWrite(eLOG_DEBUG,   ##args)
#define SDETAIL(args...)    SysLogWrite(eLOG_DETAIL,  ##args)

#define WM_THREAD_TERMINATE         (WM_USER+0x92)      /* Thread 종료 메시지 */
#define WM_PROCESS_STATE            (WM_USER+0xA4)      /* 프로세스 상태 입력 */
//---------------------------------------------------------------------------

#define MAX_SENDER_SIZE     20
typedef struct tagIpcLogMessage
{
    int         Kind;
    int         Flag;
    int         Len;
    TDateTime   Tm;
    char        Type;
    char        Msg[MAX_LOG_BUFFER];
    void       *Obj;
} IPC_LOG_MESSAGE;


typedef struct tagCommConfig
{
    int             nCenterPort;        // center udp port

    int             nListenPort;        // vms listen port
    AnsiString      sLocalDomain;       // vms server domain

    int             nClientPort;        // vms controller connect port
    AnsiString      sLoginUser;         // vms controller login id
    AnsiString      sLoginPswd;         // vms controller login pswd

    int             nConnectWait;
    int             nCmdTimeOut;
    int             nWatchDogTime;
    int             nPollingTime;
    void Clear()
    {
    }
} COMM_CONFIG;

typedef struct tagAppConfig
{
    String          sConfigFile;
    bool            bAppClose;
    bool            bAppLoad;
    bool            bThrExit;

    String          sTitle;
    long            lMainWinHandle;     /* Main Window Handle */

    String          sManagerApp;

    AnsiString      sProcessId;       	/* Process ID */
    int             nProcessId;
    int             nProcessPort;

    AnsiString      sSystemId;          /* System ID */

    AnsiString      sLogDay;
    int             nTrafficCycleTime;
    int             nTrafficBasisTime;
    int             nSaveFormType;
    int             nDefaultFillColor;

    int             nMaxLogLines;

	int  MaxMemory;                     /* Max Memory size Mbyte */
	int  MaxHandle;                     /* Max Handle count */
	int  MaxThread;                     /* Max Thread count */
	int  MaxGdi;                        /* Max Gdi count */
    bool RcRestart;                     /* 리소스 오류시 재기동 여부 */

    bool DownloadBitmap;
    bool SaveFormImage;                 // 폼이미지를 Form 디렉토리에 저장할지 여부

    //THREAD_INFO     thrDb;              /* DB Thread */
    DATABASE_INFO   db;
    COMM_CONFIG     comm;
    int             nCenterCommLogLevel;
public:
    void Clear()
    {
        bThrExit       = false;
        bAppClose      = false;
        bAppLoad       = false;
        sTitle         = "";
        lMainWinHandle = 0;

        sProcessId  = "";
        sLogDay     = "";

        MaxMemory = 0;
        MaxHandle = 0;
        MaxThread = 0;
        MaxGdi    = 0;

        //thrDb.Clear();
        db.Clear();
        comm.Clear();
    }
} APP_CONFIG;

// system state
typedef struct tagSystemState
{
    BYTE Action;      						/* 동작 상태 */
    BYTE Comm;        						/* 통신 상태 */
    BYTE DB;          						/* DB 상태 */

    HANDLE hFinishEvent;                    /* finish event */

	DWORD CommThreadID;     				/* 통신 데이터 처리 스레드 ID */
} SYSTEM_STATE;

// system informations
typedef struct tagSystemInformation
{
	SYSTEM_STATE      State;         /* system state */
} SYSTEM_INFORMATION;


/*
******************************************************************************
* Global variable definitions
******************************************************************************
*/
extern APP_CONFIG       g_AppCfg;
extern LOG_INFO         g_LogCfg;
extern TITSLog         *ITSLog;
extern TITSLog         *FDbLog;
extern bool LOGIN_CHECK;
extern bool LOAD_DB;

extern SYSTEM_INFORMATION *g_SysInfo;

#define USE_Q   0

/*
******************************************************************************
* Global function definitios
******************************************************************************
*/
bool APP_LoadConfigInfo();

#endif
