/****************************************************************************
 * @source      : 
 * @description : 
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2011/12/24   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "WindowMsgF.h"
#include "CDSFacilityF.h"
#include "CDSIncidentF.h"
#include "FrmMainF.h"
#pragma hdrstop

#include "ITS00G0MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_FacilityStatusListF"
#pragma link "FRAME_ProcessStatusListF"
#pragma link "FRAME_UtisStatusListF"
#pragma link "FRAME_IncidentListF"
#pragma link "FRAME_RepeatCongestListF"
#pragma link "FRAME_TrafficMissLinkListF"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSplitter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma resource "*.dfm"
TITS00G0M *ITS00G0M = NULL;

//---------------------------------------------------------------------------
__fastcall TITS00G0M::TITS00G0M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    LoadLocalSkin();
    CMM_LoadForm(g_sFormsDir, this);

    PgSystem->Properties->ActivePage = TsIncident;
    GrpSystem->Caption = "▶ 시스템 운영 정보 - " + TsIncident->Caption;

    FRAMEFacilityStatusList1->PnlBottom->Visible = false;

    //FrmMain->dxStatusBar->Panels->Items[isbGisPos]->Visible = true;
    ITS00GEO = new TITS00GEO(PnlMapBack, (TForm*)this);
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TITS00G0M::CommClose()
{
    try
    {
        //FrmMain->dxStatusBar->Panels->Items[isbGisPos]->Visible = false;
        if (ITS00GEO)
        {
            delete ITS00GEO;
            ITS00GEO = NULL;
        }

        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * Form이 메모리에서 생성될때 호출되는 생성자 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TITS00G0M::FormCreate(TObject *Sender)
{
    if (ITS00GEO)
    {
        ITS00GEO->Parent = PnlMapBack;
        ITS00GEO->Show();
    }
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TITS00G0M::FormShow(TObject *Sender)
{
    Refresh();
    Application->ProcessMessages();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * Form의 Skin 초기화 작업을 수행한다.
 *  arguments
 *      void
 *  return
 *      void
 */
