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

#include <vcl.h>
#pragma hdrstop

#include "FrmAcesCamF.h"
#include "AppGlobalF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma resource "*.dfm"
//TFrmAcesCam *FrmAcesCam;
//---------------------------------------------------------------------------
__fastcall TFrmAcesCam::TFrmAcesCam(TComponent* Owner)
    : TForm(Owner)
{
    FObj.Installed = false;
    FObj.IsCctv    = false;
    FObj.Data      = NULL;

    PnlBlob->Visible = false;
    PnlBlob->Align = alClient;
    FDispBlob = false;

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

    //AcesTechXPlayer21->DoubleBuffered = true;

    InitCamera(NULL, FAutoPlay);
}
//---------------------------------------------------------------------------
void __fastcall TFrmAcesCam::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (FObj.Installed)
    {
        Caption = FObj.ID + ": " + FObj.NAME;
        if (FDispBlob == false)
        {
            Connect();
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmAcesCam::FormShow(TObject *Sender)
{
    if (FObj.Installed) PnlBack->Caption = FObj.NAME;
    else                PnlBack->Caption = "";

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

void __fastcall TFrmAcesCam::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        Disconnect();
        if (FObj.Data) *FObj.Data = NULL;
    } catch(...) {}

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

void __fastcall TFrmAcesCam::FormDestroy(TObject *Sender)
{
    try {
        Disconnect();
        if (FObj.Data) *FObj.Data = NULL;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmAcesCam::RefreshPlayer()
{
    FDispBlob = false;
    PnlBlob->Visible = false;

    if (FObj.IsCctv == false) return;

    String sQry;

    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.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.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;
    }
}
//---------------------------------------------------------------------------

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

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

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

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

    try
    {
        AcesTechXPlayer21->URL = FObj.RTSP_ADDR;
        FConHandle = AcesTechXPlayer21->ConnectAsync();
    }
    catch(...)
    {
    }
    FPlay = true;
}
//---------------------------------------------------------------------------

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

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

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

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

