// NetSDKDemoDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "NetSDKDemoDlg.h"
#include "NetSDKDLL.h"
#include "afxwin.h"
#include "DevConfig.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

long g_lVerBuild = 0;
long g_lVerRelease = 0;
long g_lPlayerVer = 0;

HANDLE g_EVENT_STOP_MEDIA = NULL;
HANDLE g_EVENT_LOGIN_STATUS = NULL;
HANDLE g_EVENT_GET_VERSION = NULL;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL OnInitDialog();
public:
	CStatic m_ctlReleaseVer;
public:
	CStatic m_ctlBuildVer;
public:
	CStatic m_ctlPlayerVer;
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_DLL_VER_R, m_ctlReleaseVer);
	DDX_Control(pDX, IDC_STATIC_DLL_VER_B, m_ctlBuildVer);
	DDX_Control(pDX, IDC_STATIC_DLL_VER_PLAYER, m_ctlPlayerVer);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()



LONG __stdcall CNetSDKDemoDlg::fStatusEventCallBack(LONG lUser,LONG nStateCode,char *pResponse,void *pUser)
{
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)pUser;
	//TRACEENTRY("\nCNetSDKDemoDlg::fStatusEventCallBack nStateCode: %d,\n pRespone: %s\n", nStateCode, pResponse);
	
	if (pNetSDKDemoDlg && pNetSDKDemoDlg->m_pDevSearch && IsWindow(pNetSDKDemoDlg->m_pDevSearch->m_hWnd))
	{
		pNetSDKDemoDlg->m_pDevSearch->StatusCallBack(lUser, nStateCode, pResponse,pNetSDKDemoDlg->m_pDevSearch);
	}
	if (pNetSDKDemoDlg && pNetSDKDemoDlg->m_pUpDownLoad && IsWindow(pNetSDKDemoDlg->m_pUpDownLoad->m_hWnd))
	{
		pNetSDKDemoDlg->m_pUpDownLoad->StatusCallBack(lUser, nStateCode, pResponse, pNetSDKDemoDlg->m_pUpDownLoad);
	}

	if (nStateCode == EVENT_PTZPRESETINFO)
	{
		if (pNetSDKDemoDlg && pNetSDKDemoDlg->m_pDevCtrlDlgEx) 
		{
			pNetSDKDemoDlg->m_pDevCtrlDlgEx->StatusEvent(lUser, nStateCode, pResponse);
		}
	}

 	if (EVENT_RTSPTHREADEXIT == nStateCode)
 	{

		SetEvent(g_EVENT_STOP_MEDIA);
		return 0;
 	}

	if (EVENT_LOGINOK == nStateCode || EVENT_LOGINFAILED == nStateCode)
	{
		CDevInfo *pDevInfo = NULL;
		POSITION pos = theApp.m_DevList.GetStartPosition();
		while (pos)
		{
			CString strKey;
			CDevInfo *pDevInfo = NULL;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser)
			{
				pDevInfo->iLoginStatus = (nStateCode == EVENT_LOGINOK ? 1 : -1);
				SetEvent(g_EVENT_LOGIN_STATUS);
				int iImage = pDevInfo->iLoginStatus == 1 ? 3 : 2;
				pNetSDKDemoDlg->m_pWndDevList->m_ctlDevList.SetItemImage(pDevInfo->hSubItem, iImage, iImage);
				//pNetSDKDemoDlg->m_pWndDevList->ReflashTreeImage();
				return 0;
			}
		
		}
		return 0;
	}

	if (EVENT_NVR_CHANNELS == nStateCode)
	{
	
		CDevInfo *pDevInfo = NULL;
		POSITION pos = theApp.m_DevList.GetStartPosition();
		
		while (pos)
		{
			CString strKey;
			CDevInfo *pDevInfo = NULL;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser && 1 == pDevInfo->isNVR)
			{
				pDevInfo->iNVRChannelNums = atoi(pResponse);
				pDevInfo->iLoginStatus = (pDevInfo->iNVRChannelNums) > 0 ? 1 : 0;
				return 0;
			}

		}
		return 0;
	}
	
	if (EVENT_NVR_IPC_STATUS == nStateCode)
	{

		CDevInfo *pChannelInfo = NULL;
		char keyChannel[128] = {0};
		POSITION pos = theApp.m_DevList.GetStartPosition();
		while (pos)
		{
			CString strKey;
			CDevInfo *pDevInfo = NULL;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser && 1 == pDevInfo->isNVR)
			{
				if (pDevInfo->iNVRChannelNums > 0 && pDevInfo->iNVRChannelNums < 64)
				{
					int iImage = 2;
					memcpy(pDevInfo->szNVRCHStatus, pResponse, pDevInfo->iNVRChannelNums);
 					for (int i = 1; i <= pDevInfo->iNVRChannelNums; ++i)
 					{
						sprintf_s(keyChannel, "%s:Channel_%02d", strKey.GetBuffer(0), i);
						if (theApp.m_DevList.Lookup(keyChannel, pChannelInfo))
						{
							pChannelInfo->iUserID = pDevInfo->iUserID;
							pChannelInfo->iLoginStatus = pDevInfo->szNVRCHStatus[i-1] == '1' ? 1 : 0;

							iImage = pChannelInfo->iLoginStatus == 1 ? 3 : 2;

							pNetSDKDemoDlg->m_pWndDevList->m_ctlDevList.SetItemImage(pChannelInfo->hSubItem, iImage, iImage);
							
							//m_ctlDevList.SetItemImage(vecItem[i], 3, 3);
							//pChannelInfo->hSubItem->
						}

 					}
					SetEvent(g_EVENT_LOGIN_STATUS);
					
					
					return 0;	
				}
				
			}

		}
		return 0;
	}

	if (EVENT_DOWNLOADFAILED == nStateCode
		|| EVENT_DOWNLOADOK == nStateCode)
	{
		::PostMessage(pNetSDKDemoDlg->m_pRecordDlg->m_hWnd, nStateCode, lUser, 0);
// 		CDevInfo *pDevInfo = NULL;
// 		CMapAutoLock Lock;
// 		POSITION pos = theApp.m_DevList.GetStartPosition();
// 		while (pos)
// 		{
// 			CString strKey;
// 			CDevInfo *pDevInfo = NULL;
// 			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
// 			pDevInfo->iStatusEvent = nStateCode;
// 			break;
// 		}
	}

	if (EVENT_UPLOADFAILED == nStateCode 
		|| EVENT_UPLOADOK == nStateCode)
	{
		CMapAutoLock Lock;
		POSITION pos = theApp.m_DevList.GetStartPosition();
		while (pos)
		{
			CString strKey;
			CDevInfo *pDevInfo = NULL;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser)
			{
				pDevInfo->iUpgradeEvent = nStateCode;
				break;
			}
		}
	}
	

	CSDKEvent *pSDKEvent = NULL;
	switch (nStateCode)
	{
	case EVENT_LOGINOK: // Longin 成功
	case EVENT_LOGINFAILED: // Login 失败
		{
			::PostMessage(pNetSDKDemoDlg->m_hWnd, WM_USR_DEV_STATUS_WND, lUser, nStateCode);
			break;
			break;
		}
	/* 登录状态消息 */
	case EVENT_CONNECTFAILED: // 连接失败
	case EVENT_SOCKETERROR: // Socket连接失败
	case EVENT_LOGOUT_FINISH: // 登录线程退出
	case EVENT_RECVVIDEOPARAM: // 收到视频编码参数
	case EVENT_RECVAUDIOPARAM: //收到音频编码参数
	case EVENT_CONNECTRTSPERROR: //连接失败
	case EVENT_CONNECTRTSPOK: //连接成功
		{
			if (nStateCode == EVENT_CONNECTRTSPOK)
			{
				int a = 0;
			}
			
		}
//	case EVENT_RTSPTHREADEXIT: //连接线程退出
	case EVENT_URLERROR: //URL解释错误
	case EVENT_RECVVIDEOAUDIOPARAM: //同时收到音视频参数
	case EVENT_CONNECT_RTSPSERVER_ERROR: //连接错误，可能是IP或端口错误
	case EVENT_CREATE_RTSPCLIENT_ERROR: //创建rtsp客户端错误
	case EVENT_GET_RTSP_CMDOPTION_ERROR: //连接rtsp服务器，返回错误
	case EVENT_RTSP_AUTHERROR: //认证失败，可能是用户或密码错误
		pSDKEvent = new CSDKEvent(lUser, nStateCode, pResponse);
		theApp.m_MSGDevList.Event_AddTail(pSDKEvent);
		break;
	/* 对讲状态消息 */
	case EVENT_STARTAUDIOOK:		//启动对讲成功
	case EVENT_STARTAUDIOFAILED:	// 启动对讲失败
	case EVENT_STARTAUDIO_ISBUSY:	//设备对讲被占用
	case EVENT_STARTAUDIO_PARAMERROR://不支持此音频参数对讲
	case EVENT_STARTAUDIO_AUDIODDISABLED://不支持对讲
		pSDKEvent = new CSDKEvent(lUser, nStateCode, pResponse);
		theApp.m_MSGVoice.Event_AddTail(pSDKEvent);
		if (pNetSDKDemoDlg->m_pVoiceCtrl)
			::PostMessage(pNetSDKDemoDlg->m_pVoiceCtrl->m_hWnd, WM_DEV_STATUS, 0, 0);
		break;
	}
	return 0;
}

