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

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "LocalRecord.h"
#include "NetSDKDemoDlg.h"
#include "WndCtrl.h"

struct TimeLen
{
	int iTimeLen;
	CString strTimeLen;
};
TimeLen g_TotalTimeLen[] = 
{
	{10*60, "10分钟"},
	{30*60, "30分钟"},
	{1*3600, "一小时"},
	{1*24*3600, "一天"},
	{7*24*3600, "一星期"}, 
	{31*24*3600, "一个月"},
	{-1, NULL}
};

TimeLen g_SingleTimeLen[] = 
{
	{30, "30秒"},
	{1*60, "1分钟"},
	{5*60, "5分钟"},
	{10*60, "10分钟"},
	{20*60, "20分钟"},
	{30*60, "30分钟"},
	{-1, NULL}
};

// CLocalRecord 对话框

IMPLEMENT_DYNAMIC(CLocalRecord, CDialog)

CLocalRecord::CLocalRecord(CRect rcWnd, CWnd* pParent /*=NULL*/)
	: CDialog(CLocalRecord::IDD, pParent)
	,m_rcClient(0, 0, rcWnd.Width(), rcWnd.Height())
{

}

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

void CLocalRecord::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_GROUP, m_ctlGroup);
	DDX_Control(pDX, IDC_ED_SAVEPATH, m_ctlSavePath);
	DDX_Control(pDX, IDC_CB_FILE_TIMELEN, m_ctlCBSiglTmLen);
	DDX_Control(pDX, IDC_CB_TOTAL_TIMELEN, m_ctlCBTotlTmLen);
	DDX_Control(pDX, IDC_CK_REALRECORD, m_ctlCKReal);
	DDX_Control(pDX, IDC_CK_INPUTRECORD, m_ctlCKInput);
}


BEGIN_MESSAGE_MAP(CLocalRecord, CDialog)
	ON_BN_CLICKED(IDC_BTN_START, &CLocalRecord::OnBnClickedBtnStart)
	ON_BN_CLICKED(IDC_BTN_STOP, &CLocalRecord::OnBnClickedBtnStop)
	ON_BN_CLICKED(IDC_BTN_LOAD, &CLocalRecord::OnBnClickedBtnLoad)
	ON_BN_CLICKED(IDC_CK_REALRECORD, &CLocalRecord::OnBnClickedCkRealrecord)
	ON_BN_CLICKED(IDC_CK_INPUTRECORD, &CLocalRecord::OnBnClickedCkInputrecord)
END_MESSAGE_MAP()


