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

#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 "VMSWCAM0F.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 "cxLabel"
#pragma link "FFBaseComponent"
#pragma link "FFBasePlay"
#pragma link "FFPlay"
#pragma resource "*.dfm"

#define     WIDTH_GAP   3
#define     HEIGHT_GAP  3

#define LICENSE_KEY "FSXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX"

//TVMSWCAM0 *VMSWCAM0 = NULL;

//---------------------------------------------------------------------------
__fastcall TVMSWCAM0::TVMSWCAM0(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;

    this->ParentColor = false;
    PnlCamera->ParentColor = false;
    PnlCamera->Color       = clBlack;

    if (FVmsObj)
    {
        Caption = FVmsObj->VMS_NM;
        PnlName->Caption = Caption;
        PnlName->Hint = String(FVmsObj->VMS_CTLR_NMBR) + " [" + FVmsObj->VMS_CTLR_ID + "] \r\n" + FVmsObj->VMS_NM;
        PopupMenu = PopupMenu1;
    }
    else
    {
        Caption = "Empty";
        PnlName->Caption = Caption;
        PnlName->Hint = Caption;
    }

    PnlCamera->Align = alClient;

    LoadLocalSkin();

    Application->ProcessMessages();

    FPlay = false;
    FAutoPlay = true;
    FConHandle = 0;
    FInstalled = false;

    String sAVILibDir = g_sAppDir + "LibAV";
    FFPlayer1->SetLicenseKey(LICENSE_KEY);
    FFPlayer1->DisableFPUExceptions();
    if (!FFPlayer1->LoadAVLib(sAVILibDir))
    {
#if 0
        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Stream display library load failed.", L"Stream display fail", MB_OK|MB_ICONERROR|MB_APPLMODAL);
        else                        Application->MessageBox(L"영상표출 라이브러리를 로드하지 못하였습니다.", L"영상표출 오류", MB_OK|MB_ICONERROR|MB_APPLMODAL);
#endif
    }
}
//---------------------------------------------------------------------------

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

    PnlCamera->ParentColor = false;
    PnlCamera->Color       = clBlack;
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0::FormDestroy(TObject *Sender)
{
    TmrPlay->Enabled = false;
    TmrStop->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0::InitCamera(void *ACctv, bool AAutoPlay/*=true*/)
{
    FVmsObj = (TVmsCtlr*)ACctv;

    FAutoPlay = AAutoPlay;

    if (FVmsObj)
    {
        Caption          = " " + FVmsObj->VMS_NM + " ";
        PnlName->Caption = Caption;
        PnlName->Hint    = String(FVmsObj->VMS_CTLR_NMBR) + " [" + FVmsObj->VMS_CTLR_ID + "] \r\n" + FVmsObj->VMS_NM;
        PopupMenu        = PopupMenu1;
        FInstalled       = true;
        PopupMenu = PopupMenu1;
    }
    else
    {
        Caption          = " Empty ";
        PnlName->Caption = Caption;
        PnlName->Hint    = Caption;
        FInstalled       = false;
        PopupMenu = NULL;

        PnlCamera->Caption = "Disconnected";
    }
    DisplayName(Caption);
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0::DisplayName(String AName)
{
    LblName->Visible = false;
    LblName->Caption = AName;
    LblName->Transparent = false;
    LblName->Transparent = true;
    LblName->Visible = true;
    LblName->Refresh();
    LblName->BringToFront();

    //::SetWindowLong(LblName->Handle, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
    //::SetLayeredWindowAttributes(LblName->Handle, 0, 80, LWA_ALPHA);
//  long            __fastcall DrawText(BSTR text, long x, long y, BSTR fontname, long fontsize,
//                                      long underline, long fontcolor, long bordercolor, long bkcolor);
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0::FormShow(TObject *Sender)
{
    RefreshVmsStatus();

    DisplayName(Caption);
}
//---------------------------------------------------------------------------

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

void __fastcall TVMSWCAM0::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::SetPlay(bool APlay)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = false;

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

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

void __fastcall TVMSWCAM0::LblNameDblClick(TObject *Sender)
{
    if (FWndParent)
    {
        if (FVmsObj)
        {
            POST_MSG(FWndParent, WM_VMS_SELECT, (WPARAM)FVmsObj, (LPARAM)this);
        }
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVMSWCAM0::RefreshVmsMessage()
{
}
//---------------------------------------------------------------------------
void __fastcall TVMSWCAM0::RefreshVmsStatus()
{
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0::PopupMenu1Popup(TObject *Sender)
{
    if (FVmsObj)
    {
        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 TVMSWCAM0::MnuNetworkPingClick(TObject *Sender)
{
    if (FVmsObj)
    {
        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::CamPlay()
{
    DisplayName(Caption);

    if (!FVmsObj) return;

    CamStop();

    if (!FVmsObj) return;

    if (FVmsObj->STRM_SESN_NM.IsEmpty())
    {
        //PnlCamera->Caption = "스트리밍주소 오류...";
        PnlCamera->Caption = "Streamming address error...";
    }
    else
    {
        //PnlCamera->Caption = "연결중...";
        PnlCamera->Caption = "Connecting...";
        try
        {
            FFPlayer1->AspectRatio = -1;
            FFPlayer1->TryOpen(FVmsObj->STRM_SESN_NM.Trim(), PnlCamera->Handle);
        }
        catch(...)
        {
        }
    }

    DisplayName(Caption);
    FPlay = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSWCAM0::CamStop()
{
    if (!FVmsObj) return;

    if (!FPlay) return;

    try
    {
        if (FFPlayer1->ScreenHandle != NULL)
        {
            FFPlayer1->Stop(true);
            PostMessage(FFPlayer1->ScreenHandle, CM_INVALIDATE, 0, 0);
            UpdateWindow(FFPlayer1->ScreenHandle);
            FFPlayer1->ScreenHandle = NULL;
        }
        FConHandle = 0;
    }
    catch(...)
    {
    }

    if (FVmsObj->STRM_SESN_NM.IsEmpty())
    {
        //PnlCamera->Caption = "스트리밍주소 오류...";
        PnlCamera->Caption = "Streamming address error...";
    }
    else
    {
        PnlCamera->Caption = "Disconnected";
    }
    FConHandle = 0;
    DisplayName(Caption);
    FPlay = false;
}
//---------------------------------------------------------------------------

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

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

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

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

void __fastcall TVMSWCAM0::FFPlayer1State(TObject *Sender, TPlayState APlayState)
{
    switch(APlayState)
    {
    case Ffbasecomponent::psPlay:
    case Ffbasecomponent::psResume:
        break;
    case Ffbasecomponent::psPause:
    case Ffbasecomponent::psStep:
        break;
    case Ffbasecomponent::psStop:
        if (FFPlayer1->ScreenHandle != NULL)
        {
            PostMessage(FFPlayer1->ScreenHandle, CM_INVALIDATE, 0, 0);
            UpdateWindow(FFPlayer1->ScreenHandle);
            FFPlayer1->ScreenHandle = NULL;
        }
        break;
    case Ffbasecomponent::psEnd:
        {
            FFPlayer1->Stop(true);
            PostMessage(FFPlayer1->ScreenHandle, CM_INVALIDATE, 0, 0);
            UpdateWindow(FFPlayer1->ScreenHandle);
            FFPlayer1->ScreenHandle = NULL;
        }
        break;
    }
}
//---------------------------------------------------------------------------

