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

#ifndef SysGlobalFH
#define SysGlobalFH
//---------------------------------------------------------------------------
#include <vcl.h>
#include <assert.h>
#include <stdio.h>
#include <windef.h>
//---------------------------------------------------------------------------

/*
******************************************************************************
* Define
******************************************************************************
*/
#define     SAFE_DELETE(p)          {if (p != NULL) { delete p; p = NULL; }}

/*
******************************************************************************
* Define constant
******************************************************************************
*/
#define     MAINHANDLE                  Application->MainForm->Handle

// SendMessageTimeout
#define     SEND_LOGMSG(a,b,c,d,e,f,g)  ::SendMessageTimeout(a, b, (WPARAM)c, (LPARAM)d, e, f, &g)
//#define     SEND_LOGMSG(a,b,c,d,e,f,g)  ::PostMessage(a, b, (WPARAM)c, (LPARAM)d)

#define     SEND_MSG(x,y,z,p)           ::SendMessage(x, y,       (WPARAM)z, (LPARAM)p)
#define     POST_MSG(x,y,z,p)           ::PostMessage(x, y,       (WPARAM)z, (LPARAM)p)
#define     USER_MSG(x,y,z)             ::PostMessage(x, WM_USER, (WPARAM)y, (LPARAM)p)

#define     WM_WINDOW_RESTORE           (WM_USER+0xA0)      /* 중복실행시 기존 윈도우 활성화 */

#define     WM_THREAD                   (WM_USER+0xF1)      /* 스레드 연계 메시지 */
#define     WM_TCP_CLIENT               (WM_USER+0xF2)      /* TCP Client */
#define     WM_TCP_SERVER               (WM_USER+0xF3)      /* TCP Client */
#define     WM_VMS_THREAD               (WM_USER+0xF4)

#define     WM_VMS_SERVER_STTS          (WM_USER+0xB2)      // VMS 접속/해제(통신스레드->메인)
#define     WM_VMS_CLIENT_STTS          (WM_USER+0xB3)      // VMS 접속/해제(통신스레드->메인)
#define     WM_VMS_STATE_STTS           (WM_USER+0xB4)      // VMS 상태정보 업데이트

// 스레드 종료 대기 시간
#define     WAIT_THREAD_TERMINATE_TIME  5000    // Wait Thread Terminate Time(5 seconds)

/*
******************************************************************************
* 스레드로 전달하는 메시지
******************************************************************************
*/
#define     WM_EQUIP_STATE_ALL          (WM_USER+0xB2)      /* 시설물상태 개별 입력 */
#define     WM_EQUIP_STATE              (WM_USER+0xC2)      /* 시설물상태 전체 입력 */

#define     WM_DATABASE_CLOSE           (WM_USER+0xA0)      /* DATABASE CLOSE */
#define     WM_DATABASE_REFLASH         (WM_USER+0xA1)      /* DATABASE REFLASH */
#define     WM_DATABASE_OPEN            (WM_USER+0xA2)      /* DATABASE OPEN */
#define     WM_LOG_DISPLAY              (WM_USER+0xA3)      /* LOG DISPLAY */

#define     WM_MASTER_FORM              (WM_USER+0xD1)      /* 폼정보 변경 */
#define     WM_MASTER_IMAGE_SYMBOL      (WM_USER+0xD2)      /* 폼이미지정보 변경 */
#define     WM_MASTER_IMAGE_TRAFFIC     (WM_USER+0xD3)      /* 폼이미지소통정보 변경 */
#define     WM_MASTER_VMS_IFSC          (WM_USER+0xD4)      /* VMS IFSC 정보 변경 */

#define     WM_DATA_SAVE                (WM_USER+0xA8)      /* 데이터 저장 */
#define     WM_GET_ONOFF                (WM_USER+0xA9)      /* VMS ON/OFF */
#define     WM_PROVIDE_PROC             (WM_USER+0xAA)      /* VMS 교통정보 */
#define     WM_PROVIDE_SAVE             (WM_USER+0xAB)      /* VMS 제공정보 저장 */
#define     WM_DOWNLOAD_PROC            (WM_USER+0xAC)      /* VMS 다운로드 */

#define     WM_MAKE_DOWNLOAD_FORM       (WM_USER+0xAD)      /* VMS 다운로드폼 생성 */
#define     WM_SAVE_DOWNLOAD_FORM       (WM_USER+0xAE)      /* VMS 다운로드폼 저장 */

/*
******************************************************************************
* User Define
******************************************************************************
*/
typedef enum eLOG_TYPE
{
    LLOG     = 3,
    LJOB     = 5,
    LWARNING = 6,
    LERROR   = 7,
    LALARM   = 8,
} LOG_TYPE;
//---------------------------------------------------------------------------

typedef struct tagThreadInfo
{
    TThread     *pThread;
    DWORD        dwThreadId;
    unsigned int nHandle;
    bool         bRunning;
public:
    void Clear()
    {
        pThread    = NULL;
        dwThreadId = 0;
        nHandle    = 0;
        bRunning   = false;
    }
} THREAD_INFO;
//---------------------------------------------------------------------------

typedef struct tagDatabaseInfo
{
    String  sProvider;
    String  sServerName;
    String  sCatalog;
    String  sUserName;
    String  sPassword;
    String  sConnectStr;
public:
    void Clear()
    {
        sProvider   = "";
        sServerName = "";
        sCatalog    = "";
        sUserName   = "";
        sPassword   = "";
        sConnectStr = "";
    }
    void MakeConnString()
    {
        sConnectStr  = "";
        sConnectStr += "Provider="         + sProvider;
        sConnectStr += ";Password="        + sPassword;
        sConnectStr += ";Persist Security Info=True";
        sConnectStr += ";User ID="         + sUserName;
        if (sCatalog != "")
        {
            sConnectStr += ";Initial Catalog=" + sCatalog;
        }
        sConnectStr += ";Data Source="     + sServerName;
    }
} DATABASE_INFO;
//---------------------------------------------------------------------------

/*
******************************************************************************
* Global variable definitions
******************************************************************************
*/
extern String           g_sAppDir ;       // Application Directory
extern String           g_sAppName;       // Program name
extern String           g_sCfgDir ;       // Program Config Directory
extern String           g_sLogDir ;       // Program Log Directory
extern String           g_sTempDir;       // Program Temp Directory
extern String           g_sImgDir;        // Program Image Directory
extern String           g_sFormDir;       // Program Form Directory
extern String           g_sFtpDir ;       // Application Directory

/*
******************************************************************************
* Global function definitios
******************************************************************************
*/
bool SYS_ApplicationSingleInstance(AnsiString AProgName, AnsiString APidFile);
void SYS_WritePidFile(long AHandle);

bool SYS_WriteConfigInfo(String sTitle, String sItem, String sValue, String sCfgFile="");
bool ReadConfigInfo(String sTitle, String sItem, String &sValue, String sCfgFile="");

String SYS_GetSysError();


#endif