// CLocalRecord 消息处理程序

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

	// TODO:  在此添加额外的初始化
	m_ctlGroup.MoveWindow(CRect(m_rcClient.left+2, m_rcClient.top,m_rcClient.Width()-2, m_rcClient.Height()-2));
	
	m_ctlCBSiglTmLen.ResetContent();
	for (int i=0; g_SingleTimeLen[i].iTimeLen > 0; i++)
		m_ctlCBSiglTmLen.InsertString(i, g_SingleTimeLen[i].strTimeLen);
	m_ctlCBSiglTmLen.SetCurSel(0);

	m_ctlCBTotlTmLen.ResetContent();
	for (int i=0; g_TotalTimeLen[i].iTimeLen > 0; i++)	
		m_ctlCBTotlTmLen.InsertString(i, g_TotalTimeLen[i].strTimeLen);
	m_ctlCBTotlTmLen.SetCurSel(0);

	m_ctlSavePath.SetWindowText("C:\\");

	m_ctlCKReal.SetCheck(1);
	m_ctlCKInput.SetCheck(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CLocalRecord::OnBnClickedBtnStart()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData();
	
	CString strFilePath;
	m_ctlSavePath.GetWindowText(strFilePath);
	if (strFilePath.Right(1) != "\\")
	{
		strFilePath += "\\";
	}

	TCHAR szDrive[_MAX_DRIVE] = {0};
	TCHAR szDir[_MAX_DIR] = {0};
	TCHAR szName[_MAX_DIR] = {0};
	TCHAR szExt[_MAX_EXT] = {0};

	_tsplitpath(strFilePath, szDrive, szDir, szName, szExt);
	

	if ( _tcslen(szName) != 0)
	{
		if (_tcslen(szExt) != 0)
		{
			if (_tcscmp(szExt, _T(".avi")) != 0
				&& _tcscmp(szExt, _T(".AVI")) != 0)
			{
				AfxMessageBox(_T("非法录像文件名，请以.avi结尾！"));
				return ;
			}
		}
		else
		{
			AfxMessageBox(_T("请指定文件扩展名,如果是路径请以'\\'结尾！"));
			return ;
		}
	}
	else if (_tcslen(szExt) != 0)
	{
		AfxMessageBox(_T("请指定文件名,如果是路径请以'\\'结尾！"));
		return ;
	}
	
	CString strOnlyPath;
	strOnlyPath.Format("%s%s", szDrive, szDir);

	DWORD dwFileAttr = GetFileAttributes(strOnlyPath);
	if (dwFileAttr == INVALID_FILE_ATTRIBUTES)
	{
		AfxMessageBox("找不到指定的路径!!!");
		return;
	}

	int iSingleSel = m_ctlCBSiglTmLen.GetCurSel();

	int iTotalSel = m_ctlCBTotlTmLen.GetCurSel();

	int iCheck = m_ctlCKInput.GetCheck();

	HWND hWnd = ((CNetSDKDemoDlg *)GetParent())->GetCurSelWnd();

	CString StrKey = ((CNetSDKDemoDlg *)GetParent())->GetWndUser(hWnd);

	if (StrKey.GetLength() == 0) 
	{
		AfxMessageBox("视频没有开启");
		return ;
	}

	long hRealHandle = 0;
	long lUserID = 0;
	STREAM_AV_PARAM *pAVParam = NULL;
	long lInputStreamHandle = 0;
	{
		CMapAutoLock Lock;
		CDevInfo * pDevInfo  = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
		if (bFound)
		{
			hRealHandle = pDevInfo->lRealHandle;
			lUserID = pDevInfo->iUserID;
			if (pDevInfo->pAVParam)
			{
				pAVParam = (STREAM_AV_PARAM *)malloc(sizeof(STREAM_AV_PARAM));
				memcpy(pAVParam, pDevInfo->pAVParam, sizeof(STREAM_AV_PARAM));
			}
			lInputStreamHandle = pDevInfo->lInputStreamHandle;
		}
	}

	if (iCheck > 0)
	{
		if (lInputStreamHandle != 0) 
		{
			AfxMessageBox("已经开启输入流录像");
			goto _RETURN;
		}

		if (hRealHandle && pAVParam)
		{
			int iRet = IP_NET_DVR_StartRecordStream(pAVParam, strFilePath.GetBuffer(), 
				g_SingleTimeLen[iSingleSel].iTimeLen, g_TotalTimeLen[iTotalSel].iTimeLen);

			if (iRet == 0)
			{
				int iErr = IP_NET_DVR_GetLastErrorCode(1);
				CString strMSG;
				strMSG.Format("启动输入流录像失败 得到错误码 [%d]\n", iErr);
				AfxMessageBox(strMSG);
				goto _RETURN;
			}

			{
				CMapAutoLock Lock;
				CDevInfo * pDevInfo  =  NULL;
				BOOL bFound =theApp.m_DevList.Lookup(StrKey, pDevInfo);
				if (bFound)
				{
					pDevInfo->lInputStreamHandle = iRet;
				}
			}
		}
		else
		{
			AfxMessageBox("没有找到输入源");
		}
	}
	else
	{
		if (hRealHandle != 0)
		{
			int iRet = IP_NET_DVR_StartRecord(hRealHandle, strFilePath.GetBuffer(), g_SingleTimeLen[iSingleSel].iTimeLen, g_TotalTimeLen[iTotalSel].iTimeLen);
			
			//IP_NET_DVR_CreateIFrame();

			if (iRet == 	ERR_RECORD_FILEMAXSECONDS_ERROR)
				AfxMessageBox("参数nFileMaxSeconds错误，可用值应该是10至1800");

			else if (iRet == 	ERR_NOT_FIND_STREAM)
				AfxMessageBox("没找到lRealHandle所对应的视频流");

			else if (iRet == 	ERR_RECORD_ISRECORDING)
				AfxMessageBox("当前正在录像中，要求先停止录像，才能再开始");

			else if (iRet == 	ERR_RECORD_MEDIA_PARAM_ERROR)
				AfxMessageBox("视频参数错误");

			else if (iRet == 	ERR_RECORD_FILEPATH_ERROR)
				AfxMessageBox("filePath错误，为空，或是小于2个字符");
		}
		else
		{
			AfxMessageBox("视频流没有开启");
		}
	}

_RETURN :
	SAFE_FREE(pAVParam);
}

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

	HWND hWnd = ((CNetSDKDemoDlg *)GetParent())->GetCurSelWnd();

	CString StrKey = ((CNetSDKDemoDlg *)GetParent())->GetWndUser(hWnd);
		
	long hRealHandle = 0;

	long lInputStreamHandl = 0;

	{	
		CMapAutoLock Lock;
		CDevInfo * pDevInfo  = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
		if (bFound)
		{
			hRealHandle = pDevInfo->lRealHandle;
			lInputStreamHandl = pDevInfo->lInputStreamHandle;
		}
	}

	int iCheck = m_ctlCKInput.GetCheck();
	if(iCheck > 0)
	{	
		if (lInputStreamHandl != 0)
		{
			int iErr = IP_NET_DVR_StopRecordStream(lInputStreamHandl);

			if (iErr == 0)
			{
				CMapAutoLock Lock;
				CDevInfo * pDevInfo  = NULL;
				BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
				if (bFound)
				{
					pDevInfo->lInputStreamHandle = 0;
				}
			}
			else
			{
				AfxMessageBox("启动录像失败!!!");
			}
		}
	}
	else
	{
		if (hRealHandle != 0)
		{
			int iRet = IP_NET_DVR_StopRecord(hRealHandle);
			if (iRet == ERR_NOT_FIND_STREAM)
				AfxMessageBox("句柄lRealHandle未找到");
			else if (iRet == ERR_RECORD_NOTRUNNING)
				AfxMessageBox("此流没有在录像或已停止");
		}
		else 
		{
			AfxMessageBox("视频流没有开启");
		}

	}
	
}

void CLocalRecord::OnBnClickedBtnLoad()
{
	// TODO: 在此添加控件通知处理程序代码
	BROWSEINFO bi;
	ZeroMemory(&bi,sizeof(BROWSEINFO));
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

	TCHAR path[MAX_PATH] = {0};
	if(pidl != NULL)
	{
		SHGetPathFromIDList(pidl,path);

		m_ctlSavePath.SetWindowText(path);
	}
}

void CLocalRecord::OnBnClickedCkRealrecord()
{
	// TODO: 在此添加控件通知处理程序代码
	int iCheck = m_ctlCKReal.GetCheck();
	//
	if (iCheck)
	{
		m_ctlCKInput.SetCheck(0);
	}
	else
	{
		m_ctlCKInput.SetCheck(1);
	}
}

void CLocalRecord::OnBnClickedCkInputrecord()
{
	// TODO: 在此添加控件通知处理程序代码
	int iCheck = m_ctlCKInput.GetCheck();
	if (iCheck)
	{
		m_ctlCKReal.SetCheck(0);
	}
	else
	{
		m_ctlCKReal.SetCheck(1);
	}
}

BOOL CLocalRecord::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);
}
