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

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "RecordEx.h"
#include "NetSDKDemoDlg.h"
#include "RecordDlg.h"

char *g_pRecordMode[] =
{
	//_T("MANUAL"),//手动触发录像（不支持）
	_T("MOTION"),//移动侦测录像
	_T("SCHEDULE"),// 按时间表录像
	_T("ALARM"),//IO口告警录像
	NULL
};

char *g_pMediaType[] = 
{
	_T("AUDIOVIDEO"), // 音频视频
	_T("VIDEO"), // 仅视频
	//_T("AUDIO"), // 仅音频
	_T("PICTURE"),// 拍照
	NULL
};


// CRecordEx 对话框

IMPLEMENT_DYNAMIC(CRecordEx, CDialog)

CRecordEx::CRecordEx(CRect rcWnd, CWnd* pParent /*=NULL*/)
	: CDialog(CRecordEx::IDD, pParent)
	,m_rcClient(0, 0, rcWnd.Width(), rcWnd.Height())
	, m_iMaxLen(-1)
	, m_iMinLen(-1)
{
	
}

CRecordEx::~CRecordEx()
{
	DestroyWindow();
}

void CRecordEx::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_GROUP, m_ctlGroup);
	DDX_Control(pDX, IDC_CB_RECORDTYPE, m_ctlCBRecordType);
	DDX_Control(pDX, IDC_CB_MEDIATYPE, m_ctlCBMediaType);
	DDX_Control(pDX, IDC_CB_STRMTYPE, m_ctlCBStreamType);
	DDX_Control(pDX, IDC_DATEPICKER_START, m_ctlStartDate);
	DDX_Control(pDX, IDC_TIMEPICKER_START, m_ctlStartTime);
	DDX_Control(pDX, IDC_DATEPICKER_END, m_ctlEndDate);
	DDX_Control(pDX, IDC_TIMEPICKER_END, m_ctlEndTime);
	DDX_Text(pDX, IDC_ED_MAXLEN, m_iMaxLen);
	DDX_Text(pDX, IDC_ED_MINLEN, m_iMinLen);
	DDX_Control(pDX, IDC_ED_SAVEPATH, m_ctlSavePath);
	DDX_Control(pDX, IDC_CK_LOG_QUERY, m_ctlQueryLog);
	DDX_Control(pDX, IDC_CK_RECORD_QUERY, m_ctlQueryRecord);
	DDX_Control(pDX, IDC_ED_MAXLEN, m_ctlEDMAXLen);
	DDX_Control(pDX, IDC_ED_MINLEN, m_ctlEDMinLen);
}


BEGIN_MESSAGE_MAP(CRecordEx, CDialog)
	ON_BN_CLICKED(IDC_CK_LOG_QUERY, &CRecordEx::OnBnClickedCkLogQuery)
	ON_BN_CLICKED(IDC_CK_RECORD_QUERY, &CRecordEx::OnBnClickedCkRecordQuery)
	ON_CBN_SELCHANGE(IDC_CB_MEDIATYPE, &CRecordEx::OnCbnSelchangeCbMediatype)
END_MESSAGE_MAP()


// CRecordEx 消息处理程序

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

	// TODO:  在此添加额外的初始化
	m_ctlGroup.MoveWindow(CRect(m_rcClient.left+2, m_rcClient.top,m_rcClient.Width()-2, m_rcClient.Height()-2));
	m_ctlCBRecordType.SetCurSel(0);
	m_ctlCBMediaType.SetCurSel(0);
	m_ctlCBStreamType.SetCurSel(0);
	m_ctlSavePath.SetWindowText("C:\\");
	m_ctlQueryRecord.SetCheck(1);
	SYSTEMTIME stStart;
	GetSystemTime(&stStart);
	stStart.wHour = 0;
	stStart.wMinute = 0;
	stStart.wSecond = 1;
	m_ctlStartTime.SetTime(&stStart);

	stStart.wHour = 23;
	stStart.wMinute = 59;
	stStart.wSecond = 59;
	m_ctlEndTime.SetTime(&stStart);

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

void CRecordEx::OnBnClickedBtnPagedown()
{
	// TODO: 在此添加控件通知处理程序代码
	OnClose();
}

