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

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

#include "CTV0101MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#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 "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 resource "*.dfm"

//TCTV0101M *CTV0101M = NULL;
//---------------------------------------------------------------------------
__fastcall TCTV0101M::TCTV0101M(TComponent* Owner)
    : TForm(Owner)
{
    FRow = 0;
    FCol = 0;
    FDeleted = false;
    FCTLR_NMBR = -1;
    FObjInfo   = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::FormCreate(TObject *Sender)
{
    CreateStatusList();    
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::FormDestroy(TObject *Sender)
{
    //TForm::FormDestroy(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::FormShow(TObject *Sender)
{
    //    
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::CommClose()
{
    //CTV0101M = NULL;
}
//---------------------------------------------------------------------------

bool __fastcall TCTV0101M::Initialize()
{
    return true;
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::LoadLocalSkin()
{
    //ParentColor = false;
    //PnlMain->ParentColor = true;
    //Color = clBlue;
    PnlCctv->ParentColor = false;
    PnlCctv->Color = clBlack;
    
    TvList->Styles->Inactive = NULL;
    TvList->Styles->ContentOdd = NULL;
    TvList->Styles->ContentEven= NULL;
    TvList->Styles->Selection  = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::CreateStatusList()
{
    TvList->OptionsView->Header = false;

    CMM_ClearGridTableView(TvList);

    TcxGridDataController *pGDC = (TcxGridDataController *)TvList->DataController;
    try
    {
        int nRow;
        TvList->BeginUpdate();
        pGDC->BeginUpdate();

        nRow = pGDC->AppendRecord();
        pGDC->Values[nRow][Col00->Index] = "통신";
        pGDC->Values[nRow][Col01->Index] = "도어";
        pGDC->Values[nRow][Col02->Index] = "팬";
        pGDC->Values[nRow][Col03->Index] = "히터";
        
        nRow = pGDC->AppendRecord();

        nRow = pGDC->AppendRecord();
        pGDC->Values[nRow][Col00->Index] = "CAM";
        pGDC->Values[nRow][Col01->Index] = "문자";
        pGDC->Values[nRow][Col02->Index] = "배터리";
        pGDC->Values[nRow][Col03->Index] = "";

        nRow = pGDC->AppendRecord();

        nRow = pGDC->AppendRecord();
        pGDC->Values[nRow][Col00->Index] = "온도값";
        pGDC->Values[nRow][Col01->Index] = "입-전압";
        pGDC->Values[nRow][Col02->Index] = "출-전압";
        pGDC->Values[nRow][Col03->Index] = "";

        nRow = pGDC->AppendRecord();
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->EndUpdate();
    }

    SetCommErrorStatus();
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::SetCommErrorStatus()
{
#if 0
    TcxGridDataController *pGDC = (TcxGridDataController *)TvList->DataController;
    try
    {
        int nRow;
        TvList->BeginUpdate();
        pGDC->BeginUpdate();

        pGDC->Values[1][Col00->Index] = String(INT_STATE_ERROR);
        pGDC->Values[1][Col01->Index] = String(INT_STATE_UNKNOWN);
        pGDC->Values[1][Col02->Index] = String(INT_STATE_UNKNOWN);
        pGDC->Values[1][Col03->Index] = String(INT_STATE_UNKNOWN);

        pGDC->Values[3][Col00->Index] = String(INT_STATE_UNKNOWN);
        pGDC->Values[3][Col01->Index] = String(INT_STATE_UNKNOWN);
        pGDC->Values[3][Col02->Index] = String(INT_STATE_UNKNOWN);
        pGDC->Values[3][Col03->Index] = "";

        pGDC->Values[5][Col00->Index] = "-";//String(INT_STATE_UNKNOWN);
        pGDC->Values[5][Col01->Index] = "-";//String(INT_STATE_UNKNOWN);
        pGDC->Values[5][Col02->Index] = "-";//String(INT_STATE_UNKNOWN);
        pGDC->Values[5][Col03->Index] = "";
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->EndUpdate();
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::RefreshStatus()
{
#if 0
    if (!FObjInfo) return;

    try
    {
        if (FObjInfo->Comm != INT_STATE_NORMAL)
        {
            SetCommErrorStatus();
            return;
        }

        // 통신이 정상 일 경우 해당 상태정보를 업데이트 한다.
        TcxGridDataController *pGDC = (TcxGridDataController *)TvList->DataController;
        try
        {
            int nRow;
            TvList->BeginUpdate();
            pGDC->BeginUpdate();

            pGDC->Values[1][Col00->Index] = String(FObjInfo->Comm);
            pGDC->Values[1][Col01->Index] = String(FObjInfo->Door);
            pGDC->Values[1][Col02->Index] = String(FObjInfo->Fan);
            pGDC->Values[1][Col03->Index] = String(FObjInfo->Heater);

            pGDC->Values[3][Col00->Index] = String(FObjInfo->Camera);
            pGDC->Values[3][Col01->Index] = String(FObjInfo->SetString);
            pGDC->Values[3][Col02->Index] = String(FObjInfo->Battery);
            pGDC->Values[3][Col03->Index] = "";

            if (FObjInfo->Door == INT_STATE_UNKNOWN)
            {
                pGDC->Values[5][Col00->Index] = "-?-";
                pGDC->Values[5][Col01->Index] = "-?-";
                pGDC->Values[5][Col02->Index] = "-?-";
            }
            else
            {
                pGDC->Values[5][Col00->Index] = String(FObjInfo->Temp);
                pGDC->Values[5][Col01->Index] = String(FObjInfo->Voltage);
                pGDC->Values[5][Col02->Index] = String(FObjInfo->VoltageOut);
            }
            //pGDC->Values[5][Col02->Index] = "";
            pGDC->Values[5][Col03->Index] = "";
        }
        __finally
        {
            pGDC->EndUpdate();
            TvList->EndUpdate();
        }
    }
    catch(...)
    {
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
#if 0
    int nRecordIdx = AViewInfo->GridRecord->RecordIndex;
    if (nRecordIdx < 0)
    {
        return;
    }

    try
    {
        int nItemIdx = AViewInfo->Item->Index;
    	if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[nItemIdx])) return;

        String sStatus = AViewInfo->GridRecord->DisplayTexts[nItemIdx];
        sStatus = sStatus.Trim();
        int nStatus = -1;
        if (nRecordIdx == 1)
        {
            if      (sStatus == "0") nStatus = 0;
            else if (sStatus == "1") nStatus = 1;
            else if (sStatus == "2") nStatus = 2;
            else return;
            
            switch(nItemIdx)
            {
            case 0:  // Comm
                if (nStatus == INT_STATE_ERROR)
                    ACanvas->Canvas->Brush->Color = clRed;
                else
                    ACanvas->Canvas->Brush->Color = clLime;
                break;
            case 1:  // Door
                if (nStatus == INT_STATE_CLOSE)
                    ACanvas->Canvas->Brush->Color = clLime;
                else
                if (nStatus == INT_STATE_OPEN)
                    ACanvas->Canvas->Brush->Color = clRed;
                else
                    ACanvas->Canvas->Brush->Color = clSilver;   // status unknown
                break;
            case 2:  // Fan
                if (nStatus == INT_STATE_STOP)
                    ACanvas->Canvas->Brush->Color = clLime;
                else
                if (nStatus == INT_STATE_RUN)
                    ACanvas->Canvas->Brush->Color = clYellow;
                else
                    ACanvas->Canvas->Brush->Color = clSilver;   // status unknown;
                break;
            case 3:  // Heater
                if (nStatus == INT_STATE_STOP)
                    ACanvas->Canvas->Brush->Color = clLime;
                else
                if (nStatus == INT_STATE_RUN)
                    ACanvas->Canvas->Brush->Color = clYellow;
                else
                    ACanvas->Canvas->Brush->Color = clSilver;   // status unknown;
                break;
            } 
            ACanvas->Canvas->Font->Color = ACanvas->Canvas->Brush->Color;
        }
        else
        if (nRecordIdx == 3)
        {
            if      (sStatus == "0") nStatus = 0;
            else if (sStatus == "1") nStatus = 1;
            else if (sStatus == "2") nStatus = 2;
            else return;

            switch(nItemIdx)
            {
            case 0:  // CAM
                if (nStatus == INT_STATE_ERROR)
                    ACanvas->Canvas->Brush->Color = clRed;
                else
                if (nStatus == INT_STATE_NORMAL)
                    ACanvas->Canvas->Brush->Color = clLime;
                else
                    ACanvas->Canvas->Brush->Color = clSilver;   // status unknown;
                break;
            case 1:  // Text
                if (nStatus == INT_STATE_ERROR)
                    ACanvas->Canvas->Brush->Color = clRed;
                else
                if (nStatus == INT_STATE_NORMAL)
                    ACanvas->Canvas->Brush->Color = clLime;
                else
                    ACanvas->Canvas->Brush->Color = clSilver;   // status unknown;
                break;
            case 2:  // Battery
                if (nStatus == INT_STATE_ERROR)
                    ACanvas->Canvas->Brush->Color = clRed;
                else
                if (nStatus == INT_STATE_NORMAL)
                    ACanvas->Canvas->Brush->Color = clLime;
                else
                    ACanvas->Canvas->Brush->Color = clSilver;   // status unknown;
                break;
            }
            ACanvas->Canvas->Font->Color = ACanvas->Canvas->Brush->Color;
        }
        else
        {
            ACanvas->Canvas->Brush->Color = clWhite;
            ACanvas->Canvas->Font->Color = clBlack;
        }
    }
    catch(...)
    {
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TCTV0101M::MnuNetworkPingClick(TObject *Sender)
{
    if (!FObjInfo) return;

    String sCmd = "open";
    String sCmdParam = "ping.exe";

	ShellExecute(NULL, sCmd.c_str(), sCmdParam.c_str(), FObjInfo->CCTV_CTLR_IP.c_str(), NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------

