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

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "CDSVmsIfscF.h"
#include "CDSIfsc_VMSF.h"
#include "FrmVmsOprMainF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "VMS0000MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#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 link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxGridCardView"
#pragma link "cxGridCustomLayoutView"
#pragma link "cxCheckBox"
#pragma link "FRAME_VmsStateF"
#pragma link "cxCalc"
#pragma link "cxGridBandedTableView"
#pragma link "FRAME_FacilityStatusListF"
#pragma resource "*.dfm"
TVMS0000M *VMS0000M = NULL;

//---------------------------------------------------------------------------
__fastcall TVMS0000M::TVMS0000M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    LoadLocalSkin();

    //if (FormStyle != fsMDIChild)
    CMM_LoadForm(g_sFormsDir, this);

    PgVmsMonitoring->Properties->ActivePage = TabVmsString;
    //FrmVmsOprMain->dxStatusBar->Panels->Items[isbGisPos]->Visible = true;
    VMS00MAP = new TVMS00MAP(PnlMapBack, (TForm*)this);

    PnlInfoLeft->Width = VmsCtlrManager->FMaxWidth + 12/*폼여백*/ + 34/*기타여백*/;

	FListForm = new TList;
    FSelVmsObj= NULL;
    FSelForm  = NULL;

    FReady = false;

    FMonitoringType = enMonitoringPhase;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::CommClose()
{
    try
    {
        if (FrmVmsOprMain->dxStatusBar->Panels->Items[isbGisPos]->Visible)
            FrmVmsOprMain->dxStatusBar->Panels->Items[isbGisPos]->Visible = false;

        SAFE_DELETE(VMS00MAP);

        ClearFormList();
        SAFE_DELETE(FListForm);

        //if (FormStyle != fsMDIChild)
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::ClearFormList()
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSBACKM *pForm = (TVMSBACKM*)FListForm->Items[ii];
            pForm->Hide();
            SAFE_DELETE(pForm);
        }
        FListForm->Clear();
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::FormCreate(TObject *Sender)
{
    if (VMS00MAP)
    {
        VMS00MAP->Parent = PnlMapBack;
        VMS00MAP->Show();
    }

    CreateVmsMsgForm(VmsCtlrManager->FMaxWidth);
    FRAMEFacilityStatusList1->UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::ChkWidthClick(TObject *Sender)
{
    int nStringW(0), nStringH(0), nFigureW(0), nFigureH(0);
    int nWidth, nPnlWidth;
    if (ChkWidth->Checked)
    {
        nWidth = VmsCtlrManager->FMinWidth;
    }
    else
    {
        nWidth = VmsCtlrManager->FMaxWidth;
    }
    nPnlWidth = nWidth;
    try
    {
        LockWindowUpdate(Handle);

        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSBACKM *pForm = (TVMSBACKM*)FListForm->Items[ii];
            pForm->SetFormSize(nWidth);
            nPnlWidth = pForm->FViewWidth;
        }
    }
    __finally
    {
        PnlInfoLeft->Width = nPnlWidth + 12/*폼여백*/ + 14/*기타여백*/;
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::CreateVmsMsgForm(int AWidth)
{
    FReady = false;

    ClearFormList();

    TVMSBACKM *pStringForm = NULL;
    TVMSBACKM *pFigureForm = NULL;

    VmsCtlrManager->FLists.Lock();
    try
    {
        LockWindowUpdate(Handle);

        TVMSFORM0 *pForm = NULL;

        FOR_STL(TVmsCtlr*, pObj, VmsCtlrManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            pForm = NULL;
            if (1)//if (pObj->VMS_USAG_TYPE_CD == "VUTN")   //문자식
            {
                if (!pStringForm)
                {
                    pStringForm = new TVMSBACKM(this, Handle, TabVmsString->Caption.Trim(), 1);
                    pStringForm->FMonitoringType = FMonitoringType;
                    pStringForm->Parent = TabVmsString;
                    pStringForm->Show();
                    FListForm->Add(pStringForm);
                }
                if (pStringForm) pStringForm->AddVmsForm(String(pObj->VMS_CTLR_NMBR));
            }
            else
            if (0) //if (pObj->VMS_USAG_TYPE_CD == "VUTP")   //도형식
            {
                if (!pFigureForm)
                {
                    pFigureForm = new TVMSBACKM(this, Handle, TabVmsFigure->Caption.Trim(), 1);
                    pFigureForm->FMonitoringType = FMonitoringType;
                    pFigureForm->Parent = TabVmsFigure;
                    pFigureForm->Show();
                    FListForm->Add(pFigureForm);
                }
                if (pFigureForm) pFigureForm->AddVmsForm(String(pObj->VMS_CTLR_NMBR));
            }
        }
    }
    __finally
    {
        VmsCtlrManager->FLists.UnLock();

        PnlInfoLeft->Width = AWidth + 12/*폼여백*/ + 34/*기타여백*/;
        LockWindowUpdate(0);

        ChkWidthClick((TObject*)ChkWidth);
    }

    FReady = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::FormShow(TObject *Sender)
{
    Refresh();
    Application->ProcessMessages();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::FormActivate(TObject *Sender)
{
    ActivateForm(true);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::FormDeactivate(TObject *Sender)
{
    ActivateForm(false);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::LoadLocalSkin()
{
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
}
//---------------------------------------------------------------------------

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

        VMS0000M = NULL;
        Action = caFree;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::OnMainFormMessage(TMessage &Msg)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::OnVmsStateRefreshMessage(TMessage &Msg)
{
    FRAMEFacilityStatusList1->RefreshList();

    for(int ii = 0; ii < FListForm->Count; ii++)
    {
        TVMSBACKM *pForm = (TVMSBACKM*)FListForm->Items[ii];
        pForm->RefreshVmsStatus();
    }

    FRAMEVmsState1->SelVmsObj = FSelVmsObj;

    if (VMS00MAP) VMS00MAP->UpdateFacilityStatus();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::OnVmsFormRefreshMessage(TMessage &Msg)
{
    for(int ii = 0; ii < FListForm->Count; ii++)
    {
        TVMSBACKM *pForm = (TVMSBACKM*)FListForm->Items[ii];
        pForm->RefreshVmsMessage();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::OnVmsIncidentRefreshMessage(TMessage &Msg)
{
    if (VMS00MAP) VMS00MAP->UpdateIncidentStatus();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::OnVmsSelectMessage(TMessage &Msg)
{
    try {
        FSelVmsObj = (TVmsCtlr*)Msg.WParam;
        FRAMEVmsState1->SelVmsObj = FSelVmsObj;

        if (!FSelVmsObj) return;

        TVMSFORM0 *pSelForm = (TVMSFORM0*)Msg.LParam;
        if (pSelForm == FSelForm) return;

        if (FSelForm) FSelForm->SelectForm(false);

        FSelForm = pSelForm;
        if (FSelForm) FSelForm->SelectForm(true);

        //VMS정보제공구간 소통정보 업데이트
        LoadVmsIfscInfo(String(FSelVmsObj->VMS_CTLR_NMBR));
    } catch(...) {};
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::OnVmsSelectDblClickMessage(TMessage &Msg)
{
    try {
        FSelVmsObj = (TVmsCtlr*)Msg.WParam;
        FRAMEVmsState1->SelVmsObj = FSelVmsObj;

        if (!FSelVmsObj) return;

        TVMSFORM0 *pSelForm = (TVMSFORM0*)Msg.LParam;

        if (FSelForm) FSelForm->SelectForm(false);

        FSelForm = pSelForm;
        if (FSelForm) FSelForm->SelectForm(true);

        //VMS정보제공구간 소통정보 업데이트
        LoadVmsIfscInfo(String(FSelVmsObj->VMS_CTLR_NMBR));
        if (VMS00MAP) VMS00MAP->SelectObjectByPos(FSelVmsObj->X_CRDN, FSelVmsObj->Y_CRDN, true);
    } catch(...) {};
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::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;

        int nMemIdx = pGDC->Values[nRow][FRAMEFacilityStatusList1->Column99->Index];
        TVmsCtlr *pObj = (TVmsCtlr*)nMemIdx;
        if (!pObj) return;

        FSelVmsObj = pObj;
        FRAMEVmsState1->SelVmsObj = FSelVmsObj;

        LoadVmsIfscInfo(String(FSelVmsObj->VMS_CTLR_NMBR));
        if (VMS00MAP) VMS00MAP->SelectObjectByPos(pObj->X_CRDN, pObj->Y_CRDN, true);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::LoadVmsIfscInfo(String AVmsNmbr)
{
    CMM_ClearGridTableView(TvList);
    CMM_ClearGridTableView(TvIfsc);

    TcxDataController *pGDC = TvList->DataController;

    TVmsCtlr *pObj = VmsCtlrManager->FLists.Find(AVmsNmbr);
    if (!pObj) return;

    try
    {
        int nRow;
        TvList->BeginUpdate();
        pGDC->BeginUpdate();

        FOR_STL(TVmsIfscRltn*, pIfscRltn, pObj->FIfscRltn)
        {
            TVmsIfsc *pVmsIfsc = VmsIfscManager->FLists.Find(pIfscRltn->VMS_IFSC_ID);
            if (!pVmsIfsc) continue;

            nRow = pGDC->AppendRecord();

            String sDsplPrrt = String(pIfscRltn->DSPL_PRRT);
            pGDC->Values[nRow][Column01->Index] = sDsplPrrt;
            pGDC->Values[nRow][Column02->Index] = String(pIfscRltn->VMS_IFSC_ID);
            pGDC->Values[nRow][Column03->Index] = pVmsIfsc->VMS_IFSC_NM;
            pGDC->Values[nRow][Column04->Index] = pVmsIfsc->DSPL_STRT_NODE_NM + " -> " + pVmsIfsc->DSPL_END_NODE_NM;
            pGDC->Values[nRow][Column05->Index] = ITSUtil_FormatStr(pVmsIfsc->PRCN_DT, STR_DATETIME);

            String sVmsGrad = pVmsIfsc->CMTR_GRAD_CD;
            if      (sVmsGrad == "1") sVmsGrad = "원활";
            else if (sVmsGrad == "2") sVmsGrad = "지체";
            else if (sVmsGrad == "3") sVmsGrad = "정체";
            else                      sVmsGrad = "정보없음";

            pGDC->Values[nRow][Column06->Index] = sVmsGrad;
            pGDC->Values[nRow][Column07->Index] = String(pVmsIfsc->SPED);
            pGDC->Values[nRow][Column08->Index] = String(pVmsIfsc->TRVL_HH);

            pGDC->Values[nRow][Column99->Index] = (int)pVmsIfsc;
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::LoadIfscInfo()
{
    CMM_ClearGridTableView(TvIfsc);

    TcxDataController *pGDC = TvIfsc->DataController;

    int nSelIdx = CMM_GetRecordIndex(TvList);
    if (nSelIdx < 0) return;

    pGDC = TvList->DataController;

    int nMemPtr = pGDC->Values[nSelIdx][Column99->Index];
    TVmsIfsc *pVmsIfsc = (TVmsIfsc*)nMemPtr;
    if (!pVmsIfsc) return;

    pGDC = TvIfsc->DataController;
    try
    {
        int nRow;
        TvIfsc->BeginUpdate();
        pGDC->BeginUpdate();

        FOR_STL(TVmsLinkIfsc*, pLinkIfsc, pVmsIfsc->FLists)
        {
            nRow = pGDC->AppendRecord();

            TIfsc *pIfsc = IfscManager->FLists.Find(pLinkIfsc->IFSC_ID);
            if (!pIfsc) continue;

            pGDC->Values[nRow][cxGridColumn1->Index] = String(pLinkIfsc->ORD);
            pGDC->Values[nRow][cxGridColumn2->Index] = pIfsc->IFSC_ID;
            pGDC->Values[nRow][cxGridColumn3->Index] = pIfsc->IFSC_NM;
            pGDC->Values[nRow][cxGridColumn4->Index] = pIfsc->STRT_NM + " -> " + pIfsc->END_NM;
            pGDC->Values[nRow][cxGridColumn5->Index] = ITSUtil_FormatStr(pIfsc->PRCN_DT, STR_DATETIME);

            String sGrad = pIfsc->CMTR_GRAD_CD;
            if      (sGrad == "1") sGrad = "원활";
            else if (sGrad == "2") sGrad = "지체";
            else if (sGrad == "3") sGrad = "정체";
            else                   sGrad = "정보없음";

            pGDC->Values[nRow][cxGridColumn6->Index] = sGrad;
            pGDC->Values[nRow][cxGridColumn7->Index] = String(pIfsc->SPED);
            pGDC->Values[nRow][cxGridColumn8->Index] = String(pIfsc->TRVL_HH);
            pGDC->Values[nRow][cxGridColumn9->Index] = String(pIfsc->SECT_LNGT);
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvIfsc->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::TvListFocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    if (!AFocusedRecord) return;
    LoadIfscInfo();
}
//---------------------------------------------------------------------------


void __fastcall TVMS0000M::cxGridColumn6CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor = ACanvas->Brush->Color;
        String sGrade = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];

        if      (sGrade.Pos("원활")) tColor = clLime;
        else if (sGrade.Pos("지체")) tColor = clYellow;
        else if (sGrade.Pos("정체")) tColor = clRed;
        else                         tColor = clGray;

        ACanvas->SetBrushColor(tColor);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::MoveGisFacility(String AType, String AId)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::ScrollBoxMouseWheel(TObject *Sender, TShiftState Shift,
          int WheelDelta, TPoint &MousePos, bool &Handled)
{
    TScrollBox *pScrollBox = (TScrollBox*)Sender;
    pScrollBox->VertScrollBar->Position -= WheelDelta;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::ActivateForm(bool AActivate)
{
    try
    {
        int nTabIndex = PgVmsMonitoring->ActivePageIndex;
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSBACKM *pForm = (TVMSBACKM*)FListForm->Items[ii];
            if (AActivate)
            {
                if (nTabIndex == ii) pForm->Play = true;
                else                 pForm->Play = false;
            }
            else
                pForm->Play = false;
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::PgVmsMonitoringPageChanging(TObject *Sender, TcxTabSheet *NewPage,
          bool &AllowChange)
{
    if (!FReady) return;

    try
    {
        int nTabIndex = NewPage->TabIndex;
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSBACKM *pForm = (TVMSBACKM*)FListForm->Items[ii];
            if (nTabIndex == ii) pForm->Play = true;
            else                 pForm->Play = false;
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0000M::TvIfscCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift,
          bool &AHandled)
{
	if (!ACellViewInfo) return;

	TcxGridDataController *pDc = TvIfsc->DataController;
	int nRow = pDc->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sIfscIf  = VarToStr(pDc->Values[nRow][cxGridColumn2->Index]);
    if (VMS00MAP) VMS00MAP->SelectLinkById(2, sIfscIf, true);
}
//---------------------------------------------------------------------------

