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

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

#include "FRAME_AtmpPltnListF.h"
#include "ITS_OPLibF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxCalc"
#pragma link "cxClasses"
#pragma link "cxContainer"
#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 "cxGridBandedTableView"
#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"
//TFRAMEAtmpPltnList *FRAMEAtmpPltnList;
//---------------------------------------------------------------------------
__fastcall TFRAMEAtmpPltnList::TFRAMEAtmpPltnList(TComponent* Owner)
    : TFrame(Owner)
{
    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<현재 지역 날씨정보가 없습니다>";
    FAlarmCount1 = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEAtmpPltnList::UpdateList()
{
    CMM_ClearGridTableView(TvList);
    TcxDataController *pGDC = TvList->DataController;

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

        try
        {
            ItsVilgFrcsManager->FAtmpLists.Lock();

            FOR_STL(TItsAtmpPltn *, pObj, ItsVilgFrcsManager->FAtmpLists)
            {
                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][Col00->Index] = pObj->ATMP_STTN_NM;
                pGDC->Values[nRow][Col01->Index] = pObj->ATMP_STTN_ADDR;
                pGDC->Values[nRow][Col30->Index] = pObj->X_CRDN;
                pGDC->Values[nRow][Col31->Index] = pObj->Y_CRDN;

                pGDC->Values[nRow][Col02->Index] = pObj->MSRM_SYST_NM;

                pGDC->Values[nRow][Col03->Index] = APP_FormatStr(pObj->pnst.MSRM_DT, STR_DATETIME);

                pGDC->Values[nRow][Col04->Index] = pObj->pnst.CO_VAL;
                pGDC->Values[nRow][Col05->Index] = pObj->pnst.SO2_VAL;
                pGDC->Values[nRow][Col06->Index] = pObj->pnst.NO2_VAL;
                pGDC->Values[nRow][Col07->Index] = pObj->pnst.O3_VAL;
                pGDC->Values[nRow][Col08->Index] = pObj->pnst.PM10_VAL;
                pGDC->Values[nRow][Col09->Index] = pObj->pnst.PM10_24HH_VAL;
                pGDC->Values[nRow][Col10->Index] = pObj->pnst.PM25_VAL;
                pGDC->Values[nRow][Col11->Index] = pObj->pnst.PM25_24HH_VAL;
                pGDC->Values[nRow][Col12->Index] = pObj->pnst.INTG_ATMP_VAL;

                pGDC->Values[nRow][Col13->Index] = pObj->pnst.CO_GRAD_NM;
                pGDC->Values[nRow][Col14->Index] = pObj->pnst.SO2_GRAD_NM;
                pGDC->Values[nRow][Col15->Index] = pObj->pnst.NO2_GRAD_NM;
                pGDC->Values[nRow][Col16->Index] = pObj->pnst.O3_GRAD_NM;
                pGDC->Values[nRow][Col17->Index] = pObj->pnst.PM10_1HH_GRAD_NM;
                pGDC->Values[nRow][Col18->Index] = pObj->pnst.PM10_24HH_GRAD_NM;
                pGDC->Values[nRow][Col19->Index] = pObj->pnst.PM25_1HH_GRAD_NM;
                pGDC->Values[nRow][Col20->Index] = pObj->pnst.PM25_24HH_GRAD_NM;
                pGDC->Values[nRow][Col21->Index] = pObj->pnst.INTG_ATMP_GRAD_NM;

                pGDC->Values[nRow][ColUPDT_DT->Index] = APP_FormatStr(pObj->pnst.UPDT_DT, STR_DATETIME);

                pGDC->Values[nRow][Column99->Index] = (int)pObj;
            }
        }
        __finally
        {
            ItsVilgFrcsManager->FAtmpLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }

    UpdateSprpList();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEAtmpPltnList::RefreshList()
{
    TcxDataController *pGDC = TvList->DataController;

    try
    {
        TvList->BeginUpdate();

        try
        {
            ItsVilgFrcsManager->FAtmpLists.Lock();

            int nMemPtr;
            int nDataCnt = pGDC->RecordCount;

            for (int nRow = 0; nRow < nDataCnt; nRow++)
            {
                nMemPtr = pGDC->Values[nRow][Column99->Index];
                TItsAtmpPltn *pObj = (TItsAtmpPltn*)nMemPtr;
                if (!pObj) continue;

                pGDC->Values[nRow][Col03->Index] = APP_FormatStr(pObj->pnst.MSRM_DT, STR_DATETIME);

                pGDC->Values[nRow][Col04->Index] = pObj->pnst.CO_VAL;
                pGDC->Values[nRow][Col05->Index] = pObj->pnst.SO2_VAL;
                pGDC->Values[nRow][Col06->Index] = pObj->pnst.NO2_VAL;
                pGDC->Values[nRow][Col07->Index] = pObj->pnst.O3_VAL;
                pGDC->Values[nRow][Col08->Index] = pObj->pnst.PM10_VAL;
                pGDC->Values[nRow][Col09->Index] = pObj->pnst.PM10_24HH_VAL;
                pGDC->Values[nRow][Col10->Index] = pObj->pnst.PM25_VAL;
                pGDC->Values[nRow][Col11->Index] = pObj->pnst.PM25_24HH_VAL;
                pGDC->Values[nRow][Col12->Index] = pObj->pnst.INTG_ATMP_VAL;

                pGDC->Values[nRow][Col13->Index] = pObj->pnst.CO_GRAD_NM;
                pGDC->Values[nRow][Col14->Index] = pObj->pnst.SO2_GRAD_NM;
                pGDC->Values[nRow][Col15->Index] = pObj->pnst.NO2_GRAD_NM;
                pGDC->Values[nRow][Col16->Index] = pObj->pnst.O3_GRAD_NM;
                pGDC->Values[nRow][Col17->Index] = pObj->pnst.PM10_1HH_GRAD_NM;
                pGDC->Values[nRow][Col18->Index] = pObj->pnst.PM10_24HH_GRAD_NM;
                pGDC->Values[nRow][Col19->Index] = pObj->pnst.PM25_1HH_GRAD_NM;
                pGDC->Values[nRow][Col20->Index] = pObj->pnst.PM25_24HH_GRAD_NM;
                pGDC->Values[nRow][Col21->Index] = pObj->pnst.INTG_ATMP_GRAD_NM;
                pGDC->Values[nRow][ColUPDT_DT->Index] = APP_FormatStr(pObj->pnst.UPDT_DT, STR_DATETIME);
            }
        }
        __finally
        {
            ItsVilgFrcsManager->FAtmpLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }

    UpdateSprpList();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEAtmpPltnList::UpdateSprpList()
{
    FAlarmCount1 = 0;
    LblAlarm->Visible = false;

    String sMsg = "";
    String sTmp = "";
    bool   bAlarm = false;
    try
    {
        ItsVilgFrcsManager->FSprpLists.Lock();

        FOR_STL(TItsAtmpSprp *, pObj, ItsVilgFrcsManager->FSprpLists)
        {
            if (pObj->ANNC_DVSN != "1") continue;

            sTmp = "";
            if (pObj->ATMP_PLTN_KIND == "1")
            {
                if      (pObj->ANNC_GRAD == "1") sTmp = "미세먼지 주의보", bAlarm = true;
                else if (pObj->ANNC_GRAD == "2") sTmp = "미세먼지 경보";
                else if (pObj->ANNC_GRAD == "5") sTmp = "황사경보", bAlarm = true;
            }
            else
            if (pObj->ATMP_PLTN_KIND == "2")
            {
                if      (pObj->ANNC_GRAD == "1") sTmp = "초미세먼지 주의보";
                else if (pObj->ANNC_GRAD == "2") sTmp = "초미세먼지 경보", bAlarm = true;
            }

            if (sTmp != "")
            {
                FAlarmCount1++;
                if (sMsg != "") sMsg += ", ";

                sMsg += sTmp;
            }
        }
    }
    __finally
    {
        ItsVilgFrcsManager->FSprpLists.UnLock();
    }

    if (FAlarmCount1 > 0)
    {
        LblAlarm->Caption = "    " + sMsg + " 발령    ";

        if (bAlarm)
        {
            LblAlarm->Style->Color = Lbl4->Style->Color;
        }
        else
        {
            LblAlarm->Style->Color = Lbl3->Style->Color;
        }
        LblAlarm->AutoSize = false;
        LblAlarm->AutoSize = true;
        LblAlarm->Visible = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEAtmpPltnList::Col13CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    TColor tColor = ACanvas->Brush->Color;
    TColor tFont  = clBlack;

    try
    {
        if (!VarIsNull(AViewInfo->Value))
        {
            String sVal = AViewInfo->Value;
            if (sVal == ATMP_GOOD)
            {
                tColor = clBlue;
                tFont  = clWhite;
            }
            else
            if (sVal == ATMP_NORMAL)
            {
                tColor = clLime;
            }
            else
            if (sVal == ATMP_BAD)
            {
                tColor = clYellow;
            }
            else
            if (sVal == ATM_VERY_BAD)
            {
                tColor = clRed;
            }
            else
            //if (sVal == ATMP_NONE)
            {
                tColor = clSilver;
            }
        }

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

void __fastcall TFRAMEAtmpPltnList::Col12CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    TColor tColor = ACanvas->Brush->Color;
    TColor tFont  = clWhite;

    try
    {
        if (!VarIsNull(AViewInfo->Value))
        {
            //String sVal = AViewInfo->Value;
            String sVal = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index+9];
            if (sVal == ATMP_GOOD)
            {
                tColor = Lbl1->Style->Color;
                tFont  = Lbl1->Style->TextColor;
            }
            else
            if (sVal == ATMP_NORMAL)
            {
                tColor = Lbl2->Style->Color;
                tFont  = Lbl2->Style->TextColor;
            }
            else
            if (sVal == ATMP_BAD)
            {
                tColor = Lbl3->Style->Color;
                tFont  = Lbl3->Style->TextColor;
            }
            else
            if (sVal == ATM_VERY_BAD)
            {
                tColor = Lbl4->Style->Color;
                tFont  = Lbl4->Style->TextColor;
            }
            else
            //if (sVal == ATMP_NONE)
            {
                tColor = Lbl0->Style->Color;
                tFont  = Lbl0->Style->TextColor;
            }
        }

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

