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

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

#include "FrmFullScreenF.h"
#include "FrmCrsStreamPlayer2F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FFBaseComponent"
#pragma link "FFBasePlay"
#pragma link "FFPlay"
#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 link "dxSkinMcSkin"
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma resource "*.dfm"

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

//TFrmCrsStreamPlayer *FrmCrsStreamPlayer;
//---------------------------------------------------------------------------
__fastcall TFrmCrsStreamPlayer2::TFrmCrsStreamPlayer2(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    for (int ii = 0; ii < MAX_CRSCAM; ii++)
    {
        FObj.info[ii].Installed = false;
        FObj.info[ii].PnlStream = (TPanel*)FindComponent("PnlCam"+IntToStr(ii+1));
        FObj.info[ii].PnlTitle = (TPanel*)FindComponent("PnlTitle"+IntToStr(ii+1));
        FObj.info[ii].Cmra = NULL;
        FObj.info[ii].FConHandle = 0;
        FObj.info[ii].Timer = (TTimer*)FindComponent("Timer"+IntToStr(ii+1));;
#if FFPLAY
        FObj.info[ii].FFPlayer = (TFFPlayer*)FindComponent("FFPlayer"+IntToStr(ii+1));
        FObj.info[ii].AcesTechXPlayer = (TAcesTechXPlayer2*)FindComponent("AcesTechXPlayer2"+IntToStr(ii+1));
        if (FObj.info[ii].AcesTechXPlayer)
        {
            FObj.info[ii].AcesTechXPlayer->Visible = false;
        }

        FObj.info[ii].FFPlayer->SetLicenseKey(LICENSE_KEY);
        FObj.info[ii].FFPlayer->DisableFPUExceptions();
#endif

#if ACES
        FObj.info[ii].AcesTechXPlayer = (TAcesTechXPlayer2*)FindComponent("AcesTechXPlayer2"+IntToStr(ii+1));
        FObj.info[ii].AcesTechXPlayer->Align = alClient;
#endif

        FObj.info[ii].PnlStream->Tag = (int)this;
        FObj.info[ii].PnlTitle->Caption = "---";

        //FObj.info[ii].PnlStream->Visible = false;
    }

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

    FPlay = false;
    FAutoPlay = true;

    FFullScreen = false;
    InitCamera(NULL, FAutoPlay);

    FParent = this->Parent;
}
//---------------------------------------------------------------------------
void __fastcall TFrmCrsStreamPlayer2::TmrShowTimer(TObject *Sender)
{
    TTimer *Timer = (TTimer*)Sender;
    int idx = (int)Timer->Tag;
    FObj.info[idx].Timer->Enabled = false;
    //if (FObj.Installed)
    {
        //Caption = FObj.ID + ": " + FObj.NAME;
        //if (FDispBlob == false)
        {
            Connect(idx);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmCrsStreamPlayer2::FormShow(TObject *Sender)
{
    Refresh();
    this->FLastBounds = this->BoundsRect;

    if (FCross == NULL)
    {
        return;
    }

    int camCnt = 0;
    FOR_STL(TCrossCam *, pCrsCam, FCross->FLists)
    {
        FObj.info[camCnt].Installed = true;
        FObj.info[camCnt].Cmra = pCrsCam;
        FObj.info[camCnt].PnlTitle->Caption = pCrsCam->CAM_NM;

        camCnt++;
        if (camCnt >= MAX_CRSCAM)
        {
            break;
        }
    }


#if FFPLAY
    String sAVILibDir = g_sAppDir + "LibAV";
    for (int ii = 0; ii < MAX_CRSCAM; ii++)
    {
        if (FObj.info[ii].Installed == false)
        {
            continue;
        }

        if (!FObj.info[ii].FFPlayer->AVLibLoaded())
        {
            if (!FObj.info[ii].FFPlayer->LoadAVLib(sAVILibDir))
            {
                Application->MessageBox(lblErr1->Caption.c_str(),//L"영상표출 라이브러리를 로드하지 못하였습니다.",
                                        lblErr2->Caption.c_str(),//L"영상표출 오류",
                                        MB_OK|MB_ICONERROR|MB_APPLMODAL);
                return;
            }
        }
    }
#endif

    for (int ii = 0; ii < MAX_CRSCAM; ii++)
    {
        if (FObj.info[ii].Installed)
        {
            if (FObj.info[ii].PnlStream->Visible == false) FObj.info[ii].PnlStream->Visible = true;
            FObj.info[ii].Timer->Enabled = true;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsStreamPlayer2::FormClose(TObject *Sender, TCloseAction &Action)
{
    ReleaseCamera();

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

void __fastcall TFrmCrsStreamPlayer2::FormDestroy(TObject *Sender)
{
    ReleaseCamera();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsStreamPlayer2::ReleaseCamera()
{
    try {
        for (int ii = 0; ii < MAX_CRSCAM; ii++)
        {
            if (FObj.info[ii].Installed)
            {
                Disconnect(ii);
            }
        }

        if (FCross) FCross->FData1 = NULL;
    } catch(...) {}
}
//---------------------------------------------------------------------------

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

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

void __fastcall TFrmCrsStreamPlayer2::Connect(int AIdx)
{
    Disconnect(AIdx);

    try
    {
        CRSCAM_PLAYER_INFO *pInfo = &FObj.info[AIdx];

        if (pInfo->Installed == false)
        {
            return;
        }

        pInfo->PnlStream->Caption = "Connecting...";
        pInfo->PnlTitle->Caption = pInfo->Cmra->CAM_NM;

#if FFPLAY
        TFFPlayer *FFPlayer = pInfo->FFPlayer;
        FFPlayer->AspectRatio = -1;
        FFPlayer->TryOpen(pInfo->Cmra->RTSP_URL, pInfo->PnlStream->Handle);
        FConHandle = (long)FFPlayer->ScreenHandle;
#endif
#if ACES
        pInfo->AcesTechXPlayer->URL = pInfo->Cmra->RTSP_URL;
        pInfo->FConHandle = pInfo->AcesTechXPlayer->ConnectAsync();
        if (pInfo->FConHandle > 0)
        {
        }
#endif
        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }
    FPlay = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsStreamPlayer2::Disconnect(int AIdx)
{
    if (!FPlay) return;

    FPlay = false;

    try
    {
        CRSCAM_PLAYER_INFO *pInfo = &FObj.info[AIdx];

        if (pInfo->Installed == false)
        {
            return;
        }

#if FFPLAY
        TFFPlayer *FFPlayer = pInfo->FFPlayer;
        if (FFPlayer->ScreenHandle != NULL)
        {
            FFPlayer->Stop(true);
            PostMessage(FFPlayer->ScreenHandle, CM_INVALIDATE, 0, 0);
            UpdateWindow(FFPlayer->ScreenHandle);
            FFPlayer->ScreenHandle = NULL;
        }
#endif
#if ACES
        if (pInfo->FConHandle > 0)
        {
            pInfo->AcesTechXPlayer->Close();
        }
#endif
        pInfo->FConHandle = 0;
        pInfo->PnlStream->Caption = "Disconnected";

        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }

    FPlay = false;
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmCrsStreamPlayer2::FFPlayer1State(TObject *Sender, TPlayState APlayState)
{
enum TPlayState { psPlay, psPause, psResume, psStep, psStop, psEnd };

    if (APlayState == Ffbasecomponent::psStop || APlayState == Ffbasecomponent::psEnd)
    {
        if (FPlay)
        {
            TFFPlayer *Player = (TFFPlayer*)Sender;
            int idx = (int)Player->Tag;
            FObj.info[idx].PnlTitle->Caption = FObj.info[idx].Cmra->CAM_NM + " : Disconnected";
            FObj.info[idx].Timer->Enabled = true;
        }
    }
}
//---------------------------------------------------------------------------

