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

#ifndef CDSVmsCtlrFH
#define CDSVmsCtlrFH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <stdio.h>
#include <ADODB.hpp>
#include <DB.hpp>
#include "LibSTLF.hpp"
//---------------------------------------------------------------------------
#include "VitzroCommF.h"
#include "ITSLogF.h"
#include "TVmsFormF.h"
//---------------------------------------------------------------------------

typedef enum en_object_state
{
    object_disable = 0,
    object_enable  = 1,
} EN_OBJECT_STATE;

typedef enum en_bit_state
{
    bit_disable = 0,
    bit_enable  = 1,
} EN_BIT_STATE;

typedef enum en_state
{
    state_close = 0,
    state_open  = 1,

    state_stop  = 0,
    state_run   = 1,

    state_normal = 0,
    state_error  = 1,
    state_unknown= 2,
} EN_STATE;

typedef enum en_process_state
{
    process_fail = 0,
    process_run,
    process_stop,
} EN_PROCESS_STATE;

//---------------------------------------------------------------------------
// 프로세스 정보
//---------------------------------------------------------------------------
class PROCESS_INFO
{
public:
    PROCESS_INFO()
    {
        UseFlag         = bit_disable;
        Status          = process_fail;
        Comm            = state_close;
        pThread         = NULL;
        ActiveIndicator = false;
        wTimer          = Now() - 1;
    };
    ~PROCESS_INFO()
    {
    }

public:
    BYTE      UseFlag;
    BYTE      Status;                   /* 프로세스 상태, 0:비정상 1:정상 2:정지 */
    BYTE      Comm;                     /* 통신상태, 0:close, 1:open */
	void     *pThread;                  /* 스레드 포인터 */
    bool      ActiveIndicator;          /* 스레드 실행 상태 */
    TDateTime wTimer;                   /* timer for connect wait time check */
};
//---------------------------------------------------------------------------

// VMS 제어모드
typedef struct vms_form_download
{
	BYTE        Control;                /* VMS 제어모드 */
	AnsiString  ManSceId;               /* 수동시나리오ID */
	AnsiString  EmeSceId;               /* 긴급시나리오ID */
	AnsiString  CurSceId;               /* 현재시나리오ID */

	bool        Enable;                 /* 활성화 여부 */
	int         MaxDisplayForm;         /* 최대 표출폼 갯수 */
	AnsiString  SvcDate;                /* 제공시각 */
	int         SaveFlag;               /* 제공정보 저장 여부 */
	TDateTime   pTimer;                 /* timer for provide */
    bool        Result;
} VMS_CONTROL_MODE;

// VMS 다운로드
#define VMS_MAX_DOWNLOAD                8       /* 최대 VMS 다운로드 */
typedef struct VmsDownLoadInfo
{
	bool        Enable;             	/* 활성화 여부 */
	int         SaveFlag;               /* 다운로드정보 저장 여부 */
	TDateTime   dTimer;                 /* timer for download */
	int         Count;                  /* 정보 갯수 */
	struct
    {
		AnsiString  RegDate;           	/* 등록일시 */
		AnsiString  FileType;          	/* 다운로드파일유형 */
		AnsiString  FileId;            	/* 다운로드파일ID */
		AnsiString  SendStartDate;     	/* 전송시작시각 */
		AnsiString  SendEndDate;       	/* 전송완료시각 */
		int         SendStep;           /* 전송진행단계 */
		int         SendResult;        	/* 전송결과 */
	} Unit[VMS_MAX_DOWNLOAD];
} VMS_DOWNLOAD_INFO;

typedef enum en_vms_downalod
{
    vms_download_disable = 0,
    vms_download_enable  = 1,
} EN_VMS_DOWNLOAD;

typedef enum en_vms_download_step
{
	send_wait,			/* 0:대기 */
	send_progress,		/* 1:전송중 */
	send_complete,      /* 2:전송완료 */
} EN_VMS_DOWNLOAD_STEP;

// 전송결과
typedef enum en_send_result
{
	send_result_fail,	/* 0:실패 */
	send_result_succ	/* 1:성공 */
} EN_SEND_RESULT;

