// DlgReportingOptions.cpp : implementation file
//

#include "stdafx.h"
#include "6046dryend.h"
#include "6046dryendDlg.h"
#include "DlgReportingOptions.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgReportingOptions dialog


CDlgReportingOptions::CDlgReportingOptions(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgReportingOptions::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgReportingOptions)
	//}}AFX_DATA_INIT

	m_bUserChange = FALSE;
}


void CDlgReportingOptions::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgReportingOptions)
	DDX_Control(pDX, IDOK, m_ctrlOK);
	DDX_Control(pDX, IDCANCEL, m_ctrlCancel);
	DDX_Control(pDX, IDC_EDIT_HEALTHINT, m_ctrlEditInterval);
	DDX_Control(pDX, IDC_CHECK_QUERYCNCT, m_ctrlQueryHealth);
	DDX_Control(pDX, IDC_CHECK_AUTOHEALTH, m_ctrlAutoHealth);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgReportingOptions, CDialog)
	//{{AFX_MSG_MAP(CDlgReportingOptions)
	ON_BN_CLICKED(IDC_CHECK_QUERYCNCT, OnCheckQuerycnct)
	ON_BN_CLICKED(IDC_CHECK_AUTOHEALTH, OnCheckAutohealth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgReportingOptions message handlers

void CDlgReportingOptions::OnOK() 
{
	// TODO: Add extra validation here
	CString temp;
	m_ctrlEditInterval.GetWindowText(temp);
	m_uiSeconds = (atoi((LPCSTR)temp));	
	
	if(m_uiSeconds < 0)
	{
		AfxMessageBox("Sync Interval is set to Zero!");
		return;
	}
	pConfig->ulHealthInterval	= m_uiSeconds;	
	
	SaveSettings();
	
	((CMy6046DryEndDlg*)AfxGetMainWnd())->RemoteAutoHealthCheck(m_bAutoHealth, m_uiSeconds);
	

	CDialog::OnOK();
}

void CDlgReportingOptions::OnCheckQuerycnct() 
{
	m_bQueryConnect	= !m_bQueryConnect;
	m_bUserChange = TRUE;
	pConfig->bQueryOnConnect	= m_bQueryConnect;
	SaveSettings();
}

void CDlgReportingOptions::OnCheckAutohealth() 
{
	m_bAutoHealth = !m_bAutoHealth;
	m_bUserChange = TRUE;
	pConfig->bAutoHealth = m_bAutoHealth;
	SaveSettings();
}

void CDlgReportingOptions::LoadSettings()
{
	if(pConfig->Open(CSystemConfig::accessRead)){
		pConfig->Read();
		pConfig->Close();
	}
	
	//now save all
	m_bQueryConnect = pConfig->bQueryOnConnect;
	m_bAutoHealth	= pConfig->bAutoHealth;
	m_uiSeconds	= pConfig->ulHealthInterval;
	
	m_ctrlQueryHealth.SetCheck(m_bQueryConnect);
	m_ctrlAutoHealth.SetCheck(m_bAutoHealth);

	CString temp;
	temp.Format("%d", m_uiSeconds);
	m_ctrlEditInterval.SetWindowText(temp);

}

void CDlgReportingOptions::SaveSettings()
{
	if(pConfig->Open(CSystemConfig::accessWrite)){
			pConfig->Save();
			pConfig->Close();
	}
}

BOOL CDlgReportingOptions::OnInitDialog() 
{
	CDialog::OnInitDialog();

	HICON icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	SetIcon(icon, FALSE);

	pConfig = (CSystemConfig*)&((CMy6046DryEndApp*)AfxGetApp())->m_SystemConfig;

	m_ctrlEditInterval.SetLimitText(3);

	m_ctrlOK.SetShade			  (SHS_HARDBUMP,8,40,2,RGB(55,55,255));
	m_ctrlCancel.SetShade		  (SHS_HARDBUMP,8,40,2,RGB(55,55,255));
	
	LoadSettings();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CDlgReportingOptions::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		return TRUE ;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}
