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

#include <vcl.h>
#include "ITSDbF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "FrmCctvImgF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma resource "*.dfm"
TFrmCctvImg *FrmCctvImg;
//---------------------------------------------------------------------------
__fastcall TFrmCctvImg::TFrmCctvImg(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
}
//---------------------------------------------------------------------------

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

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

void __fastcall TFrmCctvImg::FormShow(TObject *Sender)
{
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvImg::LoadStopImage()
{
    PnlBack->Caption = "";
    if (LoadDbImage())
    {
        ImgStop->Visible = true;
    }
    else
    {
        ImgStop->Visible = false;
    }
}
//---------------------------------------------------------------------------

bool __fastcall TFrmCctvImg::LoadDbImage()
{
    bool bImage = false;
    String sQry;

    if (!FObj)
    {
        return false;
    }
    sQry = "SELECT A.CCTVID, A.COLLDT, A.IMG     \r\n"
           "  FROM CCTVIMG_BLOB A,               \r\n"
           "       (SELECT CCTVID,               \r\n"
           "               MAX(COLLDT) AS COLLDT \r\n"
           "          FROM CCTVIMG_BLOB          \r\n"
           "         WHERE CCTVID = :p01         \r\n"
           "         GROUP BY CCTVID) B          \r\n"
           " WHERE A.CCTVID = B.CCTVID           \r\n"
           "   AND A.COLLDT = B.COLLDT           \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->Prepared = true;
        pADO->Open();
        bImage = true;
    }
    catch(EDatabaseError &E)
    {
        PnlBack->Caption = lblErr1->Caption;//"정지영상 정보 읽기 실패";
        ShowMessage(String(E.ClassName()) + E.Message);
        return false;
    }
    
    if (pADO->RecordCount == 0)
    {
        pADO->Close();
        PnlBack->Caption = lblErr2->Caption;//"정지영상 정보가 없습니다";
        return false;
    }
    
    ForceDirectories(g_sTempDir);

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

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

    try
    {
        BS = (TADOBlobStream *)pADO->CreateBlobStream(pADO->FieldByName("IMG"), bmRead);
        BS->SaveToFile(sImgFile);
        nFileSize = BS->Size;
        pADO->Close();
    }
    catch(Exception &e)
    {
        delete BS;
        PnlBack->Caption = lblErr3->Caption;//"정지영상 스트림 로딩 실패";
        return false;
    }

    delete BS;
    if (nFileSize)
    {
        try 
        {
            ImgStop->Picture->LoadFromFile(sImgFile);
            return true;
        }
        catch(...) 
        {
            PnlBack->Caption = lblErr4->Caption;//"정지영상 파일 로딩 실패";
            return false;
        }
    }
    else
    {
        PnlBack->Caption = lblErr5->Caption;//"정지영상 이미지 사이즈 오류";
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvImg::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (FObj)
    {
        Caption = lblErr6->Caption/*"정지영상*/ + ": " + FObj->CCTV_CTLR_ID + ": " + FObj->ISTL_LCTN_NM;
        LoadStopImage();
    }
}
//---------------------------------------------------------------------------

