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

#include <vcl.h>
#pragma hdrstop

#include "FrmAcesVmsCamF.h"
#include "AppGlobalF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma resource "*.dfm"
//TFrmAcesVmsCam *FrmAcesVmsCam;
//---------------------------------------------------------------------------
__fastcall TFrmAcesVmsCam::TFrmAcesVmsCam(TComponent* Owner)
    : TForm(Owner)
{
    PnlBlob->Visible = false;
    PnlBlob->Align = alClient;
    FDispBlob = false;

    FObj = NULL;

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

    //AcesTechXPlayer21->DoubleBuffered = true;

    InitCamera(NULL, FAutoPlay);
}
//---------------------------------------------------------------------------
void __fastcall TFrmAcesVmsCam::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (FObj)
    {
        Caption = String(FObj->VMS_CTLR_NMBR) + ": " + FObj->VMS_NM;
        if (FDispBlob == false)
        {
            Connect();
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmAcesVmsCam::FormShow(TObject *Sender)
{
    if (FObj) PnlBack->Caption = FObj->ISTL_LCTN_NM;
    else      PnlBack->Caption = "";

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

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

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

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

void __fastcall TFrmAcesVmsCam::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 TFrmAcesVmsCam::MnuConnectClick(TObject *Sender)
{
    FAutoPlay = true;
    Connect();
}
//---------------------------------------------------------------------------

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

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

    PnlBack->Caption = "연결중...";
    AcesTechXPlayer21->Visible = true;

    try
    {
        if (FObj->STRM_SESN_NM != "")
        {
            AcesTechXPlayer21->URL = FObj->STRM_SESN_NM;
            FConHandle = AcesTechXPlayer21->ConnectAsync();
        }
    }
    catch(...)
    {
    }
    FPlay = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmAcesVmsCam::Disconnect()
{
    if (!FPlay) return;

    try
    {
        //if (FConHandle)
        {
            AcesTechXPlayer21->Close();
        }
        FConHandle = 0;
    }
    catch(...)
    {
    }

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

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

