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

#include <vcl.h>
#pragma hdrstop

#include "FrmVmsCamF.h"
#include "AppGlobalF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FFBaseComponent"
#pragma link "FFBasePlay"
#pragma link "FFPlay"
#pragma resource "*.dfm"

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

//TFrmCctvCam *FrmCctvCam;
//---------------------------------------------------------------------------
__fastcall TFrmVmsCam::TFrmVmsCam(TComponent* Owner)
    : TForm(Owner)
{
    PnlBlob->Visible = false;
    PnlBlob->Align = alClient;
    FDispBlob = false;

    FObj = NULL;

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

    InitCamera(NULL, FAutoPlay);

    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 TFrmVmsCam::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (FObj)
    {
        Caption = (FObj->VMS_CTLR_NMBR) + ": " + FObj->VMS_NM;
        if (FDispBlob == false)
        {
            Connect();
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmVmsCam::FormShow(TObject *Sender)
{
    if (FObj) PnlBack->Caption = FObj->ISTL_LCTN_NM;
    else      PnlBack->Caption = "";

    RefreshCctv();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        Disconnect();
        FObj->FData2 = NULL;
    } catch(...) {}

    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::FormDestroy(TObject *Sender)
{
    try {
        Disconnect();
        FObj->FData2 = NULL;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::RefreshCctv()
{
    FDispBlob = false;
    PnlBlob->Visible = false;

#if 0
    String sQry;

    if (!FObj)
    {
        return;
    }
    sQry = "SELECT CCTV_SBST_DSPL_YN, CCTV_SBST_IMGN \r\n"
           "  FROM TB_CCTV_CTLR                      \r\n"
           " WHERE CCTV_CTLR_ID = :p01               \r\n";

    try
    {
        pADO->Close();
        pADO->SQL->Clear();
        pADO->Connection = ITSDb_GetConnection();
        pADO->SQL->Text = sQry;
        pADO->Parameters->ParamByName("p01")->Value = FObj->CCTV_CTLR_ID;
        pADO->Open();
    }
    catch(EDatabaseError &E)
    {
        PnlBlob->Caption = "대체영상 이미지 정보 읽기 실패";
        ShowMessage(String(E.ClassName()) + E.Message);
        return;
    }

    if (pADO->RecordCount == 0)
    {
        pADO->Close();
        return;
    }

    ForceDirectories(g_sTempDir);

    TADOBlobStream *BS;
    int             nFileSize = 0;
    String          sImgFile  = g_sTempDir + "CCTVB_" + FObj->CCTV_CTLR_ID + ".bmp";

    try {
        DeleteFile(sImgFile);
    } catch(...) {}

    String sDispBlob;
    try
    {
        sDispBlob = pADO->FieldByName("CCTV_SBST_DSPL_YN")->AsString;
        if (sDispBlob == "Y")
        {
            BS = (TADOBlobStream *)pADO->CreateBlobStream(pADO->FieldByName("CCTV_SBST_IMGN"), bmRead);
            //ImgBlob->Picture->Graphic->LoadFromStream(BS);
            BS->SaveToFile(sImgFile);
            nFileSize = BS->Size;
            delete BS;
        }
        pADO->Close();
    }
    catch(Exception &e)
    {
        delete BS;
        PnlBlob->Caption = "대체영상 이미지 스트림 로딩 실패";
        return;
    }

    if (sDispBlob == "Y")
    {
        FDispBlob = true;
        PnlBlob->Visible = true;
        PnlBack->Visible = false;
        if (nFileSize)
        {
            try
            {
                ImgBlob->Picture->LoadFromFile(sImgFile);
                return;
            }
            catch(...)
            {
                ImgBlob->Visible = false;
                PnlBlob->Caption = "대체영상 이미지 파일 로딩 실패";
                return;
            }
        }
        else
        {
            ImgBlob->Visible = false;
            PnlBlob->Caption = "대체영상 이미지 사이즈 오류";
        }
    }
    else
    {
        FDispBlob = false;
        PnlBlob->Visible = false;
        PnlBack->Visible = true;
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::MnuConnectClick(TObject *Sender)
{
    FAutoPlay = true;
    Connect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::MnuDisconnectClick(TObject *Sender)
{
    FAutoPlay = false;
    Disconnect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::Connect()
{
    Disconnect();

    PnlBack->Caption = "Connecting...";

    try
    {
        if (FObj->STRM_SESN_NM != "")
        {
            FFPlayer1->AspectRatio = -1;
            FFPlayer1->TryOpen(FObj->STRM_SESN_NM.Trim(), PnlBack->Handle);
        }
    }
    catch(...)
    {
    }
    FPlay = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::Disconnect()
{
    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(...)
    {
    }

    PnlBack->Caption = "Disconnected";
    FConHandle = 0;
    FPlay = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::InitCamera(void *ACctv, bool AAutoPlay/*=true*/)
{
    FAutoPlay = AAutoPlay;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsCam::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;
    }
}
//---------------------------------------------------------------------------

