// DlgCmosConfig.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgCmosConfig dialog


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


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


BEGIN_MESSAGE_MAP(CDlgCmosConfig, CDialog)
	//{{AFX_MSG_MAP(CDlgCmosConfig)
	ON_CBN_SELCHANGE(IDC_COMBO_IRCUT, OnSelchangeIRCut)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCmosConfig message handlers

BOOL CDlgCmosConfig::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CComboBox *pBackRegion = (CComboBox*)GetDlgItem(IDC_COMBO_BACK_REGION);
	pBackRegion->InsertString(0, "Top");
	pBackRegion->InsertString(1, "Middle");
	pBackRegion->InsertString(2, "Bottom");
	pBackRegion->SetCurSel(1);
	CComboBox *pFickerControl = (CComboBox*)GetDlgItem(IDC_COMBO_FICK_CONTROL);
	pFickerControl->InsertString(0, "60HZ");
	pFickerControl->InsertString(1, "50HZ");
	pFickerControl->SetCurSel(0);
	CComboBox *pIrcutMode = (CComboBox*)GetDlgItem(IDC_COMBO_IRCUT);
	pIrcutMode->InsertString(0, "Night");
	pIrcutMode->InsertString(1, "Day");
	pIrcutMode->InsertString(2, "Auto");
	pIrcutMode->InsertString(3, "Customize");
	pIrcutMode->SetCurSel(2);
	CComboBox *pStartHour = (CComboBox*)GetDlgItem(IDC_COMBO_START_HOUR);
	pStartHour->InsertString(0, "00");
	pStartHour->InsertString(1, "01");
	pStartHour->InsertString(2, "02");
	pStartHour->InsertString(3, "03");
	pStartHour->InsertString(4, "04");
	pStartHour->InsertString(5, "05");
	pStartHour->InsertString(6, "06");
	pStartHour->InsertString(7, "07");
	pStartHour->InsertString(8, "08");
	pStartHour->InsertString(9, "09");
	pStartHour->InsertString(10, "10");
	pStartHour->InsertString(11, "11");
	pStartHour->InsertString(12, "12");
	pStartHour->InsertString(13, "13");
	pStartHour->InsertString(14, "14");
	pStartHour->InsertString(15, "15");
	pStartHour->InsertString(16, "16");
	pStartHour->InsertString(17, "17");
	pStartHour->InsertString(18, "18");
	pStartHour->InsertString(19, "19");
	pStartHour->InsertString(20, "20");
	pStartHour->InsertString(21, "21");
	pStartHour->InsertString(22, "22");
	pStartHour->InsertString(23, "23");
	pStartHour->SetCurSel(18);
	CComboBox *pStartMinute = (CComboBox*)GetDlgItem(IDC_COMBO_START_MIN);
	pStartMinute->InsertString(0, "00");
	pStartMinute->InsertString(1, "01");
	pStartMinute->InsertString(2, "02");
	pStartMinute->InsertString(3, "03");
	pStartMinute->InsertString(4, "04");
	pStartMinute->InsertString(5, "05");
	pStartMinute->InsertString(6, "06");
	pStartMinute->InsertString(7, "07");
	pStartMinute->InsertString(8, "08");
	pStartMinute->InsertString(9, "09");
	pStartMinute->InsertString(10, "10");
	pStartMinute->InsertString(11, "11");
	pStartMinute->InsertString(12, "12");
	pStartMinute->InsertString(13, "13");
	pStartMinute->InsertString(14, "14");
	pStartMinute->InsertString(15, "15");
	pStartMinute->InsertString(16, "16");
	pStartMinute->InsertString(17, "17");
	pStartMinute->InsertString(18, "18");
	pStartMinute->InsertString(19, "19");
	pStartMinute->InsertString(20, "20");
	pStartMinute->InsertString(21, "21");
	pStartMinute->InsertString(22, "22");
	pStartMinute->InsertString(23, "23");
	pStartMinute->InsertString(24, "24");
	pStartMinute->InsertString(25, "25");
	pStartMinute->InsertString(26, "26");
	pStartMinute->InsertString(27, "27");
	pStartMinute->InsertString(28, "28");
	pStartMinute->InsertString(29, "29");
	pStartMinute->InsertString(30, "30");
	pStartMinute->InsertString(31, "31");
	pStartMinute->InsertString(32, "32");
	pStartMinute->InsertString(33, "33");
	pStartMinute->InsertString(34, "34");
	pStartMinute->InsertString(35, "35");
	pStartMinute->InsertString(36, "36");
	pStartMinute->InsertString(37, "37");
	pStartMinute->InsertString(38, "38");
	pStartMinute->InsertString(39, "39");
	pStartMinute->InsertString(40, "40");
	pStartMinute->InsertString(41, "41");
	pStartMinute->InsertString(42, "42");
	pStartMinute->InsertString(43, "43");
	pStartMinute->InsertString(44, "44");
	pStartMinute->InsertString(45, "45");
	pStartMinute->InsertString(46, "46");
	pStartMinute->InsertString(47, "47");
	pStartMinute->InsertString(48, "48");
	pStartMinute->InsertString(49, "49");
	pStartMinute->InsertString(50, "50");
	pStartMinute->InsertString(51, "51");
	pStartMinute->InsertString(52, "52");
	pStartMinute->InsertString(53, "53");
	pStartMinute->InsertString(54, "54");
	pStartMinute->InsertString(55, "55");
	pStartMinute->InsertString(56, "56");
	pStartMinute->InsertString(57, "57");
	pStartMinute->InsertString(58, "58");
	pStartMinute->InsertString(59, "59");
	pStartMinute->SetCurSel(0);
	CComboBox *pStopHour = (CComboBox*)GetDlgItem(IDC_COMBO_STOP_HOUR);
	pStopHour->InsertString(0, "00");
	pStopHour->InsertString(1, "01");
	pStopHour->InsertString(2, "02");
	pStopHour->InsertString(3, "03");
	pStopHour->InsertString(4, "04");
	pStopHour->InsertString(5, "05");
	pStopHour->InsertString(6, "06");
	pStopHour->InsertString(7, "07");
	pStopHour->InsertString(8, "08");
	pStopHour->InsertString(9, "09");
	pStopHour->InsertString(10, "10");
	pStopHour->InsertString(11, "11");
	pStopHour->InsertString(12, "12");
	pStopHour->InsertString(13, "13");
	pStopHour->InsertString(14, "14");
	pStopHour->InsertString(15, "15");
	pStopHour->InsertString(16, "16");
	pStopHour->InsertString(17, "17");
	pStopHour->InsertString(18, "18");
	pStopHour->InsertString(19, "19");
	pStopHour->InsertString(20, "20");
	pStopHour->InsertString(21, "21");
	pStopHour->InsertString(22, "22");
	pStopHour->InsertString(23, "23");
	pStopHour->SetCurSel(6);
	CComboBox *pStopMinute = (CComboBox*)GetDlgItem(IDC_COMBO_STOP_MIN);
	pStopMinute->InsertString(0, "00");
	pStopMinute->InsertString(1, "01");
	pStopMinute->InsertString(2, "02");
	pStopMinute->InsertString(3, "03");
	pStopMinute->InsertString(4, "04");
	pStopMinute->InsertString(5, "05");
	pStopMinute->InsertString(6, "06");
	pStopMinute->InsertString(7, "07");
	pStopMinute->InsertString(8, "08");
	pStopMinute->InsertString(9, "09");
	pStopMinute->InsertString(10, "10");
	pStopMinute->InsertString(11, "11");
	pStopMinute->InsertString(12, "12");
	pStopMinute->InsertString(13, "13");
	pStopMinute->InsertString(14, "14");
	pStopMinute->InsertString(15, "15");
	pStopMinute->InsertString(16, "16");
	pStopMinute->InsertString(17, "17");
	pStopMinute->InsertString(18, "18");
	pStopMinute->InsertString(19, "19");
	pStopMinute->InsertString(20, "20");
	pStopMinute->InsertString(21, "21");
	pStopMinute->InsertString(22, "22");
	pStopMinute->InsertString(23, "23");
	pStopMinute->InsertString(24, "24");
	pStopMinute->InsertString(25, "25");
	pStopMinute->InsertString(26, "26");
	pStopMinute->InsertString(27, "27");
	pStopMinute->InsertString(28, "28");
	pStopMinute->InsertString(29, "29");
	pStopMinute->InsertString(30, "30");
	pStopMinute->InsertString(31, "31");
	pStopMinute->InsertString(32, "32");
	pStopMinute->InsertString(33, "33");
	pStopMinute->InsertString(34, "34");
	pStopMinute->InsertString(35, "35");
	pStopMinute->InsertString(36, "36");
	pStopMinute->InsertString(37, "37");
	pStopMinute->InsertString(38, "38");
	pStopMinute->InsertString(39, "39");
	pStopMinute->InsertString(40, "40");
	pStopMinute->InsertString(41, "41");
	pStopMinute->InsertString(42, "42");
	pStopMinute->InsertString(43, "43");
	pStopMinute->InsertString(44, "44");
	pStopMinute->InsertString(45, "45");
	pStopMinute->InsertString(46, "46");
	pStopMinute->InsertString(47, "47");
	pStopMinute->InsertString(48, "48");
	pStopMinute->InsertString(49, "49");
	pStopMinute->InsertString(50, "50");
	pStopMinute->InsertString(51, "51");
	pStopMinute->InsertString(52, "52");
	pStopMinute->InsertString(53, "53");
	pStopMinute->InsertString(54, "54");
	pStopMinute->InsertString(55, "55");
	pStopMinute->InsertString(56, "56");
	pStopMinute->InsertString(57, "57");
	pStopMinute->InsertString(58, "58");
	pStopMinute->InsertString(59, "59");
	pStopMinute->SetCurSel(0);
	CComboBox *pLocalDisplay = (CComboBox*)GetDlgItem(IDC_COMBO_LOCAL_DISPLAY);
	pLocalDisplay->InsertString(0, "OFF");
	pLocalDisplay->InsertString(1, "NTSC");
	pLocalDisplay->InsertString(2, "PAL");
	pLocalDisplay->SetCurSel(0);
	CComboBox *pMirror = (CComboBox*)GetDlgItem(IDC_COMBO_MIRROR);
	pMirror->InsertString(0, "Normal");
	pMirror->InsertString(1, "Flip Horizontal");
	pMirror->InsertString(2, "Flip Vertical");
	pMirror->InsertString(3, "Rotating 180～");
	pMirror->SetCurSel(0);

	Ms_Ipc_ImagingConfig imgConfig = {0};
	if(Ms_Ipc_GetImagingConfig(m_nUserId, &imgConfig))
	{
		CMsSDKTestDlg::OutputTips("Init cmos info failed. Please try again...");
		return TRUE;
	}
	((CButton*)GetDlgItem(IDC_CHK_BACK_LIGHT))->SetCheck(imgConfig.bBackLightCompensation==0?FALSE:TRUE);
	pBackRegion->SetCurSel(imgConfig.nBackLightRegion);
	if(imgConfig.nFlickerControl == 60)
		pFickerControl->SetCurSel(0);
	else
		pFickerControl->SetCurSel(1);
	pIrcutMode->SetCurSel(imgConfig.nIRCUTMode);
	OnSelchangeIRCut();
	pStartHour->SetCurSel(imgConfig.custimeStartTime.nHour);
	pStartMinute->SetCurSel(imgConfig.custimeStartTime.nMinute);
	pStopHour->SetCurSel(imgConfig.custimeStopTime.nHour);
	pStopMinute->SetCurSel(imgConfig.custimeStopTime.nMinute);
	pLocalDisplay->SetCurSel(imgConfig.nLocalDisplay);
	pMirror->SetCurSel(imgConfig.nMirror);
	((CButton*)GetDlgItem(IDC_CHK_SNF))->SetCheck(imgConfig.bNoiseSpaceReduction==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_TNF))->SetCheck(imgConfig.bNoiseTemporalReduction==0?FALSE:TRUE);
	((CButton*)GetDlgItem(IDC_CHK_WIDE))->SetCheck(imgConfig.bIsWideDynamic==0?FALSE:TRUE);
	UpdateData(FALSE);

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

