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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "FrmResourceF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "FRAME_RepeatCongestListF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
//TFRAMERepeatCongestList *FRAMERepeatCongestList;
//---------------------------------------------------------------------------
__fastcall TFRAMERepeatCongestList::TFRAMERepeatCongestList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<현재 확정된 반복 정체구간이 없습니다>";

    FCodeRCS = ItsCodeManager->FLists.Find("RCS");
    FCodeDTW = ItsCodeManager->FLists.Find("DTW");
    FCodeLTC = ItsCodeManager->FLists.Find("LTC");
    FCodeIFD = ItsCodeManager->FLists.Find("IFD");
}
//---------------------------------------------------------------------------

void __fastcall TFRAMERepeatCongestList::UpdateList()
{
    TCDSTraffic *pTraf;
    TItsIfsc    *pIfsc;
    TItsSubCode *pSubCode;

    CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TvList->BeginUpdate();

        try
        {
            ItsRepeatCongestManager->FListsCnfm.Lock();
            FOR_STL(TItsRepeatCongest*, pRpLink, ItsRepeatCongestManager->FListsCnfm)
            {
                //링크기본정보
                pIfsc = ItsIfscManager->FLists.Find(pRpLink->IFSC_ID);
                if (!pIfsc) continue;    //기본정보가 없는 것은 보여줄 필요가 없다.

                String sCurrAnsTm = "-";
                String sCurrSpeed = "-";
                String sCurrTrvTm = "-";

                //현재소통정보
                pTraf = pIfsc->TRAF;
                if (pTraf)
                {
                    if (pTraf->MISSING != "Y")
                    {
                        sCurrAnsTm = pTraf->PRCN_DT;
                        sCurrSpeed = pTraf->SPED;
                        sCurrTrvTm = pTraf->TRVL_HH;
                    }
                }

#if 0
                String sDTW = "[" + pRpLink->DAY_TYPE_CD + "] "; //요일 유형 코드
                if (FCodeDTW)
                {
                    pSubCode = FCodeDTW->FSubLists.Find(pRpLink->DAY_TYPE_CD);
                    if (pSubCode) sDTW = sDTW + pSubCode->CMMN_CD_KOR_NM;
                }
#endif

#if 0
                String sRCS = "[" + pRpLink->REPT_CNGS_SECT_ORGN_CD + "] "; //반복 정체 구간 원본 코드
                if (FCodeRCS)
                {
                    pSubCode = FCodeRCS->FSubLists.Find(pRpLink->REPT_CNGS_SECT_ORGN_CD);
                    if (pSubCode) sRCS = sRCS + pSubCode->CMMN_CD_KOR_NM;
                }
                if (pRpLink->REPT_CNGS_SECT_ORGN_CD == "") sRCS = "";
#endif

                if (sCurrAnsTm != "")
                    sCurrAnsTm = ITSUtil_StrToTime(sCurrAnsTm);

                String sIFD = "[" + pIfsc->DRCT_CD  + "] ";
                if (FCodeIFD)
                {
                    pSubCode = FCodeIFD->FSubLists.Find(pIfsc->DRCT_CD);                 // 방향코드
                    if (pSubCode) sIFD = sIFD + pSubCode->CMMN_CD_KOR_NM;
                }

                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Column01->Index] = pIfsc->IFSC_ID;      //'정보제공구간ID';
                m_pGDC->Values[nRow][Column02->Index] = pIfsc->IFSC_NM;      //'정보제공구간 명'
                m_pGDC->Values[nRow][Column03->Index] = sIFD;                   //'방향 코드'
                m_pGDC->Values[nRow][Column04->Index] = pIfsc->STRT_NM;      //'시점 명'
                m_pGDC->Values[nRow][Column05->Index] = pIfsc->END_NM;       //'종점 명'

                m_pGDC->Values[nRow][Column06->Index] = pRpLink->AVRG_SPED;     //'확정 평균통행속도';
                m_pGDC->Values[nRow][Column07->Index] = pRpLink->AVRG_TRVL_HH;  //'확정 평균통행시간';
                m_pGDC->Values[nRow][Column08->Index] = sCurrAnsTm;             //'소통정보가공시각';
                m_pGDC->Values[nRow][Column09->Index] = sCurrSpeed;             //'현재 통행속도';
                m_pGDC->Values[nRow][Column10->Index] = sCurrTrvTm;             //'현재 통행시간';

                m_pGDC->Values[nRow][Column20->Index] = FrmLang->lblRcTp0->Caption;//"평시";
            }
        }
        __finally
        {
            ItsRepeatCongestManager->FListsCnfm.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }

    UpdateListWthr();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMERepeatCongestList::UpdateListWthr()
{
    TCDSTraffic *pTraf;
    TItsIfsc    *pIfsc;
    TItsSubCode *pSubCode;

    //CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TvList->BeginUpdate();

        try
        {
            ItsRepeatCongestManager->FListsCnfmWthr.Lock();

            Column20->Visible = ItsRepeatCongestManager->FListsCnfmWthr.Size() > 0 ? true : false;

            FOR_STL(TItsRepeatCongestWthr*, pRpLink, ItsRepeatCongestManager->FListsCnfmWthr)
            {
                //링크기본정보
                pIfsc = ItsIfscManager->FLists.Find(pRpLink->IFSC_ID);
                if (!pIfsc) continue;    //기본정보가 없는 것은 보여줄 필요가 없다.

                String sCurrAnsTm = "-";
                String sCurrSpeed = "-";
                String sCurrTrvTm = "-";

                //현재소통정보
                pTraf = pIfsc->TRAF;
                if (pTraf)
                {
                    if (pTraf->MISSING != "Y")
                    {
                        sCurrAnsTm = pTraf->PRCN_DT;
                        sCurrSpeed = pTraf->SPED;
                        sCurrTrvTm = pTraf->TRVL_HH;
                    }
                }

#if 0
                String sDTW = "[" + pRpLink->DAY_TYPE_CD + "] "; //요일 유형 코드
                if (FCodeDTW)
                {
                    pSubCode = FCodeDTW->FSubLists.Find(pRpLink->DAY_TYPE_CD);
                    if (pSubCode) sDTW = sDTW + pSubCode->CMMN_CD_KOR_NM;
                }
#endif

#if 0
                String sRCS = "[" + pRpLink->REPT_CNGS_SECT_ORGN_CD + "] "; //반복 정체 구간 원본 코드
                if (FCodeRCS)
                {
                    pSubCode = FCodeRCS->FSubLists.Find(pRpLink->REPT_CNGS_SECT_ORGN_CD);
                    if (pSubCode) sRCS = sRCS + pSubCode->CMMN_CD_KOR_NM;
                }
                if (pRpLink->REPT_CNGS_SECT_ORGN_CD == "") sRCS = "";
#endif

                if (sCurrAnsTm != "")
                    sCurrAnsTm = ITSUtil_StrToTime(sCurrAnsTm);

                String sIFD = "[" + pIfsc->DRCT_CD  + "] ";
                if (FCodeIFD)
                {
                    pSubCode = FCodeIFD->FSubLists.Find(pIfsc->DRCT_CD);                 // 방향코드
                    if (pSubCode) sIFD = sIFD + pSubCode->CMMN_CD_KOR_NM;
                }

                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Column01->Index] = pIfsc->IFSC_ID;      //'정보제공구간ID';
                m_pGDC->Values[nRow][Column02->Index] = pIfsc->IFSC_NM;      //'정보제공구간 명'
                m_pGDC->Values[nRow][Column03->Index] = sIFD;                   //'방향 코드'
                m_pGDC->Values[nRow][Column04->Index] = pIfsc->STRT_NM;      //'시점 명'
                m_pGDC->Values[nRow][Column05->Index] = pIfsc->END_NM;       //'종점 명'

                m_pGDC->Values[nRow][Column06->Index] = pRpLink->AVRG_SPED;     //'확정 평균통행속도';
                m_pGDC->Values[nRow][Column07->Index] = pRpLink->AVRG_TRVL_HH;  //'확정 평균통행시간';
                m_pGDC->Values[nRow][Column08->Index] = sCurrAnsTm;             //'소통정보가공시각';
                m_pGDC->Values[nRow][Column09->Index] = sCurrSpeed;             //'현재 통행속도';
                m_pGDC->Values[nRow][Column10->Index] = sCurrTrvTm;             //'현재 통행시간';

                m_pGDC->Values[nRow][Column20->Index] = FrmLang->lblRcTp1->Caption;//"기상";
            }
        }
        __finally
        {
            ItsRepeatCongestManager->FListsCnfmWthr.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMERepeatCongestList::GetSelLinkIds(String &ALinkIds)
{
    return 0;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMERepeatCongestList::Column20CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (Column20->Visible == false) return;

    TColor tColor = ACanvas->Brush->Color;

    try
    {
        if (!VarIsNull(AViewInfo->Value))
        {
            String sValue = AViewInfo->Value;
            if (sValue == FrmLang->lblRcTp1->Caption)//"기상")
            {
                tColor = clFuchsia;
            }
        }

        ACanvas->Font->Color = clBlack;
        ACanvas->SetBrushColor(tColor);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