LONG __stdcall CNetSDKDemoDlg::fAUXResponseCallBack(LONG lUser,LONG nType,char *pResponse,void *pUser)
{
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)pUser;

	//TRACEENTRY("\nCNetSDKDemoDlg::fAUXResponseCallBack nType: %d,\npRespone: %s\n", nType, pResponse);
#if 0
	TRACE("fAUXResponseCallBack...lUser = %d\n", lUser);
	TRACE("fAUXResponseCallBack...pResponse = [%s]\n", pResponse);
#endif
	if (nType == CMD_GET_RECORD_FILE_LIST)
	{
		CMapAutoLock Lock;
		POSITION pos = theApp.m_DevList.GetStartPosition();
		while (pos)
		{
			CString strKey;
			CDevInfo *pDevInfo = NULL;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser)
			{
				if (pDevInfo->pRecordFileList) 
					SAFE_FREE(pDevInfo->pRecordFileList);

				pDevInfo->pRecordFileList = strdup(pResponse);

				if (pNetSDKDemoDlg->m_pRecordDlg && IsWindow(pNetSDKDemoDlg->m_pRecordDlg->m_hWnd))
					::PostMessage(pNetSDKDemoDlg->m_pRecordDlg->m_hWnd, WM_FILE_DOWNLOAD, lUser, CMD_GET_RECORD_FILE_LIST);

				return 0;
			}
		}
	}
	else if (nType == CMD_GET_LOGFILE_LIST)
	{

		CMapAutoLock Lock;
		POSITION pos = theApp.m_DevList.GetStartPosition();
		while (pos)
		{
			CString strKey;
			CDevInfo *pDevInfo = NULL;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser)
			{
				if (pDevInfo->pLogFileList) 
					SAFE_FREE(pDevInfo->pLogFileList);

				pDevInfo->pLogFileList = strdup(pResponse);

				if (pNetSDKDemoDlg->m_pRecordDlg && IsWindow(pNetSDKDemoDlg->m_pRecordDlg->m_hWnd))
					::PostMessage(pNetSDKDemoDlg->m_pRecordDlg->m_hWnd, WM_FILE_DOWNLOAD, lUser, CMD_GET_LOGFILE_LIST);

				return 0;
			}
		}
	}
	
	if (CMD_GET_SYSTEM_VERSION_INFO == nType && pNetSDKDemoDlg->m_pUpDownLoad->m_bUpGetVersion)
	{
		strcpy_s(pNetSDKDemoDlg->m_pUpDownLoad->szVersionBuf, pResponse);
		pNetSDKDemoDlg->m_pUpDownLoad->m_bUpGetVersion = FALSE;
		SetEvent(g_EVENT_GET_VERSION);
	}


	int nTypeTmp = nType & 0xffffff;
	BOOL isFound = FALSE;
	extern ConfigCMD g_strCMD[];
	extern const int g_iCmdCount;
	for (int i = 0; i < g_iCmdCount; ++i)
	{
		if (g_strCMD[i].iGetCMD == nTypeTmp || g_strCMD[i].iSetCMD == nTypeTmp)
		{
			isFound = TRUE;
			break;
		}
	}
	//g_EVENT_GET_VERSION
	if (isFound)
	{
		CSDKEvent *pSDKEvent = new CSDKEvent(lUser, nType, pResponse);
		theApp.m_MSGConfig.Event_AddTail(pSDKEvent);
		if (pNetSDKDemoDlg->m_pDevConfig)
			::PostMessage(pNetSDKDemoDlg->m_pDevConfig->m_hWnd, WM_DEV_AUX, lUser, nType);
	}
	
	return 0;
}


