// DlgMask.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "MsSDKTestDlg.h"
#include "DlgMask.h"
#include "MsIpcSDK.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgMask dialog


CDlgMask::CDlgMask(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgMask::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgMask)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgMask::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgMask)
	DDX_Control(pDX, IDC_STATIC_PLAY, m_wndPlay);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgMask, CDialog)
	//{{AFX_MSG_MAP(CDlgMask)
	ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)
	ON_CBN_SELCHANGE(IDC_COMBO_REGION, OnSelchangeComboRegion)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_CHK_ENABLE, OnChkEnable)
	ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgMask message handlers

BOOL CDlgMask::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CComboBox *pRegion = (CComboBox*)GetDlgItem(IDC_COMBO_REGION);
	pRegion->InsertString(0, "1");
	pRegion->InsertString(1, "2");
	pRegion->InsertString(2, "3");
	pRegion->InsertString(3, "4");
	pRegion->SetCurSel(0);
	CComboBox *pType = (CComboBox*)GetDlgItem(IDC_COMBO_TYPE);
	pType->InsertString(0, "White");
	pType->InsertString(1, "Black");
	pType->InsertString(2, "White and Black Mosaic");
	pType->SetCurSel(0);
	
	//Ms_Ipc_Mask mask = {0};
	//Ms_Ipc_GetPrivacyMask(m_nUserId, &mask);
	Mask_Config mask = {0};
	if(Ms_Ipc_GetPrivacyMaskConfig(m_nUserId, 0, &mask))
	{
		CMsSDKTestDlg::OutputTips("Init privacy mask info failed. Please try again...");
		return TRUE;
	}
	((CButton*)GetDlgItem(IDC_CHK_ENABLE))->SetCheck(mask.nMaskEnable==1?TRUE:FALSE);
	pType->SetCurSel(mask.nMaskType);
	UpdateData(FALSE);
	OnChkEnable();
	Ms_Ipc_ClientInfo clientInfo = {0};
	clientInfo.nProtocol = IPC_RTSP_PROTOCOL_UDP;
	clientInfo.hPlayWnd = NULL;//m_wndPlay.m_hWnd;
	clientInfo.nDisplayStream = IPC_STREAM_TYPE_MAIN;
	
	LONG nResult = Ms_IpcClient_LivePlay(m_nUserId, &clientInfo, PlayCallBack, &m_wndPlay);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgMask::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	CComboBox *pRegion = (CComboBox*)GetDlgItem(IDC_COMBO_REGION);
	CComboBox *pType = (CComboBox*)GetDlgItem(IDC_COMBO_TYPE);
	int nRegion = pRegion->GetCurSel();
	int nType = pType->GetCurSel();
	int nEnable = ((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck();
	Mask_Config mask = {0};
	if(!nEnable){
		mask.nMaskEnable = 0;
		Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, 0, &mask);
		Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, 1, &mask);
		Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, 2, &mask);
		Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, 3, &mask);
	}else{
		mask.nMaskEnable = 1;
		mask.nMaskType = nType;		
		CRect rcMask;
		m_wndPlay.GetMaskRect(&rcMask);
		if(rcMask.Width()*rcMask.Height() <= 0){
			Ms_Ipc_GetPrivacyMaskConfig(m_nUserId, nRegion, &mask);
			mask.nMaskEnable = 1;
			mask.nMaskType = nType;	
		}else{
			mask.nMaskLeft = rcMask.left;
			mask.nMaskTop = rcMask.top;
			mask.nMaskRight = rcMask.right;
			mask.nMaskBottom = rcMask.bottom;
		}
		if(!Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, nRegion, &mask)){
			//AfxMessageBox("Successfully.");
		}else{
			AfxMessageBox("Failed.");
		}
	}
//	CDialog::OnOK();
}

void CDlgMask::OnBtnSet() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CComboBox *pRegion = (CComboBox*)GetDlgItem(IDC_COMBO_REGION);
	CComboBox *pType = (CComboBox*)GetDlgItem(IDC_COMBO_TYPE);
	int nRegion = pRegion->GetCurSel();
	int nType = pType->GetCurSel();
	int nEnable = ((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck();
	Mask_Config mask = {0};
	if(!nEnable){
		mask.nMaskEnable = 0;
		if(!Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, nRegion, &mask)){
			//AfxMessageBox("Successfully");
		}else{
			AfxMessageBox("Failed.");
		}
	}else{
		mask.nMaskEnable = 1;
		mask.nMaskType = nType;		
		CRect rcMask;
		m_wndPlay.GetMaskRect(&rcMask);
		if(rcMask.Width()*rcMask.Height() <= 0){
			Ms_Ipc_GetPrivacyMaskConfig(m_nUserId, nRegion, &mask);
			mask.nMaskEnable = 1;
			mask.nMaskType = nType;	
		}else{
			mask.nMaskLeft = rcMask.left;
			mask.nMaskTop = rcMask.top;
			mask.nMaskRight = rcMask.right;
			mask.nMaskBottom = rcMask.bottom;
		}
		if(!Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, nRegion, &mask)){
			//AfxMessageBox("Successfully.");
		}else{
			AfxMessageBox("Failed.");
		}
	}
}

void CDlgMask::OnBtnClear() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CComboBox *pRegion = (CComboBox*)GetDlgItem(IDC_COMBO_REGION);
	CComboBox *pType = (CComboBox*)GetDlgItem(IDC_COMBO_TYPE);
	int nRegion = pRegion->GetCurSel();
	Mask_Config mask = {0};
	mask.nMaskEnable = ((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck();
	mask.nMaskType = pType->GetCurSel();
	if(Ms_Ipc_SetPrivacyMaskConfig(m_nUserId, nRegion, &mask))
		AfxMessageBox("Failed.");
}

void CDlgMask::OnSelchangeComboRegion() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);	
	CComboBox *pRegion = (CComboBox*)GetDlgItem(IDC_COMBO_REGION);
	int nRegin = pRegion->GetCurSel();
	Mask_Config mask = {0};
	Ms_Ipc_GetPrivacyMaskConfig(m_nUserId, nRegin, &mask);
	CComboBox *pType = (CComboBox*)GetDlgItem(IDC_COMBO_TYPE);
	pType->SetCurSel(mask.nMaskType);
// 	((CButton*)GetDlgItem(IDC_CHK_ENABLE))->SetCheck(mask.nMaskEnable==1?TRUE:FALSE);
// 	OnChkEnable();
	UpdateData(FALSE);	
}

void CDlgMask::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	Ms_IpcClient_StopLivePlay(m_nUserId);
}

void CDlgMask::OnChkEnable() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck())
	{
		m_wndPlay.SetMaskShow(TRUE);
		GetDlgItem(IDC_COMBO_REGION)->EnableWindow(TRUE);
//		GetDlgItem(IDC_BTN_SET)->EnableWindow(TRUE);
		GetDlgItem(IDC_COMBO_TYPE)->EnableWindow(TRUE);
	}else{
		m_wndPlay.SetMaskShow(FALSE);
		GetDlgItem(IDC_COMBO_REGION)->EnableWindow(FALSE);
//		GetDlgItem(IDC_BTN_SET)->EnableWindow(FALSE);
		GetDlgItem(IDC_COMBO_TYPE)->EnableWindow(FALSE);
	}
	
}
