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

#include <vcl.h>
#pragma hdrstop

#include "FrmCameraF.h"
#include "FrmCctvMonitoringF.h"
#include "AppGlobalF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma resource "*.dfm"
TFrmCamera *FrmCamera = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmCamera::TFrmCamera(TComponent* Owner)
    : TForm(Owner)
{
    FPlay = false;
    FAutoPlay = true;
    FConHandle = 0;
    FInstalled = false;

    AcesTechXPlayer21->DoubleBuffered = true;

    InitCamera(NULL, FAutoPlay);
}
//---------------------------------------------------------------------------

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

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

void __fastcall TFrmCamera::MnuReleaseClick(TObject *Sender)
{
    CameraRelease();
}
//---------------------------------------------------------------------------

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

    if (FId.IsEmpty()) return;
    if (FViewAddress.IsEmpty()) return;

    PnlCamera->Caption = "¿¬°áÁß...";
    AcesTechXPlayer21->Visible = true;

    try
    {
Log(FTitle + "  " + FName + "  Connect");
        AcesTechXPlayer21->URL = FViewAddress;
        FConHandle = AcesTechXPlayer21->ConnectAsync();
Log(FTitle + "  " + FName + "  Handle: " + String(FConHandle));
    }
    catch(...)
    {
    }
    FPlay = true;
}
//---------------------------------------------------------------------------

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

    try
    {
        //if (FConHandle)
        {
Log(FTitle + "  " + FName + "  Disconnect");
            AcesTechXPlayer21->Close();
        }
        FConHandle = 0;
    }
    catch(...)
    {
    }

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

void __fastcall TFrmCamera::CameraRelease()
{
    Disconnect();
    InitCamera(NULL, FAutoPlay);

    PnlCamera->Caption = "";
    AcesTechXPlayer21->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::PnlCameraDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept)

{
    Accept = true;

    if (!g_Drag.Id.IsEmpty())
    {
#if 0
        int x_gap = Top;
        int y_gap = Left;
        FrmMain->StDrag->Top  = (Y-10-FrmMain->StDrag->Height) - y_gap;
        FrmMain->StDrag->Left = (X) - x_gap;

        if (!FrmMain->StDrag->Visible)
        {
            FrmMain->StDrag->Visible = true;
        }
#endif
    }
    else
    {
        Accept = false;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::PnlCameraDragDrop(TObject *Sender, TObject *Source, int X, int Y)
{
    if (!g_Drag.Id.IsEmpty())
    {
        FId            = g_Drag.Id;
        FGroup         = g_Drag.Group;
        FName          = g_Drag.Name;
        FStreamingType = g_Drag.StreamingType;
        FViewAddress   = g_Drag.ViewAddress;
        FFullAddress   = g_Drag.FullAddress;

        PnlCamera->Caption = "Disconnected";
        DisplayName(FName);

        FInstalled     = true;

        if (FAutoPlay || FPlay)
        {
            Connect();
        }
    }
    g_Drag.Id = "";
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::InitCamera(TXCctv *ACctv, bool AAutoPlay/*=true*/)
{
    FAutoPlay = AAutoPlay;

    if (!ACctv)
    {
        FId            = "";
        FGroup         = "";
        FName          = "";
        FStreamingType = "";
        FViewAddress   = "";
        FFullAddress   = "";

        DisplayName(" ");

        FInstalled     = false;
    }
    else
    {
        FId            = ACctv->Id;
        FGroup         = ACctv->Group;
        FName          = ACctv->Name;
        FStreamingType = ACctv->StreamingType;
        FViewAddress   = ACctv->ViewAddress;
        FFullAddress   = ACctv->FullAddress;

        PnlCamera->Caption = "Disconnected";
        DisplayName(FName);

        FInstalled     = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::DisplayName(String AName)
{
    LblName->Visible = false;
    LblName->Caption = FName;
    LblName->Transparent = false;
    LblName->Transparent = true;
    LblName->Visible = true;
    LblName->Refresh();
    LblName->BringToFront();

    //::SetWindowLong(LblName->Handle, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
    //::SetLayeredWindowAttributes(LblName->Handle, 0, 80, LWA_ALPHA);
//  long            __fastcall DrawText(BSTR text, long x, long y, BSTR fontname, long fontsize,
//                                      long underline, long fontcolor, long bordercolor, long bkcolor);
//    AcesTechXPlayer21->DrawTextW(AName.c_str(), 10, 10, L"¸¼Àº °íµñ", 10, 0, 0, 0, 0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::UpdateCctvMasterInfo()
{
    if (!FInstalled) return;

    TXCctv *pCctv = XCctvManager->FLists.Find(FId);
    if (pCctv)
    {
        if (pCctv->EditMode == DB_DELETE ||
            pCctv->DEL_YN   == "Y")
        {
            CameraRelease();
            return;
        }
        bool bReconnect = false;
        if (FViewAddress != pCctv->ViewAddress ||
            FFullAddress != pCctv->FullAddress)
        {
            bReconnect = true;
        }
        InitCamera(pCctv, FAutoPlay);
        if (bReconnect)
        {
            if (FPlay)
            {
                Connect();
            }
        }
    }
    else
    {
        CameraRelease();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::TmrPlayTimer(TObject *Sender)
{
    TmrStop->Enabled = false;
    TmrPlay->Enabled = false;
    Connect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::TmrStopTimer(TObject *Sender)
{
    TmrPlay->Enabled = false;
    TmrStop->Enabled = false;
    Disconnect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCamera::LblNameClick(TObject *Sender)
{
return;
LblName->Parent = AcesTechXPlayer21;
LblName->Transparent = false;
LblName->Transparent = true;
//AcesTechXPlayer21->Visible = !AcesTechXPlayer21->Visible;
}
//---------------------------------------------------------------------------