CNetSDKDemoDlg::CNetSDKDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNetSDKDemoDlg::IDD, pParent)
{
	
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pWndManager		= NULL;
	m_pWndDevList		= NULL;
	m_pDevSearch		= NULL;
	m_pRecordDlg		= NULL;
	m_pCodeParamDlg		= NULL;
	m_pRecordEx			= NULL;
	m_pDevConfig		= NULL;
	m_pUpDownLoad		= NULL;
	m_pVoiceCtrl		= NULL;
	m_bFullScreen		= FALSE;
	m_pNetSDKLog		= NULL;
	m_pMenuDlg			= NULL;
	m_pPlayCtrl			= NULL;
	m_pAlarmEvent		= NULL;
}

void CNetSDKDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CNetSDKDemoDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_WM_SIZE()
	ON_MESSAGE(WM_DEV_STATUS, &CNetSDKDemoDlg::OnDevStatus)
END_MESSAGE_MAP()


// CNetSDKDemoDlg 消息处理程序

BOOL CNetSDKDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	IP_NET_DVR_Init();

	//IP_NET_DVR_LoadVendorId();

	g_lVerRelease = IP_NET_DVR_GetSDKVersion();

	g_lVerBuild = IP_NET_DVR_GetSDKBuildVersion();
	
	g_lPlayerVer = IP_TPS_GetVersion();

	IP_NET_DVR_SetStatusEventCallBack(fStatusEventCallBack, this);
	IP_NET_DVR_SetAUXResponseCallBack(fAUXResponseCallBack, this);
	
	CRect rcClient;
	GetClientRect(&rcClient);
	/*
	CRect rcPB = CRect(rcClient.left, rcClient.top+31, rcClient.right, rcClient.bottom);
	m_pPlayBackDlg = new CPlayBackDlg();
	m_pPlayBackDlg->SetWndSize(rcPB.Width(), rcPB.Height());
	m_pPlayBackDlg->Create(CPlayBackDlg::IDD, this);
	m_pPlayBackDlg->SetWindowPos(NULL, 
		rcPB.left, 
		rcPB.top,
		rcPB.Width(),
		rcPB.Height(), 
		SWP_HIDEWINDOW);
		*/
	// Center
	m_rcWndManager = CRect(rcClient.left+172,
		rcClient.top + 31,
		rcClient.right - 152, 
		rcClient.bottom - 152);

	m_pWndManager->SetWindowPos(NULL,
		m_rcWndManager.left, 
		m_rcWndManager.top, 
		m_rcWndManager.Width(), 
		m_rcWndManager.Height(),
		SWP_SHOWWINDOW);

	
	// top
	m_rcTop = CRect(
		rcClient.left+1, 
		rcClient.top,
		rcClient.right,
		rcClient.top+30);

	m_pMenuDlg = new CMenuDlg();
	m_pMenuDlg->Create(CMenuDlg::IDD, this);
	m_pMenuDlg->SetWindowPos(NULL, 
		m_rcTop.left,
		m_rcTop.top,
		m_rcTop.Width(),
		m_rcTop.Height(),
		SWP_SHOWWINDOW);
	
	// left
	m_pWndDevList = new CDevListDlg(170, rcClient.bottom-32);
	m_pWndDevList->Create(CDevListDlg::IDD, this);
	m_rcDevListDlg = CRect(
		rcClient.left + 1, 
		rcClient.top + 31, 
		rcClient.left + 170, 
		rcClient.bottom - 1);
	

	// Bottom
	m_rcBottomDlg = CRect(
		rcClient.left + 1 +170,
		rcClient.bottom - 1 - 150, 
		rcClient.right, 
		rcClient.bottom - 1);

	m_pDevSearch = new CDevSearch(m_rcBottomDlg);
	m_pDevSearch->Create(CDevSearch::IDD, this);
	m_pDevSearch->SetWindowPos(NULL,
		m_rcBottomDlg.left,
		m_rcBottomDlg.top,
		m_rcBottomDlg.Width(),
		m_rcBottomDlg.Height(),
		SWP_SHOWWINDOW);

	m_pRecordDlg = new CRecordDlg(m_rcBottomDlg);
	m_pRecordDlg->Create(CRecordDlg::IDD, this);
	m_pRecordDlg->SetWindowPos(NULL,
		m_rcBottomDlg.left,
		m_rcBottomDlg.top,
		m_rcBottomDlg.Width(),
		m_rcBottomDlg.Height(),
		SWP_HIDEWINDOW);
	/*
	m_pAlarmEvent = new CAlarmEventDlg(m_rcBottomDlg);
	m_pAlarmEvent->Create(CAlarmEventDlg::IDD, this);
	m_pAlarmEvent->SetWindowPos(NULL, m_rcBottomDlg.left, 
		m_rcBottomDlg.top, 
		m_rcBottomDlg.Width(),
		m_rcBottomDlg.Height(),
		SWP_HIDEWINDOW);
*/
	// Right
	m_rcRightDlg = CRect(
		rcClient.right - 1 - 150,
		rcClient.top + 31, 
		rcClient.right, 
		rcClient.bottom - 1 -150);
	
	m_pDevCtrlDlg = new CDevCtrlDlg(m_rcRightDlg);
	m_pDevCtrlDlg->Create(CDevCtrlDlg::IDD, this);

	m_pSnapDlg = new CSnapDlg(m_rcRightDlg);
	m_pSnapDlg->Create(CSnapDlg::IDD, this);

	m_pDevCtrlDlgEx = new CDevCtrlDlgEx(m_rcRightDlg);
	m_pDevCtrlDlgEx->Create(CDevCtrlDlgEx::IDD, this);
	
	m_pUpDownLoad = new CUpDownLoad(m_rcRightDlg);
	m_pUpDownLoad->Create(CUpDownLoad::IDD, this);
	
	m_pCodeParamDlg = new CCodeParamDlg(m_rcRightDlg);
	m_pCodeParamDlg->Create(CCodeParamDlg::IDD, this);

	m_pRecordEx = new CRecordEx(m_rcRightDlg);
	m_pRecordEx->Create(CRecordEx::IDD, this);
	
	m_pLocalRecord = new CLocalRecord(m_rcRightDlg);
	m_pLocalRecord->Create(CLocalRecord::IDD, this);
	
	m_pVoiceCtrl = new CVoiceCtrl(m_rcRightDlg);
	m_pVoiceCtrl->Create(CVoiceCtrl::IDD, this);

	m_pDevConfig = new CDevConfig(m_rcRightDlg);
	m_pDevConfig->Create(CDevConfig::IDD, this);
	
	m_pNetSDKLog = new CNetSDKLog(m_rcRightDlg);
	m_pNetSDKLog->Create(CNetSDKLog::IDD, this);
	
	m_pPlayCtrl = new CPlayCtrl(m_rcRightDlg);
	m_pPlayCtrl->Create(CPlayCtrl::IDD, this);

	m_pPlayCtrl->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	m_pWndDevList->SetWindowPos(NULL, 
		m_rcDevListDlg.left,
		m_rcDevListDlg.top,
		m_rcDevListDlg.Width(),
		m_rcDevListDlg.Height(),
		SWP_SHOWWINDOW);

	m_pSnapDlg->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);
	m_pDevCtrlDlg->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_SHOWWINDOW);
	m_pDevCtrlDlgEx->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);
	m_pCodeParamDlg->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);
	
	m_pUpDownLoad->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	m_pRecordEx->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	m_pLocalRecord->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	m_pVoiceCtrl->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	m_pDevConfig->SetWindowPos(NULL,
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	m_pNetSDKLog->SetWindowPos(NULL, 
		m_rcRightDlg.left,
		m_rcRightDlg.top,
		m_rcRightDlg.Width(),
		m_rcRightDlg.Height(),
		SWP_HIDEWINDOW);

	g_EVENT_STOP_MEDIA = CreateEvent(NULL, FALSE, FALSE, _T("EVENT_STOP_MEDIA"));
	g_EVENT_LOGIN_STATUS = CreateEvent(NULL, FALSE, FALSE, _T("EVENT_LOGIN_STATUS"));
	g_EVENT_GET_VERSION = CreateEvent(NULL, FALSE, FALSE, _T("EVENT_GET_VERSION"));
	
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CNetSDKDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CNetSDKDemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		if (!m_bFullScreen)
		{
			CRect rcClient;
			GetClientRect(rcClient);

			if (m_pWndManager)
			{
				m_rcWndManager = CRect(rcClient.left+172,
					rcClient.top + 31,
					rcClient.right - 152, 
					rcClient.bottom - 152);

				m_pWndManager->SetWndSize(m_rcWndManager.Width()-1, m_rcWndManager.Height()-1);

				m_pWndManager->MoveWindow(
					m_rcWndManager.left,
					m_rcWndManager.top,
					m_rcWndManager.Width(), 
					m_rcWndManager.Height());

			}
		}
		
	
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CNetSDKDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


BOOL CNetSDKDemoDlg::DestroyWindow()
{
	// TODO: 在此添加专用代码和/或调用基类
	IP_TPS_CloseAll();
	IP_TPS_ReleaseAll();
	IP_NET_DVR_StopAllRealPlay();
	IP_NET_DVR_LogoutAll();
	IP_NET_DVR_Cleanup();

	return CDialog::DestroyWindow();
}

BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CString strVer;
	if (g_lVerRelease > 0)
	{
		strVer.Format("Release Version: %d\n", g_lVerRelease);
		m_ctlReleaseVer.SetWindowText(strVer);
	}

	if (g_lVerBuild > 0)
	{
		strVer.Format("Build Version: %d\n", g_lVerBuild);
		m_ctlBuildVer.SetWindowText(strVer);
	}

	if (g_lPlayerVer > 0)
	{
		strVer.Format("DllPlayer Version: %d\n", g_lPlayerVer);
		m_ctlPlayerVer.SetWindowText(strVer);
	}
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

int CNetSDKDemoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	m_pWndManager = new CWndManager();
	
	if (m_pWndManager)
	{
		m_pWndManager->Create(
		NULL, 
		NULL,  
		AFX_WS_DEFAULT_VIEW, 
		CRect(0,0,0,0),
		this, 
		0, 
		NULL);
	}

	return 0;
}

void CNetSDKDemoDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	SAFE_DELETE(m_pMenuDlg);
	SAFE_DELETE(m_pSnapDlg);
	SAFE_DELETE(m_pDevCtrlDlg);
	SAFE_DELETE(m_pDevCtrlDlgEx);
	SAFE_DELETE(m_pWndDevList);
	SAFE_DELETE(m_pWndManager);
	SAFE_DELETE(m_pDevSearch);
	SAFE_DELETE(m_pUpDownLoad);
	SAFE_DELETE(m_pCodeParamDlg);
	SAFE_DELETE(m_pRecordDlg);
	SAFE_DELETE(m_pRecordEx);
	SAFE_DELETE(m_pLocalRecord);
	SAFE_DELETE(m_pVoiceCtrl);
	SAFE_DELETE(m_pDevConfig);
	SAFE_DELETE(m_pNetSDKLog);
	SAFE_DELETE(m_pPlayCtrl);
	SAFE_DELETE(m_pAlarmEvent);
	//SAFE_DELETE(m_pPlayBackDlg);
	CDialog::OnClose();
}

void CNetSDKDemoDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: 在此处添加消息处理程序代码
 	CRect rcClient;
 	GetClientRect(rcClient);

}

HWND CNetSDKDemoDlg::GetCurSelWnd()
{
	if (m_pWndManager)
	{
		return m_pWndManager->GetCurSelWnd();
	}
	return NULL;
}