void __fastcall TITS00G0M::LoadLocalSkin()
{
#define     MAX_VIEW        7

    TcxGridTableView *pView[MAX_VIEW];
    TcxGrid *pGrid[MAX_VIEW];

    pGrid[ 0] = FRAMEProcessStatusList1->CxList;    //프로세스상태
    pGrid[ 1] = FRAMEFacilityStatusList1->CxList;   //시설물상태
    pGrid[ 2] = FRAMEUtisStatusList1->CxUtisRun;    //UTIS현황-운영현황
    pGrid[ 3] = FRAMEUtisStatusList1->CxUtisRse;    //UTIS현황-RSE상태현황
    pGrid[ 4] = FRAMEIncidentList1->CxList;         //돌발
    pGrid[ 5] = FRAMERepeatCongestList1->CxList;    //반복정체구간
    pGrid[ 6] = FRAMETrafficMissLinkList1->CxList;  //소통정보결측구간

    pView[ 0] = FRAMEProcessStatusList1->TvList;
    pView[ 1] = FRAMEFacilityStatusList1->TvList;
    pView[ 2] = FRAMEUtisStatusList1->TvUtisRun;
    pView[ 3] = FRAMEUtisStatusList1->TvUtisRse;
    pView[ 4] = FRAMEIncidentList1->TvList;
    pView[ 5] = FRAMERepeatCongestList1->TvList;
    pView[ 6] = FRAMETrafficMissLinkList1->TvList;

    for (int ii = 0; ii < MAX_VIEW; ii++)
    {
        //pGrid[ii]->LookAndFeel->SkinName = "McSkin";
        continue;
        pView[ii]->Styles->Inactive   = NULL;
        pView[ii]->Styles->ContentOdd = NULL;
        pView[ii]->Styles->ContentEven= NULL;
        pView[ii]->Styles->Selection  = NULL;
    }
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TITS00G0M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    // 리스트 항목을 초기화한다.
    InitListData();
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::InitListData()
{
    FIncidentCnt  = 0;
    FRrCongestCnt = 0;
    FMissLinkCnt  = 0;

    FRAMEFacilityStatusList1->UpdateList();
    FRAMEProcessStatusList1->UpdateList();
    FRAMEUtisStatusList1->UpdateList();

    FRAMEIncidentList1->UpdateList();
    FRAMERepeatCongestList1->UpdateList();
    FRAMETrafficMissLinkList1->UpdateList();

    UpdateAlarmStatus();
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::UpdateAlarmStatus()
{
    String sAlarmMsg = "";

    FIncidentCnt  = FRAMEIncidentList1->TvList->DataController->RecordCount;
    FRrCongestCnt = FRAMERepeatCongestList1->TvList->DataController->RecordCount;
    FMissLinkCnt  = FRAMETrafficMissLinkList1->TvList->DataController->RecordCount;

    if (FIncidentCnt > 0)
    {
        sAlarmMsg += "돌발발생(" + String(FIncidentCnt) + ")  ";
        TsIncident->ImageIndex = 1;
    }
    else
        TsIncident->ImageIndex = 2;

    if (FRrCongestCnt > 0)
    {
        sAlarmMsg += "반복정체구간발생(" + String(FRrCongestCnt) + ")  ";
        TsRrCongest->ImageIndex = 1;
    }
    else
        TsRrCongest->ImageIndex = 2;

    if (FMissLinkCnt > 0)
    {
        sAlarmMsg += "결측구간발생(" + String(FMissLinkCnt) + ")  ";
        TsMissingLink->ImageIndex = 1;
    }
    else
        TsMissingLink->ImageIndex = 2;

    LblAlarm->Caption = sAlarmMsg;
    if ((FIncidentCnt+FRrCongestCnt+FMissLinkCnt) > 0)
    {
        LblAlarm->Visible = true;
    }
    else
    {
        LblAlarm->Visible = false;
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::PgSystemPageChanging(TObject *Sender, TcxTabSheet *NewPage, bool &AllowChange)
{
    GrpSystem->Caption = "▶ 시스템 운영 정보 - " + NewPage->Caption;
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        CommClose();    

        ITS00G0M = NULL;
        Action = caFree;
        g_AppCfg.bAppClose = true;
        //FrmMain->Close();
        //Application->Terminate();
    } catch(...) {}
}
//---------------------------------------------------------------------------

/*
 * 메인폼에서 데이터를 수신시 발생하는 이벤트 핸들러
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TITS00G0M::OnMainFormMessage(TMessage &Msg)
{
    switch(Msg.WParam)
    {
    case WP_DB_SELECT_OK:
        switch(Msg.LParam)
        {
        case LP_MSG_01: ITSUtil_Trace("LP_MSG_01"); UpdateTrafficMissLinkInfo();  break;
        case LP_MSG_02: ITSUtil_Trace("LP_MSG_02"); /*서비스링크소통정보*/        break;
        case LP_MSG_03: ITSUtil_Trace("LP_MSG_03"); /*도로소통정보*/              break;
        case LP_MSG_04: ITSUtil_Trace("LP_MSG_04"); UpdateTrafficRrCongestInfo(); break;
        case LP_MSG_05: ITSUtil_Trace("LP_MSG_05"); UpdateUtisStatusInfo();       break;
        case LP_MSG_11: ITSUtil_Trace("LP_MSG_11"); UpdateFacilityStatusInfo();   break;
        case LP_MSG_12: ITSUtil_Trace("LP_MSG_12"); UpdateProcessStatusInfo();    break;
        case LP_MSG_13: ITSUtil_Trace("LP_MSG_13"); UpdateIncidentInfo();         break;
        case LP_MSG_14: ITSUtil_Trace("LP_MSG_14"); UpdateFacilityInfo();         break;
        }
        break;
    case WP_MSG_01: //소통등급이 변경되었다.
        if (ITS00GEO) ITS00GEO->SetGisTrafficColor();
        break;
    case WP_MSG_02: //돌발정보를 현단말에서 취소/등록/해제 하였다.
        UpdateIncidentInfo();
        break;
    case WP_MSG_03: //시설물 기본정보가 변경됨
        FRAMEFacilityStatusList1->UpdateList();
        break;
    case WP_DB_SELECT_ERROR:
    case WP_DB_SELECT_CANCEL:
        break;
    }
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateTrafficMissLinkInfo()
{
    FMissLinkCnt  = 0;
    FRAMETrafficMissLinkList1->UpdateList();
    UpdateAlarmStatus();
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateTrafficRrCongestInfo()
{
    FRrCongestCnt = 0;
    FRAMERepeatCongestList1->UpdateList();
    UpdateAlarmStatus();
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateIncidentInfo()
{
    FIncidentCnt  = 0;
    FRAMEIncidentList1->UpdateList();
    UpdateAlarmStatus();
    if (ITS00GEO) ITS00GEO->UpdateIncidentStatus();
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateFacilityStatusInfo()
{
    FRAMEFacilityStatusList1->RefreshList();
    if (ITS00GEO) ITS00GEO->UpdateFacilityStatus();
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateProcessStatusInfo()
{
    FRAMEProcessStatusList1->RefreshList();
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateUtisStatusInfo()
{
    FRAMEUtisStatusList1->RefreshList();
}
//---------------------------------------------------------------------------
void __fastcall TITS00G0M::UpdateFacilityInfo()
{
    FRAMEFacilityStatusList1->UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::FRAMEIncidentList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    //돌발 클릭
    if (!ACellViewInfo) return;
    try {
        TcxDataController *pGDC = FRAMEIncidentList1->TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        String sId = VarToStr(pGDC->Values[nRow][FRAMEIncidentList1->Column01->Index]);
        String sDx = VarToStr(pGDC->Values[nRow][FRAMEIncidentList1->Column07->Index]);
        String sDy = VarToStr(pGDC->Values[nRow][FRAMEIncidentList1->Column08->Index]);
        if (ITS00GEO) ITS00GEO->SelectObjectByPos(StrToFloat(sDx), StrToFloat(sDy), true);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::FRAMERepeatCongestList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    //반복정체구간 클릭
    if (!ACellViewInfo) return;
    try {
        TcxDataController *pGDC = FRAMERepeatCongestList1->TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        String sSvcLinkId = VarToStr(pGDC->Values[nRow][FRAMERepeatCongestList1->Column01->Index]);
        if (ITS00GEO) ITS00GEO->SelectLinkById(2, sSvcLinkId, true);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::FRAMETrafficMissLinkList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    //소통정보 결측구간 클릭
    if (!ACellViewInfo) return;
    try {
        TcxDataController *pGDC = FRAMETrafficMissLinkList1->TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        String sLinkId = VarToStr(pGDC->Values[nRow][FRAMETrafficMissLinkList1->Column02->Index]);
        if (ITS00GEO) ITS00GEO->SelectLinkById(1, sLinkId, true);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::FRAMEFacilityStatusList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    //시설물 위치이동
    if (!ACellViewInfo) return;
    try {
        TcxDataController *pGDC = FRAMEFacilityStatusList1->TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        String sType = VarToStr(pGDC->Values[nRow][FRAMEFacilityStatusList1->Column01->Index]);
        FSelId       = VarToStr(pGDC->Values[nRow][FRAMEFacilityStatusList1->Column02->Index]);
        MoveGisFacility(sType, FSelId);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::MoveGisFacility(String AType, String AId)
{
    if (AType == "CCTV")
    {
        TItsCctv *pObj = ItsFacilityManager->FCctvLists.Find(AId);
        if (pObj) if (ITS00GEO) ITS00GEO->SelectObjectByPos(pObj->X_CRDN.ToDouble(), pObj->Y_CRDN.ToDouble(), true);
    }
    else
    if (AType == "VMS")
    {
        TItsVms *pObj = ItsFacilityManager->FVmsLists.Find(AId);
        if (pObj) if (ITS00GEO) ITS00GEO->SelectObjectByPos(pObj->X_CRDN.ToDouble(), pObj->Y_CRDN.ToDouble(), true);
    }
    else
    if (AType == "RSE")
    {
        TItsRse *pObj = ItsFacilityManager->FRseLists.Find(AId);
        if (pObj) if (ITS00GEO) ITS00GEO->SelectObjectByPos(pObj->COORDX.ToDouble(), pObj->COORDY.ToDouble(), true);
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::FRAMEFacilityStatusList1TvListCellClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (AButton != mbRight) return;
    if (!ACellViewInfo) return;

    try {
        TcxDataController *pGDC = FRAMEFacilityStatusList1->TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        String sType = VarToStr(pGDC->Values[nRow][FRAMEFacilityStatusList1->Column01->Index]);
        FSelId       = VarToStr(pGDC->Values[nRow][FRAMEFacilityStatusList1->Column02->Index]);

        POINT p;
        GetCursorPos(&p);

        if (sType == "CCTV") pmuCctv->Popup(p.x, p.y);
        else
        if (sType == "VMS")  pmuVms->Popup(p.x, p.y);
        else
        if (sType == "RSE")  pmuRse->Popup(p.x, p.y);

    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::pmuFacilityPopup(TObject *Sender)
{
    TPopupMenu *pMenu = (TPopupMenu*)Sender;
    if (pMenu->Tag == 0)
    {
        MnuCctvId->Caption = "[CCTV] " + FSelId;
    }
    else
    if (pMenu->Tag == 1)
    {
        MnuVmsId->Caption = "[VMS] " + FSelId;
    }
    else
    if (pMenu->Tag == 2)
    {
        MnuRseId->Caption = "[RSE] " + FSelId;
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::mnuFacilityMoveClick(TObject *Sender)
{
    TMenuItem *pMenu = (TMenuItem*)Sender;

    Application->ProcessMessages();
    if (pMenu->Tag == 0)
    {
        MoveGisFacility("CCTV", FSelId);
    }
    else
    if (pMenu->Tag == 1)
    {
        MoveGisFacility("VMS", FSelId);
    }
    else
    if (pMenu->Tag == 2)
    {
        MoveGisFacility("RSE", FSelId);
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::mnuFacilityPingClick(TObject *Sender)
{
    if (ITS00GEO)
    {
        ITS00GEO->FSelId = FSelId;
        ITS00GEO->mnuFacilityPingClick(Sender);
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::mnuStopImageClick(TObject *Sender)
{
    if (ITS00GEO)
    {
        ITS00GEO->FSelId = FSelId;
        ITS00GEO->mnuStopImageClick(Sender);
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::mnuCctvCamClick(TObject *Sender)
{
    if (ITS00GEO)
    {
        ITS00GEO->FSelId = FSelId;
        ITS00GEO->mnuCctvCamClick(Sender);
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::mnuVmsMsgClick(TObject *Sender)
{
    if (ITS00GEO)
    {
        ITS00GEO->FSelId = FSelId;
        ITS00GEO->mnuVmsMsgClick(Sender);
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS00G0M::mnuVmsCamClick(TObject *Sender)
{
    if (ITS00GEO)
    {
        ITS00GEO->FSelId = FSelId;
        ITS00GEO->mnuVmsCamClick(Sender);
    }
}
//---------------------------------------------------------------------------