void CRecordEx::Query(LPCTSTR strKey, int iPage)
{
	// TODO: 在此添加控件通知处理程序代码

	int iUserID = 0;
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{	
			iUserID = pDevInfo->iUserID;
		}
	}

	int iCheck = m_ctlQueryLog.GetCheck();
	if (iCheck > 0)
	{
		int iRet = IP_NET_DVR_FindDVRLogFile(iUserID);
		if (iRet == ERR_NOT_FIND_DEVICE)
		{
			AfxMessageBox("未找到指定设备");
			return ;
		}
		else if (iRet == ERR_DEV_NOT_CONNECTED)
		{
			AfxMessageBox("设备未连接");
			return ;
		}
		else if (iRet == ERR_DEV_NOT_LOGIN)
		{
			AfxMessageBox("设备未登录成功");
			return ;
		}
		else if (iRet == ERR_OUTOFF_MEMORY)
		{
			AfxMessageBox("内存不足");
			return ;
		}
	}
	else
	{
#if 1
		UpdateData(TRUE);
		
		int iRTSel = m_ctlCBRecordType.GetCurSel();

		int iMTSel = m_ctlCBMediaType.GetCurSel();

		int iSTSel = m_ctlCBStreamType.GetCurSel() + 1;

		SYSTEMTIME stStartDate, stStartTime, stEndDate, stEndTime;
		m_ctlStartDate.GetTime(&stStartDate);
		m_ctlStartTime.GetTime(&stStartTime);
		m_ctlEndDate.GetTime(&stEndDate);
		m_ctlEndTime.GetTime(&stEndTime);
		
		CString strStartTime, strEndTime;
		strStartTime.Format("%d%02d%02d %02d:%02d:%02d", 
			stStartDate.wYear, stStartDate.wMonth,stStartDate.wDay, 
			stStartTime.wHour, stStartTime.wMinute, stStartTime.wSecond);
		strEndTime.Format("%d%02d%02d %02d:%02d:%02d",
			stEndDate.wYear, stEndDate.wMonth, stEndDate.wDay, 
			stEndTime.wHour, stEndTime.wMinute, stEndTime.wSecond);

		if (iUserID != 0)
		{
			CString strXML;

			strXML.Format("<REQUEST_PARAM\n\
						  RecordMode=\"%s\"\n\
						  StartTime=\"%s\"\n\
						  EndTime=\"%s\"\n\
						  MediaType=\"%s\"\n\
						  StreamIndex=\"%d\"\n\
						  MinSize=\"%d\"\n\
						  MaxSize=\"%d\"\n\
						  Page=\"%d\"\n\
						  />",
						  g_pRecordMode[iRTSel], 
						  strStartTime,
						  strEndTime,
						  g_pMediaType[iMTSel],
						  iSTSel,
						  m_iMinLen,
						  m_iMaxLen,
						  iPage
						  );
			int iRet = IP_NET_DVR_SystemControl(iUserID, CMD_GET_RECORD_FILE_LIST, 0, strXML.GetBuffer());
			TRACE("iRet = %d, iUserID = %d\n", iRet, iUserID);
			TRACE("Something...%s\n", strXML.GetBuffer());

			if (iRet == ERR_NOT_FIND_DEVICE)
			{
				AfxMessageBox("句柄错误\n");
				return ;
			}

			else if (iRet == ERR_DEV_NOT_CONNECTED)
			{
				AfxMessageBox("设备未连接\n");
				return ;
			}

			else if (iRet == ERR_DEV_NOT_LOGIN)
			{
				AfxMessageBox("设备未登录成功\n");
				return ;
			}

			else if (iRet == ERR_OUTOFF_MEMORY)
			{
				AfxMessageBox("内存不足\n");
				return ;
			}
		}
#endif
	}
}

BOOL CRecordEx::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == 13)
			return TRUE;
		if (pMsg->wParam == 27)
		{
			::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN, 27, 0);
			return TRUE;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CRecordEx::OnBnClickedCkLogQuery()
{
	// TODO: 在此添加控件通知处理程序代码
	CNetSDKDemoDlg *pNetSDKDemo = (CNetSDKDemoDlg *)GetParent();
	CRecordDlg *pRecordDlg = (CRecordDlg *)pNetSDKDemo->m_pRecordDlg;
	int iSel = m_ctlQueryLog.GetCheck();
	if (iSel > 0)
	{
		BOOL bSet = FALSE;
		if (pRecordDlg)
		{
			bSet = pRecordDlg->SetLogQuery(TRUE);
		}
		if (bSet)
		{
			m_ctlQueryRecord.SetCheck(0);
			ShowRecord(FALSE);
		}
		else
		{
			m_ctlQueryLog.SetCheck(0);
		}
	}
	else
	{
		m_ctlQueryLog.SetCheck(1);
// 		BOOL bSet = FALSE;
// 		m_ctlQueryRecord.SetCheck(1);
// 		ShowRecord(TRUE);
// 		if (pRecordDlg)
// 		{
// 			pRecordDlg->SetLogQuery(FALSE);
// 		}
	}
}

void CRecordEx::ShowRecord(BOOL bEnable)
{
	m_ctlCBRecordType.EnableWindow(bEnable);
	m_ctlCBMediaType.EnableWindow(bEnable);
	m_ctlCBStreamType.EnableWindow(bEnable);
	m_ctlStartDate.EnableWindow(bEnable);
	m_ctlStartTime.EnableWindow(bEnable);
	m_ctlEndDate.EnableWindow(bEnable);
	m_ctlEndTime.EnableWindow(bEnable);
	m_ctlEDMAXLen.EnableWindow(bEnable);
	m_ctlEDMinLen.EnableWindow(bEnable);
}

void CRecordEx::OnBnClickedCkRecordQuery()
{
	// TODO: 在此添加控件通知处理程序代码
	CNetSDKDemoDlg *pNetSDKDemo = (CNetSDKDemoDlg *)GetParent();
	CRecordDlg *pRecordDlg = (CRecordDlg *)pNetSDKDemo->m_pRecordDlg;

	int iSel = m_ctlQueryRecord.GetCheck();

	if (iSel > 0)
	{
		BOOL bSel = FALSE;
		if (pRecordDlg)
		{
			bSel = pRecordDlg->SetLogQuery(FALSE);
		}
		
		if (bSel)
		{
			m_ctlQueryLog.SetCheck(0);
			ShowRecord(TRUE);
		}
		else
		{
			m_ctlQueryRecord.SetCheck(0);
		}
		
	}
	else
	{
		m_ctlQueryRecord.SetCheck(1);
	}
}

void CRecordEx::OnCbnSelchangeCbMediatype()
{
	// TODO: 在此添加控件通知处理程序代码

	//选择图片类型时，码流参数不可选
	CString text;
	int iCurSel = m_ctlCBMediaType.GetCurSel();
	BOOL bEnable = iCurSel == 2 ? FALSE : TRUE;
	m_ctlCBStreamType.EnableWindow(bEnable);
}