void CNetSDKDemoDlg::PTZCtrl(PTZ_CMD_TYPE PtzCmd, int iPanSpeed, int iTiltSpeed)
{
	m_pWndDevList->PTZCtrl(PtzCmd, iPanSpeed, iTiltSpeed);
}

void CNetSDKDemoDlg::PresetCtrl(PTZ_PRESET_TYPE PresetCmd, int iPresetNum)
{
	m_pWndDevList->PresetCtrl(PresetCmd, iPresetNum);
}

void CNetSDKDemoDlg::XMLCtrl(char *pStrXML)
{
	m_pWndDevList->XMLCtrl(pStrXML);
}

void CNetSDKDemoDlg::SetViewWnd(WndID emWndID, int nCmdShow)
{	
	switch (emWndID)
	{
	case DEV_LIST_DLG:
		if(m_pWndDevList) m_pWndDevList->ShowWindow(nCmdShow);
		break;
	case DEV_CTRL_DLG:
		if(m_pDevCtrlDlg) m_pDevCtrlDlg->ShowWindow(nCmdShow);
		break;
	case DEV_CTRL_DLGEX:
		if (m_pDevCtrlDlgEx) m_pDevCtrlDlgEx->ShowWindow(nCmdShow);
		break;
	case UP_DOWN_LOAD:
		if (m_pUpDownLoad) m_pUpDownLoad->ShowWindow(nCmdShow);
		break;
	case DEV_CODE_PARAM:
		if (m_pCodeParamDlg) m_pCodeParamDlg->ShowWindow(nCmdShow);
		break;
	case DEV_LOG_DLG:
		if (m_pDevSearch) m_pDevSearch->ShowWindow(nCmdShow);
		break;
	case DEV_FILE_LOAD:
		if (m_pRecordDlg) m_pRecordDlg->ShowWindow(nCmdShow);
		if (m_pRecordEx) m_pRecordEx->ShowWindow(nCmdShow);
		if (m_pDevSearch) m_pDevSearch->ShowWindow(!nCmdShow);
		break;
	case DEV_LOCAL_RECORD:
		if (m_pLocalRecord) m_pLocalRecord->ShowWindow(nCmdShow);
		break;
	case DEV_CONFIG:
		if (m_pDevConfig) m_pDevConfig->ShowWindow(nCmdShow);
		break;
	case DEV_VOICE_CTRL:
		if (m_pVoiceCtrl) m_pVoiceCtrl->ShowWindow(nCmdShow);
		break;
	case DEV_SNAP:
		if (m_pSnapDlg) m_pSnapDlg->ShowWindow(nCmdShow);
		break;
	case DEV_PLAY_WND:
		if (m_pPlayCtrl) m_pPlayCtrl->ShowWindow(nCmdShow);
		break;
// 	case FILE_PLAYBACK_WND:
// 		if (m_pPlayBackDlg) m_pPlayBackDlg->ShowWindow(nCmdShow);
// 		break;
	default:break;
	}

// 	if (emWndID == FILE_PLAYBACK_WND)
// 	{
// 		if (m_pWndDevList) m_pWndDevList->ShowWindow(SW_HIDE);
// 		if (m_pWndManager) m_pWndManager->ShowWindow(SW_HIDE);
// 	}
// 	else
// 	{
// 		if (m_pWndDevList) m_pWndDevList->ShowWindow(SW_SHOW);
// 		if (m_pWndManager) m_pWndManager->ShowWindow(SW_SHOW);
// 	}
}
BOOL CNetSDKDemoDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_USR_DEV_STATUS_WND)//设备状态消息
	{
		switch (pMsg->lParam)
		{
		case EVENT_LOGINOK:
		case EVENT_LOGINFAILED:

			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CString strKey;
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
				if (pDevInfo->iUserID == pMsg->wParam)
				{
					//int iLoginStatus;			// 0 未收到登陆状态，-1 登陆失败，1 登陆成功。
					
					pDevInfo->iLoginStatus = pMsg->lParam == EVENT_LOGINOK ? 1 : -1;
					//pDevInfo->iUserID = 0;
					break;
				}
			}
			((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->ReflashTreeImage();
			break;
		}
	}


	if (pMsg->message == WM_StatusEvent
		&& pMsg->wParam == EVENT_PTZPRESETINFO)
	{
		if (m_pDevCtrlDlgEx)
		{
			::PostMessage(m_pDevCtrlDlgEx->GetSafeHwnd(), WM_StatusEvent, pMsg->wParam, pMsg->lParam);
		}
		return TRUE;
	}
	
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == 13)
			return TRUE;
		if (pMsg->wParam == 27)
		{
			EndFullScreen();

			return TRUE;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}


