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

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "DevCtrlDlgEx.h"
#include "NetSDKDemoDlg.h"


// CDevCtrlDlgEx 对话框

IMPLEMENT_DYNAMIC(CDevCtrlDlgEx, CDialog)

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

}

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

void CDevCtrlDlgEx::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_GROUP, m_ctlGroup);
	DDX_Control(pDX, IDC_COMBO1, m_ctlCBBack);
	DDX_Control(pDX, IDC_CB_QUERY, m_ctlCBList);
}


BEGIN_MESSAGE_MAP(CDevCtrlDlgEx, CDialog)
	ON_BN_CLICKED(IDC_BTN_ADD, &CDevCtrlDlgEx::OnBnClickedBtnAdd)
	ON_BN_CLICKED(IDC_BTN_DELETE, &CDevCtrlDlgEx::OnBnClickedBtnDelete)
	ON_BN_CLICKED(IDC_BTN_QUERY, &CDevCtrlDlgEx::OnBnClickedBtnQuery)
END_MESSAGE_MAP()


// CDevCtrlDlgEx 消息处理程序

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

	// TODO:  在此添加额外的初始化
	m_ctlGroup.MoveWindow(CRect(m_rcClient.left+2, m_rcClient.top,m_rcClient.Width()-2, m_rcClient.Height()-2));
	m_ctlCBBack.SetCurSel(0);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CDevCtrlDlgEx::OnBnClickedBtnAdd()
{
	// TODO: 在此添加控件通知处理程序代码
	CString rStr;
	m_ctlCBList.GetWindowText(rStr);
	rStr.MakeLower();

	int iORuleID = 0, iSRuleID = 0, iStartH = 0, iStartM = 0, iEndH = 0, iEndM = 0;
	if ( strcmp(rStr.Left(1), "s") == 0
		|| strcmp(rStr.Left(1), "S") == 0)
	{
		CString strRule = rStr.Mid(1, 1);
		iSRuleID = atoi(strRule);
		char *pFind = strstr(rStr.GetBuffer(), "#");
		if (pFind)
		{
			sscanf_s(pFind+1, "%d:%d-%d:%d", &iStartH, &iStartM, &iEndH, &iEndM);
		}
	}

	if ( strcmp(rStr.Left(1), "o") == 0
		|| strcmp(rStr.Left(1), "O") == 0)
	{
		CString strRule = rStr.Mid(1, 1);
		iORuleID = atoi(strRule);
		char *pFind = strstr(rStr.GetBuffer(), "#");
		if (pFind)
		{
			sscanf_s(pFind+1, "%d:%d-%d:%d", &iStartH, &iStartM, &iEndH, &iEndM);
		}
	}

	if ( iStartH > iEndH
		|| (iStartH==iEndH && iStartM >= iEndM)
		|| (iORuleID+iSRuleID)<= 0)
	{
		AfxMessageBox("输入有误！！！\n");
		return ;
	}

	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;//(CNetSDKDemoDlg *)GetParent();
	HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();
	char *pPresetList = NULL;
	CString strKey =pNetSDKDemoDlg->GetWndUser(hWnd);
	
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			pPresetList = pDevInfo->pPresetList;
		}
	}

	if (pPresetList) 
	{
		// 比较时间
		CPresetList ListOld(pPresetList);
		
		int nORule = ListOld.m_PresetList.nORuleNum;
		int nSRule = ListOld.m_PresetList.nSRuleNum;
		
		for (int i=0; i<nORule; i++)
		{
			if (IsTimeIn(iStartH, iStartM, 
				ListOld.m_PresetList.ORule[i].Start.iHour,
				ListOld.m_PresetList.ORule[i].Start.iMin,
				ListOld.m_PresetList.ORule[i].End.iHour,
				ListOld.m_PresetList.ORule[i].End.iMin) != 0)
			{
				AfxMessageBox("Orbit Rule 时间有冲突！！\n");
				return ;
			}
			if (IsTimeIn(iEndH, iEndM, 
				ListOld.m_PresetList.ORule[i].Start.iHour,
				ListOld.m_PresetList.ORule[i].Start.iMin,
				ListOld.m_PresetList.ORule[i].End.iHour,
				ListOld.m_PresetList.ORule[i].End.iMin) != 0)
			{
				AfxMessageBox("Orbit Rule时间有冲突！！\n");
				return ;
			}
			
			if (IsTimeIn(ListOld.m_PresetList.ORule[i].Start.iHour,
				ListOld.m_PresetList.ORule[i].Start.iMin,
				iStartH,iStartM,iEndH, iEndM) != 0)
			{
				AfxMessageBox("Orbit Rule时间有冲突！！\n");
				return ;
			}

			if (IsTimeIn(ListOld.m_PresetList.ORule[i].End.iHour,
				ListOld.m_PresetList.ORule[i].End.iMin,
				iStartH,iStartM,iEndH, iEndM) != 0)
			{
				AfxMessageBox("Orbit Rule时间有冲突！！\n");
				return ;
			}
		}

		for (int i=0; i<nSRule; i++)
		{
			if (IsTimeIn(iStartH, iStartM, 
				ListOld.m_PresetList.SRule[i].Start.iHour,
				ListOld.m_PresetList.SRule[i].Start.iMin,
				ListOld.m_PresetList.SRule[i].End.iHour,
				ListOld.m_PresetList.SRule[i].End.iMin) != 0)
			{
				AfxMessageBox("Scan iStartH时间有冲突！！\n");
				return ;
			}
			if (IsTimeIn(iEndH, iEndM, 
				ListOld.m_PresetList.SRule[i].Start.iHour,
				ListOld.m_PresetList.SRule[i].Start.iMin,
				ListOld.m_PresetList.SRule[i].End.iHour,
				ListOld.m_PresetList.SRule[i].End.iMin) != 0)
			{
				AfxMessageBox("Scan iStartH时间有冲突！！\n");
				return ;
			}

			if (IsTimeIn(ListOld.m_PresetList.SRule[i].Start.iHour,
				ListOld.m_PresetList.SRule[i].Start.iMin,
				iStartH,iStartM,iEndH, iEndM) != 0)
			{
				AfxMessageBox("Scan iStartH时间有冲突！！\n");
				return ;
			}

			if (IsTimeIn(ListOld.m_PresetList.SRule[i].End.iHour,
				ListOld.m_PresetList.SRule[i].End.iMin,
				iStartH,iStartM,iEndH, iEndM) != 0)
			{
				AfxMessageBox("Scan iStartH时间有冲突！！\n");
				return ;
			}
		}
	}

	CString strXML;
	if (iORuleID > 0)
	{
		strXML.Format("<xml>\n<cmd>SetScan</cmd>\n<preset>%d</preset>\n<r>\n<b>%d:%d</b>\n<e>%d:%d</e>\n</r>\n</xml>",
			iORuleID, iStartH, iStartM, iEndH, iEndM);
	}
	else if (iSRuleID > 0)
	{
		strXML.Format("<xml>\n<cmd>SetOrbit</cmd>\n<preset>%d</preset>\n<r>\n<b>%d:%d</b>\n<e>%d:%d</e>\n</r>\n</xml>",
			iSRuleID, iStartH, iStartM, iEndH, iEndM);
	}
	pNetSDKDemoDlg->XMLCtrl(strXML.GetBuffer());

}
// 1 再Start - End 之间
int CDevCtrlDlgEx::IsTimeIn(int iHour,int iMin, int iStartHour, int iStartMin, int iEndHour, int iEndMin)
{
	if ( iHour <= iStartHour 
		|| iHour >= iEndHour )
	{
		if ( (iHour == iStartHour
			&& iMin >= iStartMin)
			|| (iHour == iEndHour
			&& iMin <= iEndMin) )
		{
			TRACE("%02d:%02d-%02d:%02d\n", iStartHour, iStartMin,iEndHour,iEndMin);
			return 1;
		}
		return 0;
	}
	return 1;
}