//---------------------------------------------------------------------------
#define VMS_MAX_EVENT       16
typedef struct vms_event_detl
{
    AnsiString LINK_ID;
    AnsiString IFSC_ID;
    AnsiString VMS_IFSC_ID;
    AnsiString VMS_IFSC_NM;
    AnsiString DSPL_STRT_NODE_NM;
    AnsiString DSPL_END_NODE_NM;
    AnsiString DETR_ID;
    AnsiString DSPL_PRRT;
    AnsiString INCD_OCRR_ID;
    AnsiString STRT_LCTN_NM;
    AnsiString END_LCTN_NM;
    AnsiString OCRR_LCTN_NM;
    AnsiString VMS_INCD_TYPE_NM;
    AnsiString VMS_INCD_TYPE_CD;
    AnsiString VMS_INCD_DETL_NM;
    AnsiString VMS_INCD_DETL_TYPE_CD;
    AnsiString CMTR_GRAD_NM;
    AnsiString CMTR_GRAD_CD;
    AnsiString INCD_CLSR_LANE;
    AnsiString INCD_STRT_DT;
    AnsiString INCD_END_PRAR_DT;
    AnsiString ROAD_NM;
    AnsiString SPOT_NM;
} VMS_EVENT_DETL;

typedef struct vms_event_info
{
    int            Count;
    VMS_EVENT_DETL Unit[VMS_MAX_EVENT];
} VMS_EVENT_INFO;
//---------------------------------------------------------------------------

#define VMS_MAX_SCHEDULE    16
typedef struct vms_schedule_detl
{
    int        TRFC_SITU_TYPE_CD;   //VARCHAR2(7)	N			스케줄 유형 코드(0:교통정보,1:돌발,2:공사/행사문안,3:홍보문안,4:우회도로,5:고정스케줄)
    int        PHASE;               //NUMBER(2)	    N			PHASE
    AnsiString VMS_FORM_ID;         //NUMBER(5)	    Y			VMS FORM ID
    AnsiString FRST_VMS_IFSC_ID;    //NUMBER(10)	Y			1단 VMS 정보제공구간 ID
    AnsiString SECD_VMS_IFSC_ID;    //NUMBER(10)	Y			2단 VMS 정보제공구간 ID
    AnsiString THIR_VMS_IFSC_ID;    //NUMBER(10)	Y			3단 VMS 정보제공구간 ID
    AnsiString FOUR_VMS_IFSC_ID;    //NUMBER(10)	Y			4단 VMS 정보제공구간 ID
    AnsiString FRST_IMG_IFSC_ID;    //NUMBER(10)	Y			1단 이미지 정보제공구간 ID
    AnsiString SECD_IMG_IFSC_ID;    //NUMBER(10)	Y			2단 이미지 정보제공구간 ID
    AnsiString THIR_IMG_IFSC_ID;    //NUMBER(10)	Y			3단 이미지 정보제공구간 ID
    AnsiString FOUR_IMG_IFSC_ID;    //NUMBER(10)	Y			4단 이미지 정보제공구간 ID
    int        DSPL_HH;             //NUMBER(3)	    Y	0		표출 시간
    AnsiString VMS_FORM_PRRT_CD;    //VARCHAR2(3)	Y			VMS FORM 우선순위 코드

    AnsiString VMS_IFSC_ID;         //도형식 배경 하단 소통정보 제공구간 ID

    VMS_EVENT_DETL *EVENT;
} VMS_SCHEDULE_DETL;

typedef struct vms_schedule_info
{
    int               Count;
    VMS_SCHEDULE_DETL Unit[VMS_MAX_SCHEDULE];
} VMS_SCHEDULE_INFO;
//---------------------------------------------------------------------------

class TVmsIfsc
{
public:
    TVmsIfsc() {};
    ~TVmsIfsc(){};
public:
    AnsiString VMS_IFSC_ID;         //NUMBER(10)	N			VMS 정보제공구간 ID
    int        DSPL_PRRT;           //NUMBER(2)	    Y			표출 우선순위
};
//---------------------------------------------------------------------------

class TVmsCtlr
{
public:
    TVmsCtlr();
    ~TVmsCtlr();

private:
    CRITICAL_SECTION    FCS;

public:
    PROCESS_INFO    Server;
    PROCESS_INFO    Client;

    AnsiString      ChangeDate; /* 변경시각 */
    TThreadList    *pPackQueue; /* 패킷 버퍼 */

public:
    int         RowNo;
    int         Id;
    AnsiString  VmsNo;          /* VMS NO */
    AnsiString  VmsId;          /* VMS ID */
    AnsiString  VmsType;        /* VMS종류 */
    AnsiString  Name;           /* 명칭 */
    AnsiString  Location;       /* 설치위치 */
    int         LocalNo;        /* Local번호 */
    AnsiString  IpAddress;      /* IP Address */
    AnsiString  WlTelNo;        /* 무선통신연결번호 */
    AnsiString  WlIp;           /* 무선통신 IP Address */
    int         WlPort;         /* 무선통신 Port */