void CNetSDKDemoDlg::SetWndUser(HWND hWnd, LPCTSTR strKey)
{
	if (m_pWndManager)
	{
		m_pWndManager->SetWndUser(hWnd, strKey);
	}
}

CString CNetSDKDemoDlg::GetWndUser(HWND hWnd)
{
	if (m_pWndManager)
	{
		return m_pWndManager->GetWndUser(hWnd);
	}
	return NULL;
}

void CNetSDKDemoDlg::ClearWndUser()
{
	if (m_pWndManager)
	{
		m_pWndManager->ClearWndUser();
	}
}


void CNetSDKDemoDlg::FullScreen()
{	
	if (m_bFullScreen) return ;
	CRect rcFull(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));

	if (m_pMenuDlg)
		m_pMenuDlg->ShowWindow(SW_HIDE);
	if (m_pWndDevList)
		m_pWndDevList->ShowWindow(SW_HIDE);
	if (m_pDevSearch)
		m_pDevSearch->ShowWindow(SW_HIDE);
	if (m_pRecordDlg)
		m_pRecordDlg->ShowWindow(SW_HIDE);
	if (m_pSnapDlg)
		m_pSnapDlg->ShowWindow(SW_HIDE);
	if (m_pDevCtrlDlg)
		m_pDevCtrlDlg->ShowWindow(SW_HIDE);
	if (m_pDevCtrlDlgEx)
		m_pDevCtrlDlgEx->ShowWindow(SW_HIDE);
	if (m_pUpDownLoad)
		m_pUpDownLoad->ShowWindow(SW_HIDE);
	if (m_pCodeParamDlg)
		m_pCodeParamDlg->ShowWindow(SW_HIDE);
	if (m_pRecordEx)
		m_pRecordEx->ShowWindow(SW_HIDE);
	if (m_pLocalRecord)
		m_pLocalRecord->ShowWindow(SW_HIDE);
	if (m_pVoiceCtrl)
		m_pVoiceCtrl->ShowWindow(SW_HIDE);
	if (m_pDevConfig)
		m_pDevConfig->ShowWindow(SW_HIDE);
	if (m_pNetSDKLog)
		m_pNetSDKLog->ShowWindow(SW_HIDE);

	GetWindowPlacement(&m_wndpl);

	CRect WindowRect; 
	GetWindowRect(&WindowRect); 
	CRect ClientRect; 
	RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, reposQuery, &ClientRect); 
	ClientToScreen(&ClientRect); 
	
	int nFullWidth=GetSystemMetrics(SM_CXSCREEN); 
	int nFullHeight=GetSystemMetrics(SM_CYSCREEN); 
	
	rcFull.left=WindowRect.left-ClientRect.left; 
	rcFull.top=WindowRect.top-ClientRect.top; 
	rcFull.right=WindowRect.right-ClientRect.right+nFullWidth; 
	rcFull.bottom=WindowRect.bottom-ClientRect.bottom+nFullHeight; 


	WINDOWPLACEMENT wndpl;
	wndpl.length=sizeof(WINDOWPLACEMENT); 
	wndpl.flags=0; 
	wndpl.showCmd=SW_SHOWNORMAL; 
	wndpl.rcNormalPosition=rcFull; 
	SetWindowPlacement(&wndpl);
	m_bFullScreen = TRUE;

 	if (m_pWndManager)
	{
		m_pWndManager->SetParent(NULL);
		m_pWndManager->FullScreen();
	}

}

