// DlgAlarmConfig.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "MsSDKTestDlg.h"
#include "DlgAlarmConfig.h"
#include "MsIpcSDK.h"
#include "DlgSchedule.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgAlarmConfig dialog


CDlgAlarmConfig::CDlgAlarmConfig(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgAlarmConfig::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgAlarmConfig)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bMotion = FALSE;
	m_bAudioEnable = FALSE;
}


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


BEGIN_MESSAGE_MAP(CDlgAlarmConfig, CDialog)
	//{{AFX_MSG_MAP(CDlgAlarmConfig)
	ON_BN_CLICKED(IDC_BTN_MOTION_REGION, OnBtnMotionRegion)
	ON_BN_CLICKED(IDC_BTN_SCHEDULE, OnBtnSchedule)
	ON_BN_CLICKED(IDC_CHK_MOTION, OnChkMotion)
	ON_BN_CLICKED(IDC_CHK_ENABEL_ALARM, OnChkEnabelAlarm)
	ON_BN_CLICKED(IDC_CHK_AUDIO_OUTPUT, OnChkAudioOutput)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgAlarmConfig message handlers

BOOL CDlgAlarmConfig::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CComboBox *pAlarmDuration = (CComboBox*)GetDlgItem(IDC_COMBO_ALARM_DURATION);
	pAlarmDuration->InsertString(0, "10 Seconds");
	pAlarmDuration->InsertString(1, "30 Seconds");
	pAlarmDuration->InsertString(2, "1 Minute");
	pAlarmDuration->InsertString(3, "5 Minutes");
	pAlarmDuration->InsertString(4, "10 Minutes");
	pAlarmDuration->InsertString(5, "Non-Stop");
	pAlarmDuration->SetCurSel(1);
	CComboBox *pAudioFiles = (CComboBox*)GetDlgItem(IDC_COMBO_AUDIO_FILE);
	pAudioFiles->InsertString(0, "1.wav");
	pAudioFiles->InsertString(1, "2.wav");
	pAudioFiles->SetCurSel(0);

	CComboBox *pFtpFormat = (CComboBox*)GetDlgItem(IDC_COMBO_FTP_FORMAT);
	CComboBox *pSmtpFormat = (CComboBox*)GetDlgItem(IDC_COMBO_SMTP_FORMAT);
	CComboBox *pSdFormat = (CComboBox*)GetDlgItem(IDC_COMBO_SD_FORMAT);

	Ms_Ipc_Alarm alarmConfig = {0};
	Ms_Ipc_MotionConfig motionConfig = {0};
	alarmConfig.alarmEvent.pMotionConfig = &motionConfig;
	if(Ms_Ipc_GetAlarmConfig(m_nUserId, &alarmConfig))
	{
		CMsSDKTestDlg::OutputTips("Init alarm info failed. Please try again...");
		pFtpFormat->InsertString(0, "Not Avaiable");
		pSmtpFormat->InsertString(0, "Not Avaiable");
		pSdFormat->InsertString(0, "Not Avaiable");
		return TRUE;
	}

	((CButton*)GetDlgItem(IDC_CHK_ENABEL_ALARM))->SetCheck(alarmConfig.bEnable==0?FALSE:TRUE);
	m_bMotion = alarmConfig.alarmEvent.pMotionConfig->bEnable==0?FALSE:TRUE;
	((CButton*)GetDlgItem(IDC_CHK_MOTION))->SetCheck(m_bMotion);
	OnChkMotion();

	Ms_Ipc_AudioConfig audioConfig = {0};
	Ms_Ipc_GetAudioConfig(m_nUserId, &audioConfig);
	m_bAudioEnable = audioConfig.bEnableAudio==0?FALSE:TRUE;

	((CButton*)GetDlgItem(IDC_CHK_NETWORK_LOST))->SetCheck(alarmConfig.alarmEvent.bNetworkLostAlarm==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_AUDIO_INPUT))->SetCheck(alarmConfig.alarmEvent.bAudioInputAlarm==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_EXTEN_INPUT))->SetCheck(alarmConfig.alarmEvent.bExternalInputAlarm==0?FALSE:TRUE);

	((CButton*)GetDlgItem(IDC_CHK_FTP))->SetCheck(alarmConfig.alarmAction.bUploadToFtp==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_SMTP))->SetCheck(alarmConfig.alarmAction.bUploadToSmtp==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_SD))->SetCheck(alarmConfig.alarmAction.bUploadSdCard==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_EXTEN_OUTPUT))->SetCheck(alarmConfig.alarmAction.bExternalOutput==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_AUDIO_OUTPUT))->SetCheck(alarmConfig.alarmAction.bPlayAudio==0?FALSE:TRUE);

	if(!m_bAudioEnable){
		((CButton*)GetDlgItem(IDC_CHK_AUDIO_INPUT))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_AUDIO_OUTPUT))->SetCheck(FALSE);
		
		GetDlgItem(IDC_CHK_AUDIO_INPUT)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_AUDIO_OUTPUT)->EnableWindow(FALSE);
	}else{

		GetDlgItem(IDC_STATIC_WARNING1)->ShowWindow(FALSE);
		GetDlgItem(IDC_STATIC_WARNING2)->ShowWindow(FALSE);
	}
	OnChkAudioOutput();

	pFtpFormat->InsertString(0, ".avi");
	pFtpFormat->SetCurSel(0);
	pSmtpFormat->InsertString(0, ".avi");
	pSmtpFormat->SetCurSel(0);
	pSdFormat->InsertString(0, ".avi");
	pSdFormat->SetCurSel(0);
	pAlarmDuration->SetCurSel(alarmConfig.alarmEvent.nAlarmDuration);
	pAudioFiles->SetCurSel(alarmConfig.alarmAction.nAudioFile);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgAlarmConfig::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	CComboBox *pAlarmDuration = (CComboBox*)GetDlgItem(IDC_COMBO_ALARM_DURATION);
	CComboBox *pAudioFiles = (CComboBox*)GetDlgItem(IDC_COMBO_AUDIO_FILE);
	Ms_Ipc_Alarm alarmConfig = {0};
	Ms_Ipc_MotionConfig motionConfig = {0};	
	alarmConfig.bEnable = ((CButton*)GetDlgItem(IDC_CHK_ENABEL_ALARM))->GetCheck();
	if(m_bMotion != ((CButton*)GetDlgItem(IDC_CHK_MOTION))->GetCheck()){
		Ms_Ipc_GetMotionConifg(m_nUserId, &motionConfig);
		alarmConfig.alarmEvent.pMotionConfig = &motionConfig;
		alarmConfig.alarmEvent.pMotionConfig->bEnable = ((CButton*)GetDlgItem(IDC_CHK_MOTION))->GetCheck();
	}
	alarmConfig.alarmEvent.bNetworkLostAlarm = ((CButton*)GetDlgItem(IDC_CHK_NETWORK_LOST))->GetCheck();
	alarmConfig.alarmEvent.bAudioInputAlarm = ((CButton*)GetDlgItem(IDC_CHK_AUDIO_INPUT))->GetCheck();
	alarmConfig.alarmEvent.bExternalInputAlarm = ((CButton*)GetDlgItem(IDC_CHK_EXTEN_INPUT))->GetCheck();

	alarmConfig.alarmAction.bUploadToFtp = ((CButton*)GetDlgItem(IDC_CHK_FTP))->GetCheck();
	alarmConfig.alarmAction.bUploadToSmtp = ((CButton*)GetDlgItem(IDC_CHK_SMTP))->GetCheck();
	alarmConfig.alarmAction.bUploadSdCard = ((CButton*)GetDlgItem(IDC_CHK_SD))->GetCheck();
	alarmConfig.alarmAction.bExternalOutput = ((CButton*)GetDlgItem(IDC_CHK_EXTEN_OUTPUT))->GetCheck();
	alarmConfig.alarmAction.bPlayAudio = ((CButton*)GetDlgItem(IDC_CHK_AUDIO_OUTPUT))->GetCheck();
	alarmConfig.alarmEvent.nAlarmDuration= pAlarmDuration->GetCurSel();
	alarmConfig.alarmAction.nAudioFile = pAudioFiles->GetCurSel();
	
	if(!Ms_Ipc_SetAlarmConfig(m_nUserId, &alarmConfig)){
		CMsSDKTestDlg::OutputTips("Set Alarm Config Successsfully.");
	}else{
		AfxMessageBox("Failed.");
		return;
	}
	CDialog::OnOK();
}

