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

#include <vcl.h>
#pragma hdrstop

#include "FrmCctvCam_OLDF.h"
#include "AppGlobalF.h"
#include "rtspPlayer1F.h"

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

    FObj = NULL;
    FPalyerForm = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TFrmCctvCam_OLD::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (FObj)
    {
        Caption = FObj->CCTV_CTLR_ID + ": " + FObj->ISTL_LCTN_NM;
        if (FDispBlob == false)
        {
            CamPlay();
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmCctvCam_OLD::FormShow(TObject *Sender)
{
    if (FObj) PnlBack->Caption = FObj->ISTL_LCTN_NM;
    else      PnlBack->Caption = "";

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

void __fastcall TFrmCctvCam_OLD::CamPlay()
{
    if (!FObj) return;

    FPalyerForm = rtspPlayer1_create(this, PnlBack, FObj->STRM_SESN_NM, FObj->CCTV_CTLR_ID, FObj->ISTL_LCTN_NM);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCam_OLD::CamStop()
{
    if (FPalyerForm)
    {
        rtspPlayer1_stop(FPalyerForm);
        FPalyerForm = NULL;
    }
    if (!FObj) return;
}
//---------------------------------------------------------------------------

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

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

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

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

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

