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

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

#include "WindowMsgF.h"

#include "FrmFacilityAlarmF.h"
#include "FrmLinkGradeSetF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "CDSRoadF.h"
#include "CDSTrafficGradeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#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 "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"
TFrmFacilityAlarm *FrmFacilityAlarm = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmFacilityAlarm::TFrmFacilityAlarm(TComponent* Owner)
    : TForm(Owner)
{
Width = 330;
Height= 340;
int nHeight = Height;
int nWidth  = Width;

    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    Caption = lblCaption->Caption;//" 시설물 알람";
    m_sLinkId    = "";
    m_nLinkLevel = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::FormClose(TObject *Sender, TCloseAction &Action)
{
    TmrTimeOut->Enabled = false;
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
    Action = caFree;
    FrmFacilityAlarm = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::ShowDsrcAlarm()
{
    TcxDataController *m_pGDC;
    m_pGDC = TvList->DataController;

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

        TItsSubCode *pSubCode;
        try
        {
            ItsFacilityManager->FLists.Lock();
            FOR_STL(TItsFacility*, pFacility, ItsFacilityManager->FLists)
            {
                if (pFacility->DEL_YN == "Y") continue;

                if (pFacility->FCLT_TYPE != "DSRC") continue;

                if (pFacility->FCollCntBase <= pFacility->FCollCntCurr) continue;

                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Column01->Index]  = pFacility->FCLT_TYPE;
                if (pFacility->FCLT_TYPE == "PARK")
                    m_pGDC->Values[nRow][Column11->Index] = "주차장";
                else
                if (pFacility->FCLT_TYPE == "CCAM")
                    m_pGDC->Values[nRow][Column11->Index] = "교차로감시";
                else
                if (pFacility->FCLT_TYPE == "ICAM")
                    m_pGDC->Values[nRow][Column11->Index] = "돌발상황감시";
                else
                if (pFacility->FCLT_TYPE == "WCAM")
                    m_pGDC->Values[nRow][Column11->Index] = "CCTV(웹카메라)";
                else
                if (pFacility->FCLT_TYPE == "VDS")
                {
                    if (pFacility->FCLT_KIND == "C")
                    {
                        m_pGDC->Values[nRow][Column01->Index] = "CRSSVR";
                        m_pGDC->Values[nRow][Column11->Index] = "교차로분석서버";
                    }
                    else
                        m_pGDC->Values[nRow][Column11->Index] = pFacility->FCLT_TYPE;
                }
                else
                    m_pGDC->Values[nRow][Column11->Index] = pFacility->FCLT_TYPE;

                m_pGDC->Values[nRow][Column02->Index]  = pFacility->FCLT_ID;
                m_pGDC->Values[nRow][Column03->Index]  = pFacility->FCLT_LCTN;

                String sComm = FrmLang->lblClctErr->Caption;//"수집이상";

                m_pGDC->Values[nRow][Column04->Index] = sComm;
                m_pGDC->Values[nRow][Column05->Index] = pFacility->FDoor;
                m_pGDC->Values[nRow][Column06->Index] = "3";//pFacility->FComm;
                m_pGDC->Values[nRow][Column07->Index] = pFacility->FDoorStatus;

                m_pGDC->Values[nRow][Column08->Index] = String(pFacility->FCollCntCurr);
                //m_pGDC->Values[nRow][Column99->Index]  = 0;
            }
        }
        __finally
        {
            ItsFacilityManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::ShowFacilityAlarm()
{
    TmrTimeOut->Enabled = false;

    CMM_ClearGridTableView(TvList);

#ifdef USE_DSRC
    //ShowDsrcAlarm();
#endif

    TItsLayer *pLayer;
    TcxDataController *m_pGDC;
    m_pGDC = TvList->DataController;


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

        TItsSubCode *pSubCode;
        try
        {
            ItsFacilityManager->FLists.Lock();
            FOR_STL(TItsFacility*, pFacility, ItsFacilityManager->FLists)
            {
                if (pFacility->DEL_YN == "Y") continue;

                bool  bAlarm  = false;
                String sComm  = FrmLang->lblComm0->Caption;//"정상";
                String sAlarm = "";

                if (pFacility->FComm != str_state_normal)
                {
                    sComm = FrmLang->lblComm1->Caption;//"통신이상";
                    sAlarm = "1";
                    bAlarm = true;
                }
                else
                {
                    if (!bAlarm && pFacility->FDoor != str_state_normal)
                    {
                        sComm = "도어열림";
                        sAlarm = "2";
                        bAlarm = true;
                    }
                    if (!bAlarm && pFacility->FCBoxTmpr > g_AppCfg.Temp.AlarmValue)
                    {
                        sComm = "온도이상";
                        sAlarm = "3";
                        bAlarm = true;
                    }

                    if (!bAlarm && pFacility->FCLT_TYPE == "VMS")
                    {
                        if (pFacility->Sts01 != str_state_normal || pFacility->Sts02 != str_state_normal)
                        {
                            sComm = "전광판/전원꺼짐";
                            sAlarm = "4";
                            bAlarm = true;
                        }
                    }
                    if (!bAlarm && pFacility->FCLT_TYPE == "VDS")
                    {
                        if (pFacility->FCollError)
                        {
                            sComm = "수집이상";
                            sAlarm = "5";
                            bAlarm = true;
                        }
                    }
                }

                if (!bAlarm)
                {
                    continue;
                }

                nRow = m_pGDC->AppendRecord();
                pLayer = ItsLayerManager->FLists.Find(pFacility->FCLT_TYPE);
                if (pLayer)
                {
                    m_pGDC->Values[nRow][Column11->Index] = pLayer->Name;                        // FCLT_TYPE VARCHAR(7 BYTE) NOT NULL, '시설물유형';
                }
                else
                {
                    m_pGDC->Values[nRow][Column11->Index] = pFacility->FCLT_TYPE;                        // FCLT_TYPE VARCHAR(7 BYTE) NOT NULL, '시설물유형';
                }

                m_pGDC->Values[nRow][Column01->Index]  = pFacility->FCLT_TYPE;
                m_pGDC->Values[nRow][Column02->Index]  = pFacility->FCLT_ID;
                m_pGDC->Values[nRow][Column03->Index]  = pFacility->FCLT_LCTN;

                m_pGDC->Values[nRow][Column04->Index] = sComm;
                m_pGDC->Values[nRow][Column06->Index] = sAlarm;
                //m_pGDC->Values[nRow][Column05->Index] = pFacility->FDoor;
                //m_pGDC->Values[nRow][Column06->Index] = pFacility->FComm;
                //m_pGDC->Values[nRow][Column07->Index] = pFacility->FDoorStatus;

                m_pGDC->Values[nRow][Column08->Index] = pFacility->FCBoxTmpr;
                //m_pGDC->Values[nRow][Column99->Index]  = 0;
            }
        }
        __finally
        {
            ItsFacilityManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
    TmrTimeOut->Interval = g_AppCfg.Alarm.TimeOut * 1000;
    TmrTimeOut->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::Column04CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo->GridRecord->RecordIndex < 0) return;

    Graphics::TBitmap *pBmp = NULL;
    pBmp = ImgState1->Picture->Bitmap;
	if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column06->Index]))
    {
        String sCode = AViewInfo->GridRecord->DisplayTexts[Column06->Index];

        if      (sCode == "1") pBmp = ImgState1->Picture->Bitmap;
        else if (sCode == "2") pBmp = ImgState2->Picture->Bitmap;
        else if (sCode == "3") pBmp = ImgState3->Picture->Bitmap;
        else if (sCode == "4") pBmp = ImgState4->Picture->Bitmap;
        else                   pBmp = ImgState5->Picture->Bitmap;
    }

    if (!pBmp) return;
    pBmp->Transparent = true;

    int nW, nH;
    nW = nH = 21;
    try
    {
        TColor bBrush = ACanvas->Brush->Color;
        if (AViewInfo->GridRecord->Selected)
        {
            if (AViewInfo->GridRecord->Index % 2 == 0)
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentEvenColor();
            }
            else
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentOddColor();
            }
        }
        ACanvas->Brush->Color = bBrush;

        TRect rc;
        TRect rcOrg = AViewInfo->Bounds;
        ACanvas->FillRect(rcOrg);

        nW = rcOrg.Bottom - rcOrg.Top - 2;
        rc.Top    = rcOrg.Top+1;
        rc.Bottom = rcOrg.Bottom - 1;
        rc.Left   = rcOrg.Left + (rcOrg.Width() - nW) / 2;
        rc.Right  = rc.Left + nW;

        ACanvas->Canvas->StretchDraw(rc, pBmp);
        //ACanvas->Draw(AViewInfo->ContentBounds.Left, AViewInfo->ContentBounds.Top, pBmp);
        ADone = true;
    }
    catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::TmrTimeOutTimer(TObject *Sender)
{
    TmrTimeOut->Enabled = false;
    if (!g_AppCfg.Alarm.Window)
    {
        Close();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::FormShow(TObject *Sender)
{
    chkViewAlway->Checked = g_AppCfg.Alarm.Window;
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::chkViewAlwayClick(TObject *Sender)
{
    g_AppCfg.Alarm.Window = chkViewAlway->Checked;
    String sWindow = g_AppCfg.Alarm.Window ? "1" : "0";

    WriteConfigInfo(g_AppCfg.sConfigFile, "ALARM", "WINDOW", sWindow);
}
//---------------------------------------------------------------------------

