#pragma once

#include "CNZSDK.h"
#include "DlgScreen.h"
#include "afxwin.h"
#include "afxdtctl.h"
#include <map>
#include <vector>

#include <memory>
#ifndef SHARED_PTR
#if _MSC_VER > 1500		// Heigher than Visual Studio 2008  
#define SHARED_PTR std::shared_ptr
#define DYNAMIC_POINTER_CAST std::dynamic_pointer_cast
#else
#define SHARED_PTR std::tr1::shared_ptr
#define DYNAMIC_POINTER_CAST std::tr1::dynamic_pointer_cast
#endif
#endif

class CDlgLiveSample : public CDialog
{
	DECLARE_DYNAMIC(CDlgLiveSample)

protected:
	typedef struct _STREAM_INFO
	{
		int nSessionType;
		int nNrsID;
		int nDeviceKey;
		int nStreamNo;

		_STREAM_INFO()
		{
			nSessionType = 0;
			nNrsID = 0;
			nDeviceKey = 0;
			nStreamNo = 0;
		}

		_STREAM_INFO(int _nSessionType, int _nNrsID, int _nDeviceKey)
		{
			nSessionType = _nSessionType;
			nNrsID = _nNrsID;
			nDeviceKey = _nDeviceKey;
			nStreamNo = 0;
		}

		_STREAM_INFO(int _nSessionType, int _nNrsID, int _nDeviceKey, int _nStreamNo)
		{
			nSessionType = _nSessionType;
			nNrsID = _nNrsID;
			nDeviceKey = _nDeviceKey;
			nStreamNo = _nStreamNo;
		}

		bool operator ==(const _STREAM_INFO& r) const
		{
			return (nSessionType == r.nSessionType) &&
				(nNrsID == r.nNrsID) &&
				(nDeviceKey == r.nDeviceKey) &&
				(nStreamNo == r.nStreamNo);
		}

		bool operator !=(const _STREAM_INFO& r) const
		{
			return !(*this == r);
		}

		bool operator <(const _STREAM_INFO& r) const
		{
			if (nSessionType < r.nSessionType)
				return true;
			else if (nSessionType > r.nSessionType)
				return false;

			if (nNrsID < r.nNrsID)
				return true;
			else if (nNrsID > r.nNrsID)
				return false;

			if (nDeviceKey < r.nDeviceKey)
				return true;
			else if (nDeviceKey > r.nDeviceKey)
				return false;

			return nStreamNo < r.nStreamNo;
		}

		bool operator >(const _STREAM_INFO& r) const
		{
			if (nSessionType > r.nSessionType)
				return true;
			else if (nSessionType < r.nSessionType)
				return false;

			if (nNrsID > r.nNrsID)
				return true;
			else if (nNrsID < r.nNrsID)
				return false;

			if (nDeviceKey > r.nDeviceKey)
				return true;
			else if (nDeviceKey < r.nDeviceKey)
				return false;

			return nStreamNo > r.nStreamNo;
		}

	} STREAM_INFO, *LPSTREAM_INFO;

protected:
	CNZSDK m_sdk;
	std::map<STREAM_INFO, SHARED_PTR<CDlgScreen>> m_mapScreens;

	BOOL m_stop_capture;
	UINT m_id_capture;
	HANDLE m_capture_thread;
	NZ_SDK_KIND m_eSdkKind;

	CComboBox m_cb_ptz_speed;
	CComboBox m_cb_model;
	CDateTimeCtrl m_dtp;
	CComboBox m_cb_data_category;
	CDateTimeCtrl m_export_from_date;
	CDateTimeCtrl m_export_from_time;
	CDateTimeCtrl m_export_to_date;
	CDateTimeCtrl m_export_to_time;
	CComboBox m_cb_data_image;

	CRITICAL_SECTION m_csCaptureStreams;
	std::vector<STREAM_INFO> m_vCaptureStreams;

public:
	CDlgLiveSample(NZ_SDK_KIND eSdkKind, CWnd* pParent=NULL);
	virtual ~CDlgLiveSample();

	BOOL OnCapture();

protected:
	void EnableVideo(BOOL enable);
	void EnableAudio(BOOL enable);
	void EnableEvent(BOOL enable);
	void EnableTalk(BOOL enable);
	void EnableBackup(BOOL enable);
	void EnablePTZ(BOOL enable);
	void EnableData(BOOL enable);

	NZ_VMS_DATA_TYPE GetDataCategory();
	CString GetDataCategoryName();
	CString GetImageFormat();
	int GetNrsIDByDeviceID(int nDeviceID);

	static void CALLBACK OnExportMessage(NZ_EXPORT_MESSAGE eMessage, LONG_PTR pParam1, LONG_PTR pParam2, LONG_PTR pUserParam);


protected:
	enum { IDD = IDD_LIVE_SAMPLE };
	DECLARE_MESSAGE_MAP()
	virtual void DoDataExchange(CDataExchange* pDX);
	virtual BOOL OnInitDialog();
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	afx_msg void OnDestroy();
	afx_msg void OnBnClickedNrsBtnConnect();
	afx_msg void OnBnClickedNrsBtnDisconnect();
	afx_msg void OnBnClickedVideoBtnOpen();
	afx_msg void OnBnClickedVideoBtnClose();
	afx_msg void OnClose();
	afx_msg void OnBnClickedPresetBtnSet();
	afx_msg void OnBnClickedPresetBtnMove();
	afx_msg void OnBnClickedAudioBtnOpen();
	afx_msg void OnBnClickedAudioBtnClose();
	afx_msg void OnBnClickedEventBtnOpen();
	afx_msg void OnBnClickedEventBtnClose();
	afx_msg void OnCbnSelchangeCbModel();
	afx_msg void OnBnClickedBtnTourStart();
	afx_msg void OnBnClickedBtnTourStop();
	afx_msg void OnBnClickedTalkBtnStart();
	afx_msg void OnBnClickedTalkBtnStop();
	afx_msg void OnBnClickedPbVideoBtnOpen();
	afx_msg void OnBnClickedPbVideoBtnClose();
	afx_msg void OnBnClickedPbPlay();
	afx_msg void OnBnClickedPbPause();
	afx_msg void OnBnClickedGetDatas();
	afx_msg void OnBnClickedExportVideo();
	afx_msg void OnBnClickedCancelExport();
	afx_msg void OnBnClickedExportImage();
	afx_msg void OnBnClickedGetPtzSpecification();
	afx_msg void OnBnClickedGetPtzPosition();
	afx_msg void OnBnClickedSetPtzPosition();
	LRESULT OnScreenClosed(WPARAM wParam, LPARAM lParam);
};