    AnsiString  OPER_MODE;      /* 운영 모드 A:Auto, F:Fix */

    AnsiString  ConnectTm;
    AnsiString  DisconnectTm;

    struct
    {
        AnsiString  PowerOnTime;    /* ON TIME */
	    AnsiString  PowerOffTime;   /* OFF TIME */
        int         Retry;
    } MODULE;

    struct
    {
        AnsiString  SchTime;
        int         Value;
        int         Retry;
    } LUMINANCE;

    int         VMS_PHSE_CHNG_CYCL;     // NUMBER(3)    Y            VMS 표출면 변경 주기
    int         VMS_CMNC_ERR_BASS_VAL;  // NUMBER(3)    Y            VMS 통신 오류 기본 값
    int         FAN_MTNS_TMPR;          // NUMBER(6,3)    Y            팬 동작 온도
    int         HETR_MTNS_TMPR;         // NUMBER(6,3)    Y            히터 동작 온도
    int         VMS_MODL_ERR_RATE;      // NUMBER(3)    Y            VMS 모듈 오류 율
    int         VMS_MODL_CHK_CYCL;      // NUMBER(3)	Y	30		모듈체크주기(초)

    int         VMS_NGHT_BRGH_STEP;     // NUMBER(2)    Y            VMS 야간 휘도 단계
    int         VMS_WEEK_BRGH_STEP;     // NUMBER(2)    Y            VMS 주간 휘도 단계
    int         CMNCFAIL_SLOT_NMBR;     // NUMBER(4)    Y    0        통신장애 SLOT 번호
    int         PWER_FAIL_SLOT_NMBR;    // NUMBER(4)    Y    0        전원 장애 SLOT 번호

    int         VMS_MAX_PHSE_NUM;       // NUMBER(2)    Y            VMS 최대 표출면 개수

    int     WIDTH;
    int     HEIGHT;
    int     ModHorCnt;
    int     ModVerCnt;

    IntMap<TVmsIfsc> FIfscLists;

    VMS_SCHEDULE_INFO  *SCHEDULE;
    VMS_EVENT_INFO     *INCIDENT;
    VMS_EVENT_INFO     *EVENT;

    VMS_DOWNLOAD_INFO  *DOWNLOAD;
    VMS_CONTROL_MODE   *CTLMODE;

    TVmsFormManager    *pForms;

    bool                FSaveState;
    BYTE                FOldState;
    INT_VMS_STATE       DSTATE;
    INT_VMS_STATE       RSTATE;

    bool                FProvide;
    bool                FModeChange;

    bool                FIsOldProtocol;
    bool                FIsFtpDownload;
    bool                FIsProtolFontName;
    bool                FIsStatusAuto;
    AnsiString          VmsDownId;
    bool                FBmpDownload;

    bool                FSymbolDownload;

public:
    bool         FDispLog;
    TITSLog     *FSLog;
    TITSLog     *FCLog;

    void Lock();
    void UnLock();

};
//---------------------------------------------------------------------------

class TVmsCtlrManager
{
public:
    TVmsCtlrManager();
    ~TVmsCtlrManager();

private:

public:
    unsigned short    Total;
    unsigned short    Error;
    unsigned short    Normal;
    unsigned short    Module;

    ListMap<TVmsCtlr> FLists;

public:
    int GetCount() { return FLists.Size(); }
    void InitProvide(bool AProvide=false);

    __property int Count = { read = GetCount };
};
//---------------------------------------------------------------------------
extern TVmsCtlrManager *VmsManager;
//---------------------------------------------------------------------------

typedef std::map<AnsiString, TVmsCtlr*>::iterator   VmsIterator;
#ifndef VMS_STL
#define VMS_STL(_class, _pointer)		for(VmsIterator it=_list.begin(); it!=_list.end(); ++it,  _pointer=(TVmsCtrl*)it->second)
#if 0
    VmsIterator it;
    for(it=VmsManager->FLists.FMapObject.begin(); it != VmsManager->FLists.FMapObject.end(); ++it)
    {
        TVmsCtlr *pObj = (TVmsCtlr*)it->second;
        pInfo = pObj->DOWNLOAD;
        InitDownLoadInfo(pInfo);
    }

#endif
#endif

bool ChangedState(INT_VMS_STATE *DSTATE, INT_VMS_STATE *RSTATE);
void InitDownLoadInfo(VMS_DOWNLOAD_INFO *AInfo);
void InitCtlModeInfo(VMS_CONTROL_MODE *AInfo);
void EventInit(VMS_EVENT_INFO *AInfo);
void ScheduleInit(VMS_SCHEDULE_INFO *AInfo);

#endif