void CDevCtrlDlgEx::OnBnClickedBtnDelete()
{
	// TODO: 在此添加控件通知处理程序代码
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;//(CNetSDKDemoDlg *)GetParent();
	CString rStr;
	m_ctlCBList.GetWindowText(rStr);

	rStr.MakeLower();

	int iORuleID = 0, iSRuleID = 0, iStartH = 0, iStartM = 0, iEndH = 0, iEndM = 0;
	if ( strcmp(rStr.Left(1), "s") == 0
		|| strcmp(rStr.Left(1), "S") == 0)
	{
		CString strRule = rStr.Mid(1, 1);
		iSRuleID = atoi(strRule);
		char *pFind = strstr(rStr.GetBuffer(), "#");
		if (pFind)
		{
			sscanf_s(pFind+1, "%d:%d-%d:%d", &iStartH, &iStartM, &iEndH, &iEndM);
		}
	}

	if ( strcmp(rStr.Left(1), "o") == 0
		|| strcmp(rStr.Left(1), "O") == 0)
	{
		CString strRule = rStr.Mid(1, 1);
		iORuleID = atoi(strRule);
		char *pFind = strstr(rStr.GetBuffer(), "#");
		if (pFind)
		{
			sscanf_s(pFind+1, "%d:%d-%d:%d", &iStartH, &iStartM, &iEndH, &iEndM);
		}
	}

	if ( iStartH > iEndH
		|| (iStartH==iEndH && iStartM >= iEndM)
		|| (iORuleID+iSRuleID)<= 0)
	{
		AfxMessageBox("输入有误！！！\n");
		return ;
	}
	
	CString strXML;
	if (iORuleID > 0)
	{
		strXML.Format("<xml>\n<cmd>DelScan</cmd>\n<preset>%d</preset>\n</xml>\n", iORuleID);
	}
	else if (iSRuleID > 0)
	{
		//strXML.Format("<xml>\n<cmd>DelScan</cmd>\n<preset>%d</preset>\n</xml>\n", iSRuleID);
		strXML.Format("<xml>\n<cmd>DelOrbit</cmd>\n<preset>%d</preset>\n</xml>\n", iSRuleID);
	}
	else
	{
		AfxMessageBox("Rule ID Invalidate!!!\n");
		return ;
	}

	pNetSDKDemoDlg->XMLCtrl(strXML.GetBuffer());
	TRACE("%s", strXML.GetBuffer());
}