void CDlgCmosConfig::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	CComboBox *pBackRegion = (CComboBox*)GetDlgItem(IDC_COMBO_BACK_REGION);
	CComboBox *pFickerControl = (CComboBox*)GetDlgItem(IDC_COMBO_FICK_CONTROL);
	CComboBox *pIrcutMode = (CComboBox*)GetDlgItem(IDC_COMBO_IRCUT);
	CComboBox *pStartHour = (CComboBox*)GetDlgItem(IDC_COMBO_START_HOUR);
	CComboBox *pStartMinute = (CComboBox*)GetDlgItem(IDC_COMBO_START_MIN);
	CComboBox *pStopHour = (CComboBox*)GetDlgItem(IDC_COMBO_STOP_HOUR);
	CComboBox *pStopMinute = (CComboBox*)GetDlgItem(IDC_COMBO_STOP_MIN);
	CComboBox *pLocalDisplay = (CComboBox*)GetDlgItem(IDC_COMBO_LOCAL_DISPLAY);
	CComboBox *pMirror = (CComboBox*)GetDlgItem(IDC_COMBO_MIRROR);

	Ms_Ipc_ImagingConfig imgConfig = {0};
	Ms_Ipc_GetImagingConfig(m_nUserId, &imgConfig);
	imgConfig.bBackLightCompensation = ((CButton*)GetDlgItem(IDC_CHK_BACK_LIGHT))->GetCheck();
	imgConfig.bNoiseSpaceReduction = 0;/*((CButton*)GetDlgItem(IDC_CHK_SNF))->GetCheck();*/ //謗跺趼僇眒冪ィ蚚
	imgConfig.bNoiseTemporalReduction = 0;/*((CButton*)GetDlgItem(IDC_CHK_TNF))->GetCheck();*/
	imgConfig.bIsWideDynamic = ((CButton*)GetDlgItem(IDC_CHK_WIDE))->GetCheck();

	imgConfig.nBackLightRegion = pBackRegion->GetCurSel();
	if(pFickerControl->GetCurSel() == 0)
		imgConfig.nFlickerControl = 60;
	else
		imgConfig.nFlickerControl = 50;
	imgConfig.nIRCUTMode = pIrcutMode->GetCurSel();
	imgConfig.custimeStartTime.nHour = pStartHour->GetCurSel();
	imgConfig.custimeStartTime.nMinute = pStartMinute->GetCurSel();
	imgConfig.custimeStopTime.nHour = pStopHour->GetCurSel();
	imgConfig.custimeStopTime.nMinute = pStopMinute->GetCurSel();
	imgConfig.nLocalDisplay = pLocalDisplay->GetCurSel();
	imgConfig.nMirror = pMirror->GetCurSel();

	if(!Ms_Ipc_SetImagingConfig(m_nUserId, &imgConfig))
		CMsSDKTestDlg::OutputTips("Set Image Config Successfully.");
	else{
		AfxMessageBox("Failed.");
		return;
	}
	CDialog::OnOK();
}

void CDlgCmosConfig::OnSelchangeIRCut() 
{
	// TODO: Add your control notification handler code here
	if(((CComboBox*)GetDlgItem(IDC_COMBO_IRCUT))->GetCurSel() != 3)
	{
		(CComboBox*)GetDlgItem(IDC_COMBO_START_HOUR)->EnableWindow(FALSE);
		(CComboBox*)GetDlgItem(IDC_COMBO_START_MIN)->EnableWindow(FALSE);
		(CComboBox*)GetDlgItem(IDC_COMBO_STOP_HOUR)->EnableWindow(FALSE);
		(CComboBox*)GetDlgItem(IDC_COMBO_STOP_MIN)->EnableWindow(FALSE);
	}
	else
	{
		(CComboBox*)GetDlgItem(IDC_COMBO_START_HOUR)->EnableWindow(TRUE);
		(CComboBox*)GetDlgItem(IDC_COMBO_START_MIN)->EnableWindow(TRUE);
		(CComboBox*)GetDlgItem(IDC_COMBO_STOP_HOUR)->EnableWindow(TRUE);
		(CComboBox*)GetDlgItem(IDC_COMBO_STOP_MIN)->EnableWindow(TRUE);
	}
}
