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

#include <vcl.h>
#pragma hdrstop

#include "FRAME_SamsungCamF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCheckBox"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "ImageBtn"
#pragma link "XNSSDKDEVICELib_OCX"
#pragma link "XNSSDKWINDOWLib_OCX"
#pragma resource "*.dfm"
TFRAMESansungCam *FRAMESamsungCam;
//---------------------------------------------------------------------------
__fastcall TFRAMESansungCam::TFRAMESansungCam(TComponent* Owner)
    : TFrame(Owner)
{
    PnlVmsCam->ParentColor = false;
    PnlVmsCam->Color       = clBlack;

    XnsSdkWindow1->Parent = PnlVmsCam;
    XnsSdkWindow1->Align  = alClient;

    //SetupWebCamera();
}
//---------------------------------------------------------------------------
bool __fastcall TFRAMESansungCam::SetupWebCamera()
{
//// Samsung ip camera activex initialize start
    String sLog;
	long nRet = XnsSdkDevice1->Initialize();
	if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
	{
    	sLog.printf(L"Samsung XnsSdkDevice Initialize() fail: return=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
        ShowMessage(sLog);
        return false;
	}

	nRet = XnsSdkWindow1->Initialize(NULL, NULL);
	if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
	{
    	sLog.printf(L"Samsung XnsSdkWindow Initialize() fail: return=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
        ShowMessage(sLog);
        return false;
	}

	m_lDeviceId = XnsSdkDevice1->CreateDeviceEx();
	m_hDevice = XnsSdkDevice1->GetDeviceHandle(m_lDeviceId);
	if (m_hDevice == 0)
	{
		sLog = "CreateDevice() fail";
    	ShowMessage(sLog);
        return false;
	}

#if 0
    WideString sFindModel = "samsung";
	long nFind = XnsSdkDevice1->FindModel(sFindModel.c_bstr());

	long nCount = XnsSdkDevice1->GetFindSize(nFind);
	String sModel;
    CboModel->Properties->Items->Clear();
	for (int ii = 0 ; ii < nCount ; ii++)
	{
		sModel = XnsSdkDevice1->GetFindString(nFind, ii);
        CboModel->Properties->Items->Add(sModel);
	}
	XnsSdkDevice1->CloseFind(nFind);
    CboModel->ItemIndex = 0;
#endif

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMESansungCam::BtnCamCtlMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
    //PTZ Control
	//	XPTZ_UP			1		Tilt up
	//	XPTZ_DOWN		2		Tilt down
	//	XPTZ_LEFT		3		Pan left
	//	XPTZ_RIGHT		4		Pan right
	//	XPTZ_UPLEFT		5		Tilt up and pan left
	//	XPTZ_UPRIGHT	6		Tilt up and pan right
	//	XPTZ_DOWNLEFT	7		Tilt down and pan left
	//	XPTZ_DOWNRIGHT	8		Tilt down and pan right
	//	XPTZ_ZOOMIN		9		Zoom in
	//	XPTZ_ZOOMOUT	10		Zoom out
	//	XPTZ_STOP		11		Stop the PTZ moving
	//	XPTZ_FOCUS_NEAR	12		Focus near
	//	XPTZ_FOCUS_FAR	13		Focus far
	//	XPTZ_FOCUS_STOP	14		Stop focus moving
	//	XPTZ_IRIS_OPEN	15		Open iris
	//	XPTZ_IRIS_CLOSE	16		Close iris

    try
    {
        TImageBtn *pBtn = (TImageBtn*)Sender;
        //	PTZ_FOCUS_NEAR	12		Focus near
        //	PTZ_FOCUS_FAR	13		Focus far
        //	PTZ_FOCUS_STOP	14		Stop focus moving
        if (pBtn->Tag == 0)
        {
        }
        else
        {
            moveCamera((int)pBtn->Tag);
        }
    }
    catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFRAMESansungCam::BtnCamCtlMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
    //PTZ Control Stop

    try
    {
        TImageBtn *pBtn = (TImageBtn*)Sender;
        //	PTZ_FOCUS_NEAR	12		Focus near
        //	PTZ_FOCUS_FAR	13		Focus far
        //	PTZ_FOCUS_STOP	14		Stop focus moving
        if (pBtn->Tag == PTZ_FOCUS_NEAR || pBtn->Tag == PTZ_FOCUS_FAR)
        {
            moveCamera(PTZ_FOCUS_STOP);
        }
        else if (pBtn->Tag == 0)
        {
        }
        else
        {
            moveCamera(PTZ_STOP);
        }
    }
    catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMESansungCam::moveCamera(int ACommand)
{
	long ret;

    if (m_hDevice == 0 || m_nControlId == 0) return;

    int nPtzSpeed = PTZSpeed->Position;

    if (ACommand == XPTZ_STOP ||
        ACommand == PTZ_FOCUS_STOP)
    {
        nPtzSpeed = 0;
    }

	//WLOGD(_T("MoveCamera, command=[%d], speed=%d\n"), nCommand, m_nPtzSpeed);

// 	if( nCommand != XPTZ_STOP)
// 		m_ctrlXnsDevice.ControlPtz(m_hDevice, m_nControlId, XPTZ_STOP, 30);

	// Returns the capabilities of the control module.
	long bIsCap = XnsSdkDevice1->GetControlCapability(m_hDevice, m_nControlId, XCTL_CAP_PTZ_SPEED);

	// [ XNS ACTIVEX HELP ]
	// -----------------------------------------------------------------------
	// Moves the camera to the predefined PTZ position. This function is
	// valid as long as the application is receiving media stream from the
	// camera.
	// -----------------------------------------------------------------------
	//	Command			Value	Description
	// -----------------------------------------------------------------------
	//	XPTZ_UP			1		Tilt up
	//	XPTZ_DOWN		2		Tilt down
	//	XPTZ_LEFT		3		Pan left
	//	XPTZ_RIGHT		4		Pan right
	//	XPTZ_UPLEFT		5		Tilt up and pan left
	//	XPTZ_UPRIGHT	6		Tilt up and pan right
	//	XPTZ_DOWNLEFT	7		Tilt down and pan left
	//	XPTZ_DOWNRIGHT	8		Tilt down and pan right
	//	XPTZ_ZOOMIN		9		Zoom in
	//	XPTZ_ZOOMOUT	10		Zoom out
	//	XPTZ_STOP		11		Stop the PTZ moving
	//	XPTZ_FOCUS_NEAR	12		Focus near
	//	XPTZ_FOCUS_FAR	13		Focus far
	//	XPTZ_FOCUS_STOP	14		Stop focus moving
	//	XPTZ_IRIS_OPEN	15		Open iris
	//	XPTZ_IRIS_CLOSE	16		Close iris
	// -----------------------------------------------------------------------
	if (bIsCap)
	{
		ret = XnsSdkDevice1->ControlPtz(
			m_hDevice,		// [in] Device handle. This value is returned from CreateDevice().
			m_nControlId,	// [in] Control ID.
			ACommand,		// [in] PTZ command
			nPtzSpeed		// [in] If nPtzCommand is a speed-related command,
							// this value indicates the PTZ operation speed.
							// (1~100) This value is valid only if the camera
							// supports the XCTL_CAP_PTZ_SPEED capability.
			);
	}
	else
    {
		ret = XnsSdkDevice1->ControlPtz(m_hDevice, m_nControlId, ACommand, (long)nPtzSpeed);
    }

	if (ret != ERR_SUCCESS)
	{
        String sErr;
		sErr.printf(L"ControlPtz() fail:: error=[%d](%s)", ret, XnsSdkDevice1->GetErrorString(ret));
        ShowMessage(sErr);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMESansungCam::XnsSdkWindow1LButtonDblClk(TObject *Sender, long nDeviceID,
          long nControlID, long nRecDay)
{
ShowMessage("XnsSdkWindow1LButtonDblClk");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMESansungCam::PnlVmsCamDblClick(TObject *Sender)
{
ShowMessage("PnlVmsCamDblClick");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMESansungCam::PnlCamConrtolDblClick(TObject *Sender)
{
ShowMessage("PnlCamConrtolDblClick");
}
//---------------------------------------------------------------------------