void CDlgAlarmConfig::OnBtnMotionRegion() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgAlarmConfig::OnBtnSchedule() 
{
	// TODO: Add your control notification handler code here
	CDlgSchedule dlgSchedule(this);
	Ms_Ipc_MotionConfig motionConfig = {0};
	Ms_Ipc_GetMotionConifg(m_nUserId, &motionConfig);
	memcpy(&dlgSchedule.m_motionSchedule, motionConfig.motionSchedule, sizeof(motionConfig.motionSchedule));
	if(dlgSchedule.DoModal() == IDOK){
		memcpy(&motionConfig.motionSchedule, &dlgSchedule.m_motionSchedule, sizeof(dlgSchedule.m_motionSchedule));
		Ms_Ipc_SetMotionConifg(m_nUserId, &motionConfig);
	}
}

void CDlgAlarmConfig::OnChkMotion() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_MOTION))->GetCheck())
	{
		GetDlgItem(IDC_BTN_SCHEDULE)->EnableWindow(TRUE);
	}else{
		GetDlgItem(IDC_BTN_SCHEDULE)->EnableWindow(FALSE);
	}
}

void CDlgAlarmConfig::OnChkEnabelAlarm() 
{
	// TODO: Add your control notification handler code here
	
	if(((CButton*)GetDlgItem(IDC_CHK_ENABEL_ALARM))->GetCheck())
	{
		GetDlgItem(IDC_CHK_MOTION)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_NETWORK_LOST)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_AUDIO_INPUT)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_EXTEN_INPUT)->EnableWindow(TRUE);
		GetDlgItem(IDC_COMBO_ALARM_DURATION)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_FTP)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_SMTP)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_SD)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_EXTEN_OUTPUT)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_AUDIO_OUTPUT)->EnableWindow(TRUE);
	}else{
		((CButton*)GetDlgItem(IDC_CHK_MOTION))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_NETWORK_LOST))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_AUDIO_INPUT))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_EXTEN_INPUT))->SetCheck(FALSE);
		GetDlgItem(IDC_CHK_MOTION)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_NETWORK_LOST)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_AUDIO_INPUT)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_EXTEN_INPUT)->EnableWindow(FALSE);
		GetDlgItem(IDC_COMBO_ALARM_DURATION)->EnableWindow(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_FTP))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_SMTP))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_SD))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_EXTEN_OUTPUT))->SetCheck(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_AUDIO_OUTPUT))->SetCheck(FALSE);
		GetDlgItem(IDC_CHK_FTP)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_SMTP)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_SD)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_EXTEN_OUTPUT)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_AUDIO_OUTPUT)->EnableWindow(FALSE);
	}
	OnChkMotion();
	OnChkAudioOutput();
}

void CDlgAlarmConfig::OnChkAudioOutput() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_AUDIO_OUTPUT))->GetCheck())
	{
		GetDlgItem(IDC_COMBO_AUDIO_FILE)->EnableWindow(TRUE);
	}else{
		GetDlgItem(IDC_COMBO_AUDIO_FILE)->EnableWindow(FALSE);
	}
}
