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

#include <vcl.h>
#include "AppGlobalF.h"
#include "FrmCameraPlayerF.h"
#include "FrmCameraFullScreenF.h"
#include "FrmCameraInfoF.h"
#include "FrmCameraScreenF.h"
#pragma hdrstop

//---------------------------------------------------------------------------
#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 "AcesTechXPlayer2Lib_OCX"
#pragma resource "*.dfm"

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

#define FFPLAYER    0
#define ACES        1

TFrmCameraPlayer *FrmCameraPlayer = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmCameraPlayer::TFrmCameraPlayer(TComponent* Owner)
    : TForm(Owner)
{
    FPlay = false;
    FAutoPlay = true;
    FConHandle = 0;

    FFullScreen = false;

    FParent = this->Parent;
    PnlCamera->Tag = (int)this;

#if FFPLAYER
    AcesTechXPlayer21->Visible = false;

    FFPlayer1->SetLicenseKey(LICENSE_KEY);
    FFPlayer1->DisableFPUExceptions();

    if (!FFPlayer1->AVLibLoaded())
    {
        String sAVILibDir = g_sAppDir + "LibAV";
        if (!FFPlayer1->LoadAVLib(sAVILibDir))
        {
            if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Stream display library load failed.", L"Stream display fail", MB_OK|MB_ICONERROR|MB_APPLMODAL);
            else                        Application->MessageBox(L"영상표출 라이브러리를 로드하지 못하였습니다.", L"영상표출 오류", MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
#endif
#if ACES
#endif

    if (g_AppCfg.sLang != "kr")
    {
        MnuConnect->Caption = "Connect";
        MnuDisconnect->Caption = "Disconnect";
        MnuInfo->Caption = "Information...";
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmCameraPlayer::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (Installed)
    {
        PopupMenu = PopupMenu1;
        ImgNoData->Visible = false;
        PnlName->Caption = CCAM_ID + ": " + NAME;
        Play();
    }
    else
    {
        PopupMenu = NULL;
        PnlCamera->Caption = NAME;
        LoadEmptyImage();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::LoadEmptyImage()
{
    try
    {
        String sImageFile = g_AppCfg.sEmptyImg;
        if (sImageFile != "")
        {
            ImgNoData->Picture->LoadFromFile(sImageFile);
            ImgNoData->Align = alClient;
            ImgNoData->Visible = true;
        }
    }
    catch(Exception &e)
    {
        ImgNoData->Visible = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::FormShow(TObject *Sender)
{
    Refresh();
    this->FLastBounds = this->BoundsRect;
//    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        Stop();
    } catch(...) {}

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

void __fastcall TFrmCameraPlayer::FormDestroy(TObject *Sender)
{
    try {
        Stop();
    } catch(...) {}
}
//---------------------------------------------------------------------------

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

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

void __fastcall TFrmCameraPlayer::AutoPlay()
{
    FAutoPlay = true;
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFrmCameraPlayer::Play()
{
    Stop();

    PnlCamera->Caption = "Try connect...";

    try
    {
#if FFPLAYER
        FFPlayer1->AspectRatio = -1;
        FFPlayer1->TryOpen(STRM_ADDR, PnlCamera->Handle);
        FConHandle = (long)FFPlayer1->ScreenHandle;
#endif
#if ACES
        AcesTechXPlayer21->URL = STRM_ADDR;
        FConHandle = AcesTechXPlayer21->ConnectAsync();
        if (FConHandle > 0)
        {
        }
#endif
        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }
    FPlay = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::Stop()
{
    if (!FPlay) return;

    FPlay = false;
    PnlCamera->Caption = "Try disconnect";
    try
    {
#if FFPLAYER
        if (FFPlayer1->ScreenHandle != NULL)
        {
            FFPlayer1->Stop(true);
            PostMessage(FFPlayer1->ScreenHandle, CM_INVALIDATE, 0, 0);
            UpdateWindow(FFPlayer1->ScreenHandle);
        }
#endif
#if ACES
        if (FConHandle > 0)
        {
            AcesTechXPlayer21->Close();
        }
#endif
        Application->ProcessMessages();
        FConHandle = 0;
    }
    catch(Exception &e)
    {
    }

    PnlCamera->Caption = "Disconnected";
    FConHandle = 0;
    FPlay = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::InitCamera(bool AInstalled, String ACtlrNmbr, String AName, String AStrmAddr, String AFullStrmAddr, int AViewMode)
{
    FAutoPlay      = true;
    Installed      = AInstalled;
    CCAM_ID        = ACtlrNmbr;
    NAME           = AName;
    STRM_ADDR      = AStrmAddr;
    FULL_STRM_ADDR = AFullStrmAddr;
    VIEW_MODE      = AViewMode;

    Caption = NAME;
    PnlName->Caption = NAME;
    PnlCamera->Caption = AStrmAddr;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::PnlCameraDblClick(TObject *Sender)
{
    // 여기서 더블클릭은 최대화면을 호출하는 것임
#if 0
    TFrmCameraFullScreen *pFrmFullScreen = new TFrmCameraFullScreen(this);
    pFrmFullScreen->BoundsRect = this->Monitor->BoundsRect;
    pFrmFullScreen->PnlStream->Align = alNone;
    pFrmFullScreen->PnlStream->Align = alClient;

    pFrmFullScreen->RTSP_ADDR = FULL_STRM_ADDR;

    pFrmFullScreen->ShowModal();
    PnlCamera->Parent = this;
    PnlCamera->Tag = (int)this;

    delete pFrmFullScreen;
    pFrmFullScreen = NULL;
#else
#if FFPLAYER
    FrmCameraScreen->TmrFullScreen->Enabled = false;
    FrmCameraScreen->FFullScreenIdx = Tag;
    FrmCameraScreen->TmrFullScreen->Enabled = true;
#endif
#endif
}
//---------------------------------------------------------------------------

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

    if (APlayState == Ffbasecomponent::psStop || APlayState == Ffbasecomponent::psEnd)
    {
        if (FPlay)
        {
            Caption = Caption + " : Disconnected";
            TmrShow->Enabled = true;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraPlayer::MnuInfoClick(TObject *Sender)
{
    FrmCameraInfo = new TFrmCameraInfo(this);
    FrmCameraInfo->Edit1->Text = CCAM_ID;
    FrmCameraInfo->Edit2->Text = NAME;
    FrmCameraInfo->Edit3->Text = STRM_ADDR;
    FrmCameraInfo->Edit4->Text = FULL_STRM_ADDR;
    FrmCameraInfo->ShowModal();
    FrmCameraInfo = NULL;
}
//---------------------------------------------------------------------------

