// DlgSchedule.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "DlgSchedule.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgSchedule dialog


CDlgSchedule::CDlgSchedule(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSchedule::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSchedule)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	memset(&m_motionSchedule, 0, sizeof(m_motionSchedule));
}


void CDlgSchedule::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSchedule)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSchedule, CDialog)
	//{{AFX_MSG_MAP(CDlgSchedule)
	ON_BN_CLICKED(IDC_CHK_SELECT_ALL, OnChkSelectAll)
	ON_BN_CLICKED(IDC_CHK_1, OnChkSunday)
	ON_BN_CLICKED(IDC_CHK_2, OnChkMonday)
	ON_BN_CLICKED(IDC_CHK_3, OnChkTuesday)
	ON_BN_CLICKED(IDC_CHK_4, OnChkWednesday)
	ON_BN_CLICKED(IDC_CHK_5, OnChkThursday)
	ON_BN_CLICKED(IDC_CHK_6, OnChkFriday)
	ON_BN_CLICKED(IDC_CHK_7, OnChkSaturday)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSchedule message handlers
BOOL CDlgSchedule::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CTime starttime(2012, 1, 1, 0, 0, 0);
	int id = 0;
	for(id = IDC_DATETIMEPICKER_START1_1; id <= IDC_DATETIMEPICKER_STOP7_3; id ++)
	{
		((CDateTimeCtrl*)GetDlgItem(id))->SetFormat(_T("HH:mm"));
		((CDateTimeCtrl*)GetDlgItem(id))->SetTime(&starttime);
	}

	CTime stoptime(2012, 1, 1, 23, 59, 0);
	for(int idStop1 = IDC_DATETIMEPICKER_STOP1_1; idStop1 <= IDC_DATETIMEPICKER_STOP7_1; idStop1 += 6)
	{
		//((CDateTimeCtrl*)GetDlgItem(id))->SetFormat(_T("HH:mm"));
		((CDateTimeCtrl*)GetDlgItem(idStop1))->SetTime(&stoptime);
	}

	for(id = IDC_CHK_1; id <= IDC_CHK_7; id ++)
	{
		((CButton*)GetDlgItem(id))->SetCheck(m_motionSchedule[id-IDC_CHK_1].bEnable);
	}
	OnChkSunday();
	OnChkMonday();
	OnChkTuesday();
	OnChkWednesday();
	OnChkThursday();
	OnChkFriday();
	OnChkSaturday();
	SYSTEMTIME systemTime = {0};
	systemTime.wYear = 2012;
	systemTime.wMonth = 1;
	systemTime.wDay = 1;
	for(int i = 0; i < 7; i ++)
	{
		for(int j = 0; j < 3; j++)
		{
			systemTime.wHour = m_motionSchedule[i].tStart[j].nHour;
			systemTime.wMinute = m_motionSchedule[i].tStart[j].nMinute;
			((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START1_1+i*6+j*2))->SetTime(&systemTime);
			systemTime.wHour = m_motionSchedule[i].tStop[j].nHour;
			systemTime.wMinute = m_motionSchedule[i].tStop[j].nMinute;
			((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START1_1+i*6+j*2+1))->SetTime(&systemTime);
		}
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSchedule::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE); 
	for(int id = IDC_CHK_1; id <= IDC_CHK_7; id ++)
	{
		m_motionSchedule[id-IDC_CHK_1].bEnable = ((CButton*)GetDlgItem(id))->GetCheck();
	}

	m_motionSchedule[0].nWeekDay = 7; // Sunday
	m_motionSchedule[1].nWeekDay = 1;
	m_motionSchedule[2].nWeekDay = 2;
	m_motionSchedule[3].nWeekDay = 3;
	m_motionSchedule[4].nWeekDay = 4;
	m_motionSchedule[5].nWeekDay = 5;
	m_motionSchedule[6].nWeekDay = 6;

	SYSTEMTIME systemTime = {0};
	for(int i = 0; i < 7; i ++)
	{
		for(int j = 0; j < 3; j++)
		{
			((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START1_1+i*6+j*2))->GetTime(&systemTime);
			m_motionSchedule[i].tStart[j].nHour = (BYTE)systemTime.wHour;
			m_motionSchedule[i].tStart[j].nMinute = (BYTE)systemTime.wMinute;
			((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START1_1+i*6+j*2+1))->GetTime(&systemTime);
			m_motionSchedule[i].tStop[j].nHour = (BYTE)systemTime.wHour;
			m_motionSchedule[i].tStop[j].nMinute = (BYTE)systemTime.wMinute;

		}
	}
	CDialog::OnOK();
}

void CDlgSchedule::OnChkSelectAll() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	int nCheck = ((CButton*)GetDlgItem(IDC_CHK_SELECT_ALL))->GetCheck();
	for(int id = IDC_CHK_1; id <= IDC_CHK_7; id ++)
	{
		((CButton*)GetDlgItem(id))->SetCheck(nCheck);
	}
	UpdateData(FALSE);
	OnChkSunday();
	OnChkMonday();
	OnChkTuesday();
	OnChkWednesday();
	OnChkThursday();
	OnChkFriday();
	OnChkSaturday();
}

void CDlgSchedule::OnChkSunday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_1))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START1_1; id <= IDC_DATETIMEPICKER_STOP1_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START1_1; id <= IDC_DATETIMEPICKER_STOP1_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}

void CDlgSchedule::OnChkMonday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_2))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START2_1; id <= IDC_DATETIMEPICKER_STOP2_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START2_1; id <= IDC_DATETIMEPICKER_STOP2_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}

void CDlgSchedule::OnChkTuesday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_3))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START3_1; id <= IDC_DATETIMEPICKER_STOP3_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START3_1; id <= IDC_DATETIMEPICKER_STOP3_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}

void CDlgSchedule::OnChkWednesday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_4))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START4_1; id <= IDC_DATETIMEPICKER_STOP4_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START4_1; id <= IDC_DATETIMEPICKER_STOP4_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}

void CDlgSchedule::OnChkThursday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_5))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START5_1; id <= IDC_DATETIMEPICKER_STOP5_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START5_1; id <= IDC_DATETIMEPICKER_STOP5_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}

void CDlgSchedule::OnChkFriday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_6))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START6_1; id <= IDC_DATETIMEPICKER_STOP6_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START6_1; id <= IDC_DATETIMEPICKER_STOP6_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}

void CDlgSchedule::OnChkSaturday() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_7))->GetCheck())
	{
		for(int id = IDC_DATETIMEPICKER_START7_1; id <= IDC_DATETIMEPICKER_STOP7_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(TRUE);
		}
	}else{
		for(int id = IDC_DATETIMEPICKER_START7_1; id <= IDC_DATETIMEPICKER_STOP7_3; id ++)
		{
			GetDlgItem(id)->EnableWindow(FALSE);
		}
	}
}
