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

#include <vcl.h>
#pragma hdrstop

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

#include "VMSWCAM0_OLDF.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 "ITSVmsDispF"
#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 "AXVLC_TLB"
#pragma resource "*.dfm"

#define     WIDTH_GAP   3
#define     HEIGHT_GAP  3

//TVMSWCAM0 *VMSWCAM0 = NULL;

//---------------------------------------------------------------------------
__fastcall TVMSWCAM0_OLD::TVMSWCAM0_OLD(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 = false;
    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 = ".";
    PnlStatus->Align = alClient;
    PnlStatus->Color = clBlack;
    PnlMsg->Color    = clBlack;

    //VLCPlugin21->FullscreenEnabled = false;
    VLCPlugin21->Toolbar = false;
    VLCPlugin21->Align   = alClient;
    VLCPlugin21->Visible = true;
}
//---------------------------------------------------------------------------

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

    PnlForm->ParentColor = false;
    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 TVMSWCAM0_OLD::FormDestroy(TObject *Sender)
{
    TmrPlay->Enabled = false;
    TmrStop->Enabled = false;
    //CamStop();
}
//---------------------------------------------------------------------------

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

void __fastcall TVMSWCAM0_OLD::SetFormSize(int AFormW)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::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 TVMSWCAM0_OLD::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;  

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

void __fastcall TVMSWCAM0_OLD::SetPlay(bool APlay)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = false;

    FPlay = APlay;
    if (FPlay)
    {
        TmrPlay->Enabled = true;
    }
    else
    {
        TmrStop->Enabled = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::FormClose(TObject *Sender, TCloseAction &Action)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = false;
    //CamStop();
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TVMSWCAM0_OLD::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->WSTATE.Comm == vms_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;
        if ( ImgState1->Visible) ImgState1->Visible = false;
        if (!ImgState2->Visible) ImgState2->Visible = 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->WSTATE.Comm != vms_normal)
    {
        PnlDoor->Color   = clGray;  //도어
        PnlFan->Color    = clGray;  //팬
        PnlHeater->Color = clGray;  //히터
        PnlModule->Color = clGray;  //모듈상태
        PnlPower->Color  = clGray;  //전원상태
        PnlTemp->Color   = clGray;  //온도
        return;
    }


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

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

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

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

    //전원상태
    clrState = clLime;
    if (FVmsObj->WSTATE.ModulePowerStatus == vms_on)
        clrState = clYellow;
    if (PnlPower->Color != clrState) PnlPower->Color = clrState;

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

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

void __fastcall TVMSWCAM0_OLD::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 TVMSWCAM0_OLD::CamPlay()
{
    //CamStop();

AnsiString sTrace = "CamPlay: " + Caption;
ITSUtil_Trace(sTrace.c_str());

    try {
        OleVariant ovar1 = OleVariant("active");
        OleVariant ovar2 = OleVariant(":noaudio");
        int        res;

        int nPlayLists;
        VLCPlugin21->playlist->Get_itemCount(nPlayLists);
        if (nPlayLists > 0)
        {
            try {
                //if (VLCPlugin21->playlist->isPlaying)
                {
                    VLCPlugin21->playlist->stop();
                }
            } catch(...) {}
        }
        VLCPlugin21->playlist->clear();

        VLCPlugin21->AutoPlay = true;

        String sUrl;
        sUrl = FVmsObj->STRM_SESN_NM;
        VLCPlugin21->playlist->add(sUrl, ovar1, ovar2, res);


        VLCPlugin21->MRL = sUrl;
        VLCPlugin21->playlist->playItem(0);

        FProg = ".";
        PnlMsg->Caption    = " Ready...";
        PnlStatus->Caption = Caption;
        PnlStatus->Visible = true;

        VLCPlugin21->playlist->play();
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::CamStop()
{
AnsiString sTrace = "CamStop: " + Caption;
ITSUtil_Trace(sTrace.c_str());

    try {
        PnlMsg->Caption    = " Ready...";
        //PnlStatus->Caption = "CCTV를 선택하세요.";
        PnlStatus->Visible = true;

        //if (VLCPlugin21->playlist->isPlaying)
        {
            VLCPlugin21->playlist->stop();
        }
        VLCPlugin21->playlist->clear();
        VLCPlugin21->AutoPlay = false;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21MediaPlayerBuffering(TObject *ASender, int cache)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption    = " PlayerBuffering " + FProg;
        FProg = FProg + ".";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21MediaPlayerTimeChanged(TObject *ASender, int Time)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption    = " PlayerTimeChanged";
        PnlStatus->Visible = false;
        FProg = ".";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21MediaPlayerOpening(TObject *Sender)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption    = " PlayerOpening";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21MediaPlayerPaused(TObject *Sender)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption    = " PlayerPaused";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21MediaPlayerPlaying(TObject *Sender)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption    = " PlayerPlaying";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21MediaPlayerStopped(TObject *Sender)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption    = " PlayerStopped";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21play(TObject *Sender)
{
    if (PnlStatus->Visible)
    {
        //PnlMsg->Caption    = " PlayerPlay";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::VLCPlugin21stop(TObject *Sender)
{
    if (PnlStatus->Visible)
    {
        PnlMsg->Caption = " PlayerStop";
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::MnuPlayClick(TObject *Sender)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::MnuStopClick(TObject *Sender)
{
    TmrStop->Enabled = true;
    TmrPlay->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::TmrPlayTimer(TObject *Sender)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = false;
    CamPlay();
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0_OLD::TmrStopTimer(TObject *Sender)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = false;
    CamStop();
}
//---------------------------------------------------------------------------

