// DlgAudioConfig.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgAudioConfig dialog


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


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


BEGIN_MESSAGE_MAP(CDlgAudioConfig, CDialog)
	//{{AFX_MSG_MAP(CDlgAudioConfig)
	ON_BN_CLICKED(IDC_CHK_ENABLE, OnChkEnableAudio)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgAudioConfig message handlers

BOOL CDlgAudioConfig::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CComboBox *pCbMode = (CComboBox*)GetDlgItem(IDC_COMBO_MODE);
	pCbMode->InsertString(0, "Only Mic");
	pCbMode->InsertString(1, "Only Speaker");
	pCbMode->InsertString(2, "Both Mic & Speaker");
	CComboBox *pCbEncoding = (CComboBox*)GetDlgItem(IDC_COMBO_ENCODING);
	pCbEncoding->InsertString(0, "G711-ULAW");
	pCbEncoding->InsertString(1, "G711-ALAW");
	pCbEncoding->InsertString(2, "AAC LC");
	CSliderCtrl *sliderInput = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_INPUT);
	sliderInput->SetRange(0, 100);
	CSliderCtrl *sliderAlarmLevel = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_ALARM_LEVEL);
	sliderAlarmLevel->SetRange(1, 100);
	CSliderCtrl *sliderOutput = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_OUTPUT);
	sliderOutput->SetRange(0, 100);

	Ms_Ipc_AudioConfig audioConfig = {0};
	if(Ms_Ipc_GetAudioConfig(m_nUserId, &audioConfig))
	{
		CMsSDKTestDlg::OutputTips("Init audio info failed. Please try again...");
		return TRUE;
	}
	((CButton*)GetDlgItem(IDC_CHK_ENABLE))->SetCheck(audioConfig.bEnableAudio==0?FALSE:TRUE);
	OnChkEnableAudio();
	((CButton*)GetDlgItem(IDC_CHK_DENOISE_ENABLE))->SetCheck(audioConfig.bDenoiseEnable==0?FALSE:TRUE);
	m_bEnable = audioConfig.bEnableAudio;
	pCbMode->SetCurSel(audioConfig.nAudioMode);
	pCbEncoding->SetCurSel(audioConfig.nEncoding);
	sliderInput->SetPos(audioConfig.nInputGain);
	sliderAlarmLevel->SetPos(audioConfig.nAlarmLevel);
	sliderOutput->SetPos(audioConfig.nOutputVolume);
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgAudioConfig::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	CComboBox *pCbMode = (CComboBox*)GetDlgItem(IDC_COMBO_MODE);
	CComboBox *pCbEncoding = (CComboBox*)GetDlgItem(IDC_COMBO_ENCODING);
	CSliderCtrl *sliderInput = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_INPUT);
	CSliderCtrl *sliderAlarmLevel = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_ALARM_LEVEL);
	CSliderCtrl *sliderOutput = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_OUTPUT);
	Ms_Ipc_AudioConfig audioConfig = {0};
	audioConfig.bEnableAudio = ((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck();
	audioConfig.nAudioMode = pCbMode->GetCurSel();
	audioConfig.nInputGain = sliderOutput->GetPos();
	audioConfig.nAlarmLevel = sliderAlarmLevel->GetPos();
	audioConfig.nOutputVolume = sliderOutput->GetPos();
	audioConfig.bDenoiseEnable = ((CButton*)GetDlgItem(IDC_CHK_DENOISE_ENABLE))->GetCheck();
	audioConfig.nEncoding = pCbEncoding->GetCurSel();
	if(!Ms_Ipc_SetAudioConfig(m_nUserId, &audioConfig))
		CMsSDKTestDlg::OutputTips("Set Audio Config Successfully.");
	else{
		AfxMessageBox("Failed.");
		return;
	}
	if(m_bEnable != audioConfig.bEnableAudio){
		CMsSDKTestDlg::OutputTips("Set audio parameters now. Wait a moment...");
	}
	CDialog::OnOK();
}

void CDlgAudioConfig::OnChkEnableAudio() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck()){
		GetDlgItem(IDC_COMBO_MODE)->EnableWindow(TRUE);
		GetDlgItem(IDC_COMBO_ENCODING)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_DENOISE_ENABLE)->EnableWindow(TRUE);
	}else{
		GetDlgItem(IDC_COMBO_MODE)->EnableWindow(FALSE);
		GetDlgItem(IDC_COMBO_ENCODING)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_DENOISE_ENABLE)->EnableWindow(FALSE);
	}
}