void CDevCtrlDlgEx::OnBnClickedBtnQuery()
{
	// TODO: 在此添加控件通知处理程序代码
	m_ctlCBList.ResetContent();
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;//(CNetSDKDemoDlg *)GetParent();
	HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();
	char *pPresetList = NULL;
	CString strKey =pNetSDKDemoDlg->GetWndUser(hWnd);
	
	if (strKey == _T(""))
	{
		AfxMessageBox(_T("请选择正在播放的视频!"));
		return ;
	}

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

	if(pPresetList)
	{
		CPresetList List(pPresetList);
		int nORoule = List.m_PresetList.nORuleNum;
		int nSRoule = List.m_PresetList.nSRuleNum;
		int nPreset = List.m_PresetList.nPresetNum;

		for (int i = 0; i<nORoule; i++)
		{
			int iRuleID = List.m_PresetList.ORule[i].iRuleID;
			int iStartH = List.m_PresetList.ORule[i].Start.iHour;
			int iStartM = List.m_PresetList.ORule[i].Start.iMin;
			int iEndH = List.m_PresetList.ORule[i].End.iHour;
			int iEndM = List.m_PresetList.ORule[i].End.iMin;
			CString strRule;
			strRule.Format("o%d#%02d:%02d-%02d:%02d", iRuleID, iStartH, iStartM, iEndH, iEndM);
			int iCount = m_ctlCBList.GetCount();
			m_ctlCBList.InsertString(iCount, strRule);
			m_ctlCBList.SetCurSel(iCount);
		}
		
		for (int i=0; i<nSRoule; i++)
		{
			int iRuleID = List.m_PresetList.SRule[i].iRuleID;
			int iStartH = List.m_PresetList.SRule[i].Start.iHour;
			int iStartM = List.m_PresetList.SRule[i].Start.iMin;
			int iEndH = List.m_PresetList.SRule[i].End.iHour;
			int iEndM = List.m_PresetList.SRule[i].End.iMin;
			CString strRule;
			strRule.Format("s%d#%02d:%02d-%02d:%02d", iRuleID, iStartH, iStartM, iEndH, iEndM);
			int iCount = m_ctlCBList.GetCount();
			m_ctlCBList.InsertString(iCount, strRule);
			m_ctlCBList.SetCurSel(iCount);
		}
	}
}

