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

#include <vcl.h>
#pragma hdrstop

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

#include "VMSMODL0F.h"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#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 "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 "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

#define     WIDTH_GAP   3
#define     HEIGHT_GAP  3

//TVMSMODL0 *VMSMODL0 = NULL;

//---------------------------------------------------------------------------
__fastcall TVMSMODL0::TVMSMODL0(TComponent* Owner, TVmsCtlr *AVmsObj)
    : TForm(Owner)
{
    FVmsObj = AVmsObj;

    FScrollBox   = NULL;

    FSelected    = false;
    FDisplayMode = 0;
    FZoomRate    = 1.0;
    FDispIndex   = 0;
    FPlay        = false;

    FWndParent = NULL;
    FZoomView  = false;

    PnlForm->ParentColor = true;
    //PnlForm->Color       = clBlack;

    FDispW = FVmsObj->WIDTH;
    FDispH = FVmsObj->HEIGHT;
    //PnlName->Caption = "[" + FVmsObj->VMS_CTLR_ID + "] " + FVmsObj->VMS_NM;
    Caption = FVmsObj->VMS_NM;
    PnlName->Caption = FVmsObj->VMS_NM;
    PnlName->Hint = String(FVmsObj->VMS_CTLR_NMBR) + " [" + FVmsObj->VMS_CTLR_ID + "] \r\n" + FVmsObj->VMS_NM;

    FGapW = 16;
    FGapH =  8;

    PnlForm->Align = alClient;

#if 0
    ImgCommError->Parent = ImgCommNormal->Parent;
    ImgCommError->Left = ImgCommNormal->Left;
    ImgCommError->Top  = ImgCommNormal->Top;
    ImgCommError->Visible = false;
    ImgCommNormal->Visible= true;
#else
    ImgState1->Parent = ImgState0->Parent;
    ImgState2->Parent = ImgState0->Parent;
    ImgState1->Left = ImgState0->Left;
    ImgState1->Top  = ImgState0->Top;
    ImgState2->Left = ImgState0->Left;
    ImgState2->Top  = ImgState0->Top;

    ImgState1->Visible = false;
    ImgState2->Visible = false;
#endif

    LoadLocalSkin();

    Application->ProcessMessages();

    FProg = ".";

    TvModStatusList->OptionsView->Header = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::LoadLocalSkin()
{
    PnlMain->ParentColor = false;
    PnlMain->Color       = clBtnFace;

    PnlForm->ParentColor = true;
    //PnlForm->Color       = clBlack;

    PnlDoor->ParentColor = false;
    PnlFan->ParentColor = false;
    PnlHeater->ParentColor = false;
    PnlModule->ParentColor = false;
    PnlPower->ParentColor = false;
    PnlTemp->ParentColor = false;
    PnlOprMode->ParentColor = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::FormDestroy(TObject *Sender)
{
    //CamStop();
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::FormShow(TObject *Sender)
{
    TmrShow->Enabled = true;
    RefreshVmsMessage();
    RefreshVmsStatus();
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::SetFormSize(int AFormW)
{
    InitVmsModule();
    RefreshVmsModuleStatus();
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::SelectForm(bool ASelect)
{
    FSelected = ASelect;
    if (ASelect)
    {
        PopupMenu = PopupMenu1;
        PnlMain->ParentColor = false;
        PnlMain->Color       = clBtnHighlight;//clBackground;
    }
    else
    {
        PopupMenu = NULL;
        PnlMain->ParentColor = false;
        PnlMain->Color       = clBtnFace;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;  

    PopupMenu = NULL;
    if (!FVmsObj)
    {
        PnlName->Caption = "VMS";
    }
    RefreshVmsStatus();
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::SetPlay(bool APlay)
{
    FPlay = APlay;
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::FormClose(TObject *Sender, TCloseAction &Action)
{
    //CamStop();
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::PnlNameClick(TObject *Sender)
{
    if (FScrollBox) FScrollBox->SetFocus();
    if (FWndParent)
    {
        POST_MSG(FWndParent, WM_VMS_SELECT, (WPARAM)FVmsObj, (LPARAM)this);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::OnFormClick(TObject *Sender)
{
    if (FScrollBox) FScrollBox->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::RefreshVmsMessage()
{
    //PnlSeq->Caption = String(FVmsObj->VmsMsg->Total);
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::InitVmsModule()
{
    CMM_ClearGridTableView(TvModStatusList);

    int nVerCnt  = FVmsObj->MODL_ROW_NUM;
    int nHorCnt  = FVmsObj->MODL_COL_NUM;

    int nTotWidth = CxModStatusList->Width;
    int nModSize  = (nTotWidth-(nHorCnt-2)) / nHorCnt;

    CxModStatusList->Visible = false;
    TcxGridDataController *pGDC = (TcxGridDataController *)TvModStatusList->DataController;
    try
    {
        int ii, jj;
        int nRow;
        int nCols = TvModStatusList->ColumnCount;

        TvModStatusList->OptionsView->DataRowHeight = nModSize;
        CxModStatusList->Height = (nVerCnt * nModSize) + 2;

        TvModStatusList->BeginUpdate();
        pGDC->BeginUpdate();

        for (ii = 0; ii < nCols; ii++)
        {
            TvModStatusList->Columns[ii]->Visible = false;
        }
        for (ii = 0; ii < nHorCnt && ii < nCols; ii++)
        {
            TvModStatusList->Columns[ii]->Width   = nModSize;
            TvModStatusList->Columns[ii]->Visible = true;
        }

        for (ii = 0; ii < nVerCnt; ii++)
        {
            nRow = pGDC->AppendRecord();
            for (jj = 0; jj < nHorCnt; jj++)
            {
                if (FVmsObj->STATE.Comm == vms_comm_normal)
                {
                    int bit = CComm_GetBitString(FVmsObj->STATE.ModuleStatus, ii*jj);
                    pGDC->Values[nRow][jj] = (bit == vms_module_state_normal) ? "1" : "0";
                }
                else
                {
                    pGDC->Values[nRow][jj] = "2";
                }
            }
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvModStatusList->EndUpdate();
        CxModStatusList->Visible = true;
    }
    Height = CxModStatusList->Height + 54;
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::RefreshVmsModuleStatus()
{
    int nVerCnt  = FVmsObj->MODL_ROW_NUM;
    int nHorCnt  = FVmsObj->MODL_COL_NUM;

    if (FVmsObj->STATE.Comm == vms_comm_normal)
    {
#if 0
        int nRealVerCnt  = (int)FVmsObj->STATE.ModuleVertical;                               /* 세로 모듈수 */
        int nRealHorCnt  = (int)FVmsObj->STATE.ModuleHorizontal;                             /* 가로 모듈수 */
        if (nRealVerCnt != nVerCnt || nRealHorCnt != nHorCnt)
        {
            FVmsObj->ModVerCnt = nRealVerCnt;
            FVmsObj->ModHorCnt = nRealHorCnt;
            nVerCnt  = FVmsObj->ModVerCnt;
            nHorCnt  = FVmsObj->ModHorCnt;
            InitVmsModule();
        }
#endif
    }

    TcxGridDataController *pGDC = (TcxGridDataController *)TvModStatusList->DataController;
    try
    {
        int ii, jj, nRow;

        TvModStatusList->BeginUpdate();
        pGDC->BeginUpdate();

#if 0
        if (FVmsObj->VMS_CTLR_NMBR == 10005)
        {
            nRow = 1;
        }
#endif

        for (ii = 0; ii < nVerCnt; ii++)
        {
            nRow = ii;
            for (jj = 0; jj < nHorCnt; jj++)
            {
                if (FVmsObj->STATE.Comm == vms_comm_normal)
                {
                    int bit = CComm_GetBitString(FVmsObj->STATE.ModuleStatus, ii*jj);
                    pGDC->Values[nRow][jj] = (bit == vms_module_state_normal) ? "1" : "0";
                }
                else
                {
                    pGDC->Values[nRow][jj] = "2";
                }
            }
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvModStatusList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::RefreshVmsStatus()
{
    PnlDoor->Caption    = VmsModuleDoor(FVmsObj);     //도어
    PnlFan->Caption     = VmsModuleFan(FVmsObj);      //팬
    PnlHeater->Caption  = VmsModuleHeater(FVmsObj);   //히터
    PnlModule->Caption  = VmsModuleState(FVmsObj);    //모듈상태
    PnlPower->Caption   = VmsModulePower(FVmsObj);    //전원상태
    PnlTemp->Caption    = VmsModuleTemp(FVmsObj);     //온도
    PnlOprMode->Caption = VmsOperMode(FVmsObj);

#if 0
    if (FVmsObj->STATE.Comm == vms_comm_normal)
    {
        if (!ImgCommNormal->Visible) ImgCommNormal->Visible = true;
        if ( ImgCommError->Visible)  ImgCommError->Visible  = false;
    }
    else
    {
        if (!ImgCommError->Visible) ImgCommError->Visible  = true;
        if (ImgCommNormal->Visible) ImgCommNormal->Visible = false;
    }
#else
    switch(FVmsObj->RunState)
    {
    case state_normal:
        if (!ImgState0->Visible) ImgState0->Visible = true;
        if ( ImgState1->Visible) ImgState1->Visible = false;
        if ( ImgState2->Visible) ImgState2->Visible = false;
        break;
    case state_module:
        if ( ImgState0->Visible) ImgState0->Visible = false;  //false
        if ( ImgState1->Visible) ImgState1->Visible = false;
        if (!ImgState2->Visible) ImgState2->Visible = true;   //true
        break;
    default:
        if ( ImgState0->Visible) ImgState0->Visible = false;
        if (!ImgState1->Visible) ImgState1->Visible = true;
        if ( ImgState2->Visible) ImgState2->Visible = false;
        break;
    }
#endif

    TColor clrState = clLime;

    //운영모드
    if (FVmsObj->OPER_MODE != "A")
    {
        clrState = clYellow;
    }
    if (PnlOprMode->Color != clrState) PnlOprMode->Color = clrState;

    if (FVmsObj->STATE.Comm != vms_comm_normal)
    {
        PnlDoor->Color   = clGray;  //도어
        PnlFan->Color    = clGray;  //팬
        PnlHeater->Color = clGray;  //히터
        PnlModule->Color = clGray;  //모듈상태
        PnlPower->Color  = clGray;  //전원상태
        PnlTemp->Color   = clGray;  //온도
        return;
    }


    //모듈상태
    clrState = clLime;
    if (FVmsObj->STATE.ModuleState == vms_comm_error)
        clrState = clRed;
    if (PnlModule->Color != clrState) PnlModule->Color = clrState;

    //전원상태
    clrState = clLime;
    if (FVmsObj->STATE.ModulePowerStatus == vms_module_power_off)
        clrState = clRed;
    if (PnlPower->Color != clrState) PnlPower->Color = clrState;

    //도어상태
    clrState = clLime;
    if (FVmsObj->STATE.DoorStatus == vms_door_open)
        clrState = clRed;
    if (PnlDoor->Color != clrState) PnlDoor->Color = clrState;

    //팬상태
    clrState = clLime;
    if (FVmsObj->STATE.FanStatus == vms_fan_on)
        clrState = clYellow;
    if (PnlFan->Color != clrState) PnlFan->Color = clrState;

    //히터상태
    clrState = clLime;
    if (FVmsObj->STATE.HeaterStatus == vms_heater_on)
        clrState = clYellow;
    if (PnlHeater->Color != clrState) PnlHeater->Color = clrState;

    //온도
    clrState = clLime;
    if (PnlTemp->Color != clrState) PnlTemp->Color = clrState;
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::PopupMenu1Popup(TObject *Sender)
{
    MnuVmsInfId->Caption = "[" + FVmsObj->VMS_CTLR_ID + "]";
    MnuCtlrPing->Caption = FrmLang->lblText2->Caption + " - " + FVmsObj->VMS_CTLR_IP;//"제어기 Ping - " +  FVmsObj->VMS_CTLR_IP;
    if (FVmsObj->WEB_CMRA_IP == "")
    {
        MnuWebcamPing->Caption = FrmLang->lblText3->Caption;//"웹카메라 Ping";
        MnuWebcamPing->Enabled = false;
    }
    else
    {
        MnuWebcamPing->Caption = FrmLang->lblText3->Caption + " - " + FVmsObj->WEB_CMRA_IP;//"웹카메라 Ping - " +  FVmsObj->WEB_CMRA_IP;
        MnuWebcamPing->Enabled = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::MnuNetworkPingClick(TObject *Sender)
{
    String sIp = FVmsObj->VMS_CTLR_IP;

    TMenuItem *pMenu = (TMenuItem*)Sender;
    if (pMenu->Tag == 1)
    {
        sIp = FVmsObj->WEB_CMRA_IP;
    }

    ITSUtil_NetworkPing(sIp);
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::CamPlay()
{
}
//---------------------------------------------------------------------------

void __fastcall TVMSMODL0::CamStop()
{
}
//---------------------------------------------------------------------------


void __fastcall TVMSMODL0::TvModStatusListCustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    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];
        if (sStatus == "1")
        {
            ACanvas->Canvas->Brush->Color = clLime;
        }
        else
        if (sStatus == "0")
        {
            ACanvas->Canvas->Brush->Color = clRed;
        }
        else
        {
            ACanvas->Canvas->Brush->Color = clSilver;
        }

        ACanvas->Canvas->Font->Color = ACanvas->Canvas->Brush->Color;
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

