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

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

#include "WindowMsgF.h"

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

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#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 "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#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 "cxCheckBox"
#pragma resource "*.dfm"
TFrmFacilityAlarm *FrmFacilityAlarm = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmFacilityAlarm::TFrmFacilityAlarm(TComponent* Owner)
    : TForm(Owner)
{
Width = 330;
Height= 340;
int nHeight = Height;
int nWidth  = Width;

    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
//Height = nHeight;
//Width  = nWidth;

    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::ShowFacilityAlarm()
{
    TmrTimeOut->Enabled = false;

    CMM_ClearGridTableView(TvList);

    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)
            {
                pFacility->FStatusGridIndex = -1;
                if (pFacility->DEL_YN == "Y") continue;

                if (pFacility->FComm == "1")
                {
#ifdef D_GJ
                    try
                    {
                        String sValue = pFacility->FCBoxTmpr;
                        int nValue = 0;
                        if (TryStrToInt(sValue, nValue))
                        {
                            if (nValue <= g_AppCfg.Temp.AlarmValue)
                            {
                                continue;
                            }
                        }
                        else continue;
                    } catch(...) {}
#else
                    continue;
#endif
                }

                nRow = m_pGDC->AppendRecord();

                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;

                String sComm = "정상";
                if (pFacility->FComm == "0") sComm = "통신이상";
                else
                if (pFacility->FComm == "2") sComm = "함체문열림";

#ifdef D_GJ
                try
                {
                    String sValue = pFacility->FCBoxTmpr;
                    int nValue = 0;
                    if (TryStrToInt(sValue, nValue))
                    {
                        if (nValue > g_AppCfg.Temp.AlarmValue)
                        {
                            sComm = "함체온도";
                        }
                    }
                } catch(...) {}
#endif

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

                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)
{
#ifdef D_GJ
    if (AViewInfo->GridRecord->RecordIndex < 0) return;

    Graphics::TBitmap *pBmp = NULL;
    pBmp = ImgStop->Picture->Bitmap;
	if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column06->Index]))
    {
        String sCode = AViewInfo->GridRecord->DisplayTexts[Column06->Index];
        if      (sCode == "0") pBmp = ImgStop->Picture->Bitmap;
        else if (sCode == "1") pBmp = ImgRun->Picture->Bitmap;
        else if (sCode == "2") pBmp = ImgStop->Picture->Bitmap;

        if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column08->Index]))
        {
            String sValue = AViewInfo->GridRecord->DisplayTexts[Column08->Index];
            int nValue = 0;
            if (TryStrToInt(sValue, nValue))
            {
                if (nValue > g_AppCfg.Temp.AlarmValue)
                {
                    pBmp = ImgModule->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(...) {}
#else
    if (AViewInfo->GridRecord->RecordIndex < 0) return;

    Graphics::TBitmap *pBmp = NULL;
    pBmp = ImgStop->Picture->Bitmap;
	if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column06->Index]))
    {
        String sCode = AViewInfo->GridRecord->DisplayTexts[Column06->Index];
        if      (sCode == "0") pBmp = ImgStop->Picture->Bitmap;
        else if (sCode == "1") pBmp = ImgRun->Picture->Bitmap;
        else if (sCode == "2") pBmp = ImgModule->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(...) {}
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFrmFacilityAlarm::TmrTimeOutTimer(TObject *Sender)
{
    TmrTimeOut->Enabled = false;
    Close();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmFacilityAlarm::chkViewAlwayClick(TObject *Sender)
{
    String sWindow = chkViewAlway->Checked ? "1" : "0";
    String sIniFile = ChangeFileExt(ExtractFileName(Application->ExeName), ".ini");
    WriteConfigInfo(sIniFile, "ALARM", "WINDOW", sWindow);
}
//---------------------------------------------------------------------------

