// DlgPtz.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgPtz dialog


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


void CDlgPtz::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgPtz)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgPtz, CDialog)
	//{{AFX_MSG_MAP(CDlgPtz)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgPtz message handlers

BOOL CDlgPtz::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CComboBox *pComboxBaudrate = (CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE);
	pComboxBaudrate->InsertString(0, "2400");
	pComboxBaudrate->InsertString(1, "4800");
	pComboxBaudrate->InsertString(2, "9600");
	pComboxBaudrate->InsertString(3, "19200");
	pComboxBaudrate->SetCurSel(2);
	CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO1);

	Ms_Ipc_PTZ ptzConfig = {0};
	if(!Ms_Ipc_GetPTZ(m_nUserId, &ptzConfig)){
		if(ptzConfig.dwBaudRate == 2400)
			pComboxBaudrate->SetCurSel(0);
		else if(ptzConfig.dwBaudRate == 4800)
			pComboxBaudrate->SetCurSel(1);
		else if(ptzConfig.dwBaudRate == 9600)
			pComboxBaudrate->SetCurSel(2);
		else
			pComboxBaudrate->SetCurSel(3);
		if(ptzConfig.nProtocol == 0)
			CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO1);
		else
			CheckRadioButton(IDC_RADIO1, IDC_RADIO3, IDC_RADIO2);
	}
	else
		CMsSDKTestDlg::OutputTips("Init ptz info failed. Please try again...");
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgPtz::OnOK() 
{
	CComboBox *pComboxBaudrate = (CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE);
	Ms_Ipc_PTZ ptzConfig = {0};
	int nSel = pComboxBaudrate->GetCurSel();

	if(nSel == 0)
		ptzConfig.dwBaudRate = 2400;
	else if(nSel == 1)
		ptzConfig.dwBaudRate = 4800;
	else if(nSel == 2)
		ptzConfig.dwBaudRate = 9600;
	else
		ptzConfig.dwBaudRate = 19200;
	if(GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO2) == IDC_RADIO1)
		ptzConfig.nProtocol = 0;
	else
		ptzConfig.nProtocol = 1;
	
	if(!Ms_Ipc_SetPTZ(m_nUserId, &ptzConfig)){
		AfxMessageBox("Successfully.");
	}

	//CDialog::OnOK();
}