BOOL CDevCtrlDlgEx::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 CPresetList::DoParse()
{
	if (m_pStrPresetList == NULL) return ;

	char *pStrPresetList = m_pStrPresetList;
	BeNoSpace(pStrPresetList, strlen(pStrPresetList));
	CStringArray strarr;
	int nCount = Split(pStrPresetList, "^", 1, strarr);
	for(int i=0; i<nCount; i++)
	{
		CString strG = strarr.GetAt(i);
		int x = strG.GetLength();

		if ( x>0 )
		{
			strG.MakeLower();
			if ( strcmp(strG.Left(1), "s") == 0
				|| strcmp(strG.Left(1), "S") == 0)
			{
				CString strRule = strG.Mid(1,1);
				int iRuleNum = m_PresetList.nSRuleNum++;
				m_PresetList.SRule[iRuleNum].iRuleID = atoi(strRule);
				char *pFind = strstr(strG.GetBuffer(), "#");
				if (pFind)
				{
					int iStartH = 0, iStartM = 0, iEndH = 0, iEndM = 0;
					sscanf_s(pFind+1, "%d:%d-%d:%d", &iStartH, &iStartM, &iEndH, &iEndM);
					
					m_PresetList.SRule[iRuleNum].Start.iHour = iStartH; 
					m_PresetList.SRule[iRuleNum].Start.iMin = iStartM;
					m_PresetList.SRule[iRuleNum].End.iHour = iEndH;
					m_PresetList.SRule[iRuleNum].End.iMin = iEndM;	
				}

			}
			else if (strcmp(strG.Left(1), "o") == 0
				|| strcmp(strG.Left(1), "O") == 0)
			{
				CString strRule = strG.Mid(1,1);
				int iRuleNum = m_PresetList.nORuleNum++;
				m_PresetList.ORule[iRuleNum].iRuleID = atoi(strRule);
				char *pFind = strstr(strG.GetBuffer(), "#");
				if (pFind)
				{
					int iStartH = 0, iStartM = 0, iEndH = 0, iEndM = 0;
					sscanf_s(pFind+1, "%d:%d-%d:%d", &iStartH, &iStartM, &iEndH, &iEndM);
					m_PresetList.ORule[iRuleNum].Start.iHour = iStartH; 
					m_PresetList.ORule[iRuleNum].Start.iMin = iStartM;
					m_PresetList.ORule[iRuleNum].End.iHour = iEndH;
					m_PresetList.ORule[iRuleNum].End.iMin = iEndM;	
				}
			}
			else
			{
				int iPreset = m_PresetList.nPresetNum++;
				m_PresetList.iPreset[iPreset] = atoi(strG);
			}
		}
	}
}

int CPresetList::Split(char *m_innerText,char* spobj,int NeedRemoveEmpty,CStringArray &retls)
{
	int retcount=0;
	CString temp=m_innerText;
	CString FindMsg=spobj;
	int startIdx=0;
	int spobjlen=FindMsg.GetLength();
	if(spobjlen<=0)
	{
		return 0;
	}
	do
	{
		int FindIdx=temp.Find(FindMsg,startIdx);
		if(FindIdx<0)
		{
			break;
		}
		if(FindIdx==startIdx)
		{
			if(!NeedRemoveEmpty)
			{
				retcount++;
				CString spmsg;
				retls.Add(spmsg);
			}
			startIdx+=spobjlen;
		}else
		{
			retcount++;
			CString spmsg=temp.Mid(startIdx,FindIdx-startIdx);
			startIdx+=(FindIdx-startIdx)+spobjlen;
			retls.Add(spmsg);
		}
	}while(startIdx>=0);

	if(startIdx>=temp.GetLength())
	{
		if(!NeedRemoveEmpty)
		{
			retcount++;
			CString spmsg;
			retls.Add(spmsg);
		}
	}else
	{
		retcount++;
		CString spmsg=temp.Right(temp.GetLength()-startIdx);
		retls.Add(spmsg);
	}
	return retcount;
}

void CPresetList::BeNoSpace(char *pStr, int iStrLen)
{
	char *pp = pStr;
	char * pTemp=pStr;
	for(int idx=0; idx<iStrLen; idx++)
	{
		*pTemp=*pStr;
		if(!isspace(*pStr))
		{
			pTemp++;
		}
		pStr++;
	}
	*pTemp=0;
	pStr = pp;
};



void CDevCtrlDlgEx::StatusEvent(LONG lUser,LONG nStateCode,char *pResponse)
{
	if (nStateCode == EVENT_PTZPRESETINFO)
	{
		CMapAutoLock Lock;
		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)
			{
				if (pDevInfo->iUserID == lUser)
				{
					pDevInfo->pPresetList = _strdup(pResponse);
					break;
				}
			}
		}
	}
}