#include "StdAfx.h"
#include "NetSDKDLL.h"
#include "NetSDKDemo.h"
#include "WndCtrl.h"
#include "WndManager.h"
#include "DllPlayer.h"
#include "NetSDKDemoDlg.h"

CWndCtrl::CWndCtrl(int iIndex, CWnd* pParent)
:m_pParentWnd(pParent)
{
	m_iIndex = iIndex;
	m_hBrush = CreateSolidBrush(RGB(0, 0, 0));
	m_iWndUser = -1;
	m_bCurSel = FALSE;
	m_bMouseTrack = TRUE;
	m_bOpenDigitZoom = FALSE;
}

CWndCtrl::~CWndCtrl(void)
{
	m_iWndUser = -1;
	m_bCurSel = FALSE;
	m_bOpenDigitZoom = FALSE;
	DestroyWindow();
}

BOOL CWndCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;
	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;

	cs.lpszClass = AfxRegisterWndClass(
		CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), 
		m_hBrush, 
		NULL);

	return CWnd::PreCreateWindow(cs);
}

BEGIN_MESSAGE_MAP(CWndCtrl, CWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_KILLFOCUS()
	ON_WM_NCLBUTTONDBLCLK()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_MOUSEWHEEL()
	ON_WM_MOUSEMOVE()
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()



BOOL CWndCtrl::SetWindowFocus(BOOL bSelect)
{
	CDC *pDC = GetWindowDC();

	CRect rect;
	GetWindowRect(rect);
	
	ScreenToClient(rect);

 	CPen	*pOldPen = NULL;
// 
 	CPen	CRectPen(PS_SOLID,  2,  RGB(236, 233, 216));
 	CPen	CRectPen2(PS_SOLID, 2,  RGB(255, 0, 0 ));

	if (bSelect)
	{
		//pDC->Draw3dRect(rect, RGB(255, 0, 0), RGB(255, 0, 0));
		//pDC->FillRect(&rect, &CBrush(RGB(0,0,0)));
		pOldPen = pDC->SelectObject(&CRectPen2);
	}

	else
	{
		//pDC->Draw3dRect(rect, RGB(255, 255, 255), RGB(255, 255, 255));
		//pDC->FillRect(&rect, &CBrush(RGB(0,0,0)));
		pOldPen = pDC->SelectObject(&CRectPen);
		//pDC->FillRect(rect, RGB(255, 255, 255));
		//pDC->FillRect(rect, RGB(0, 0, 0), RGB(0, 0, 0));
	}

  	pDC->MoveTo(rect.left, rect.top);
// // 
  	pDC->LineTo(rect.right, rect.top);	
  	pDC->LineTo(rect.right, rect.bottom);	
  	pDC->LineTo(rect.left, rect.bottom);	
  	pDC->LineTo(rect.left, rect.top);
 
  	pOldPen = pDC->SelectObject(&CRectPen);
  	pDC->SelectObject(pOldPen);

	ReleaseDC(pDC);
	return TRUE;
}

void CWndCtrl::OnPaint()
{

	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CWnd::OnPaint()

 	CRect rcClient;
 	GetClientRect(&rcClient);
 	dc.FillSolidRect(0,0,rcClient.Width(),rcClient.Height(),RGB(0,0,0));
	if (((CWndManager *)GetParent())->m_iCurSelWndIndex == m_iIndex)
		SetWindowFocus(TRUE);
	else
		SetWindowFocus(FALSE);

// 	CDC *pDC = GetWindowDC();
// 
// 	CRect rect;
// 	GetWindowRect(rect);
// 
// 	ScreenToClient(rect);
// 	pDC->FillRect(&rect, RGB(0,0,0));


//	ReleaseDC(pDC);
}

void CWndCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	((CWndManager*)m_pParentWnd)->SetCurSelWnd(m_iIndex);
	SetWindowFocus(TRUE);

	// 查询预置列表
	int iUserID = 0;
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo  = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(m_strKey, pDevInfo);
		if (bFound)
		{
			iUserID = pDevInfo->iUserID;
		}
	}

	if (iUserID != 0)
	{	
		int iRet = IP_NET_DVR_PTZControlEx(iUserID, _T("<xml>\n<cmd>GetPresetList</cmd>\n</xml>"));	
	}
	((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pMenuDlg->SetBtnVoiceStatus();

	::SetFocus(m_ctlPicture.m_hWnd);
	CWnd::OnLButtonDown(nFlags, point);
}

void CWndCtrl::OnKillFocus(CWnd* pNewWnd)
{
	CWnd::OnKillFocus(pNewWnd);
	// TODO: 在此处添加消息处理程序代码
}

CString CWndCtrl::GetWndUser()
{
	return m_strKey;
}

void CWndCtrl::SetWndUser(LPCTSTR strKey)
{
	if (strKey == NULL)
		m_strKey = _T("");
	else
		m_strKey.Format("%s", strKey);
#if 1
	if (strKey == NULL)
	{
		CDC *pDC = GetWindowDC();

		CRect rect;
		GetWindowRect(rect);

		ScreenToClient(rect);
		pDC->FillSolidRect(&rect, RGB(0,0,0));


		ReleaseDC(pDC);
	}
#endif
}

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

	if ((pMsg->message >= WM_MOUSEFIRST) && (pMsg->message <= WM_MOUSEWHEEL))
	{
		if (m_iDigitPlayID>0 && m_bOpenDigitZoom)
		{
			if (pMsg->message == WM_MOUSEMOVE)
			{
				if (m_bMouseTrack)    //若允许追踪，则。
				{
					TRACKMOUSEEVENT csTME;
					csTME.cbSize = sizeof(csTME);
					csTME.dwFlags = TME_LEAVE|TME_HOVER;
					csTME.hwndTrack = m_hWnd;//指定要追踪的窗口
					csTME.dwHoverTime = 10;  //鼠标在按钮上停留超过10ms，才认为状态为HOVER
					::_TrackMouseEvent(&csTME); //开启Windows的WM_MOUSELEAVE，WM_MOUSEHOVER事件支持
					m_bMouseTrack=FALSE;   //若已经追踪，则停止追踪
				}
			}

			IP_TPS_InputMouseEvent(m_iDigitPlayID, pMsg->message, pMsg->wParam, pMsg->lParam);
		}
	}

	if (pMsg->message == WM_MOUSELEAVE || pMsg->message == WM_MOUSEHOVER)
	{
		if (m_iDigitPlayID>0 && m_bOpenDigitZoom)
		{
			IP_TPS_InputMouseEvent(m_iDigitPlayID, pMsg->message, pMsg->wParam, pMsg->lParam);
		}	
		if (pMsg->message == WM_MOUSELEAVE)
		{
			m_bMouseTrack = TRUE;
		}
	}
	return CWnd::PreTranslateMessage(pMsg);
}

void CWndCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	

	CWnd::OnLButtonDblClk(nFlags, point);
}


HWND CWndCtrl::GetPlayWnd()
{
	return m_ctlPicture.m_hWnd;
}

int CWndCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CRect rcClient;
	GetClientRect(&rcClient);
	rcClient.DeflateRect(1, 1);
	m_ctlPicture.Create(_T(""), WS_CHILD|WS_VISIBLE|SS_CENTER, 
		rcClient, this);
	
	return 0;
}

void CWndCtrl::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	CRect rcClient;
	GetClientRect(&rcClient);
	rcClient.DeflateRect(1, 1, 1, 1);
	m_ctlPicture.MoveWindow(rcClient);
	// TODO: 在此处添加消息处理程序代码
}

BOOL CWndCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}

void CWndCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CWnd::OnMouseMove(nFlags, point);
}


void CWndCtrl::OpenDigitZoom(BOOL bOpen, int lPlayID)
{
	m_bOpenDigitZoom = bOpen;
	m_iDigitPlayID = lPlayID;
}
HBRUSH CWndCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CWnd::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何属性
	static BOOL isfirst = TRUE;
	static CBrush oBrush;
	if (isfirst)
	{
		oBrush.CreateSolidBrush(RGB(0,0,0));
		isfirst = FALSE;
	}
	//static CBrush oBrush;

	return oBrush;
	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return NULL;
}