void CNetSDKDemoDlg::EndFullScreen()
{
	if (m_bFullScreen)
	{
		m_pWndManager->SetParent(this);
		SetWindowPlacement(&m_wndpl);
		m_bFullScreen = FALSE;
		if (m_pMenuDlg)
		{
			m_pMenuDlg->ShowWindow(SW_SHOW);
			m_pMenuDlg->ShowSelWnd();
		}
		if (m_pWndDevList)
			m_pWndDevList->ShowWindow(SW_SHOW);
		if (m_pWndManager)
			m_pWndManager->EndFullScreen();
	}
	
}

void CNetSDKDemoDlg::OpenDigitZoom(BOOL bOpen, int lPlayID)
{
	if (m_pWndManager)
	{
		m_pWndManager->OpenDigitZoom(bOpen, lPlayID);
	}
}


HRESULT CNetSDKDemoDlg::OnDevStatus(WPARAM wParam, LPARAM lParam)
{

	return 0l;
}

#if 0
FILE* OpenLogFile()
{
	static FILE* fp = NULL;
	static int flag = 0;

	if (flag == 0)
	{
		SYSTEMTIME sysTime = {0};
		GetLocalTime(&sysTime);

		char fileName[MAX_PATH] = {0};
		sprintf(fileName, "E:\\%04d%02d%02d%02d%02d%02d.txt", sysTime.wYear, sysTime.wMonth, sysTime.wDay,
			sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
		//sprintf(fileName, "E:\\Log\\%04d%02d%02d", sysTime.wYear, sysTime.wMonth);
		fp = fopen(fileName, "a+");

		if (fp != NULL)
		{
			flag = 1;
		}
	}
	

	return fp;
}

void WriteLog(char* format, ...)
{
	FILE* fp = OpenLogFile();

	if(NULL == fp)
	{
		return;
	}

	va_list list;
	va_start(list, format);

	//char  ss[256] = {0};
	//_vsnprintf(ss, 256, format, list);
	vfprintf(fp, format, list);
	//fprintf(fp, ss);
	fprintf(fp, "\r\n");

	va_end(list);

	fflush(fp);
#if 0
	if (NULL != fp)
	{
		fclose(fp);
		fp = NULL;
	}
#endif
	return;
}
#endif