// Dlg7kSonarParamsMore.cpp : implementation file
//

#include "stdafx.h"
#include "6046dryend.h"
#include "Dlg7kSonarParamsMore.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlg7kSonarParamsMore dialog


CDlg7kSonarParamsMore::CDlg7kSonarParamsMore(CWnd* pParent /*=NULL*/)
	: CDialog(CDlg7kSonarParamsMore::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlg7kSonarParamsMore)
	//}}AFX_DATA_INIT

	m_pConfig = NULL;
	m_pMainWnd = NULL;

	m_fMinRange = m_fMaxRange = m_fMinDepth = m_fMaxDepth = 0.0f;
}


void CDlg7kSonarParamsMore::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlg7kSonarParamsMore)
	DDX_Control(pDX, IDC_CHECK_FLIP, m_ctrlCheckFlip);
	DDX_Control(pDX, IDC_CHECK_FILTERS, m_ctrlCheckFilters);
	DDX_Control(pDX, IDC_EDIT_RANGEMIN, m_ctrlRangeMin);
	DDX_Control(pDX, IDC_EDIT_RANGEMAX, m_ctrlRangeMax);
	DDX_Control(pDX, IDC_EDIT_DEPTHMIN, m_ctrlDepthMin);
	DDX_Control(pDX, IDC_EDIT_DEPTHMAX, m_ctrlDepthMax);
	DDX_Control(pDX, IDC_BUTTON_UPDATE, m_ctrlUpdate);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlg7kSonarParamsMore, CDialog)
	//{{AFX_MSG_MAP(CDlg7kSonarParamsMore)
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
	ON_EN_CHANGE(IDC_EDIT_DEPTHMAX, OnChangeEditDepthmax)
	ON_EN_CHANGE(IDC_EDIT_DEPTHMIN, OnChangeEditDepthmin)
	ON_EN_CHANGE(IDC_EDIT_RANGEMAX, OnChangeEditRangemax)
	ON_EN_CHANGE(IDC_EDIT_RANGEMIN, OnChangeEditRangemin)
	ON_BN_CLICKED(IDC_CHECK_FILTERS, OnCheckFilters)
	ON_BN_CLICKED(IDC_CHECK_FLIP, OnCheckFlip)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlg7kSonarParamsMore message handlers

BOOL CDlg7kSonarParamsMore::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_ctrlUpdate.SetShade(SHS_METAL,8,40,6,RGB(55,0,0));

	CString temp;

	//Set these up first!!!
	m_pConfig	= &((CMy6046DryEndApp*)AfxGetApp())->m_SystemConfig;
	m_pMainWnd  = (CMy6046DryEndDlg*)AfxGetMainWnd();


	LoadSettings();

	temp.Format("%.1f", m_fMinRange);
	m_ctrlRangeMin.SetWindowText(temp);
	
	temp.Format("%.1f", m_fMaxRange);
	m_ctrlRangeMax.SetWindowText(temp);
	
	temp.Format("%.1f", m_fMinDepth);
	m_ctrlDepthMin.SetWindowText(temp);
	
	temp.Format("%.1f", m_fMaxDepth);
	m_ctrlDepthMax.SetWindowText(temp);

	m_ctrlCheckFilters.EnableWindow(FALSE);
	m_ctrlRangeMin.EnableWindow(FALSE);
	m_ctrlRangeMax.EnableWindow(FALSE);
	m_ctrlDepthMin.EnableWindow(FALSE);
	m_ctrlDepthMax.EnableWindow(FALSE);
	m_ctrlUpdate.EnableWindow(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlg7kSonarParamsMore::InitControls()
{
	m_ctrlCheckFilters.EnableWindow(TRUE);
	m_ctrlRangeMin.EnableWindow(TRUE);
	m_ctrlRangeMax.EnableWindow(TRUE);
	m_ctrlDepthMin.EnableWindow(TRUE);
	m_ctrlDepthMax.EnableWindow(TRUE);
	m_ctrlUpdate.EnableWindow(TRUE);

}

void CDlg7kSonarParamsMore::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CDlg7kSonarParamsMore::LoadSettings()
{
	if(m_pConfig->Open(CSystemConfig::accessRead))
	{	
		m_pConfig->Read();
		m_pConfig->Close();
	}

	m_fMinRange = m_pConfig->fMinRange;
	m_fMaxRange	= m_pConfig->fMaxRange;
	m_fMinDepth = m_pConfig->fMinDepth;
	m_fMaxDepth = m_pConfig->fMaxDepth;

	m_ctrlCheckFlip.SetCheck( m_pConfig->bFlip );
		
}

void CDlg7kSonarParamsMore::SaveSettings()
{	
	if(m_pConfig->Open(CSystemConfig::accessWrite))
	{	
		m_pConfig->Save();
		m_pConfig->Close();
	}

}

void CDlg7kSonarParamsMore::OnButtonUpdate() 
{
	CString temp;
	double  rangeMax = 0, rangeMin = 0;
	
	C7kXMLParsor *m_xml = (C7kXMLParsor*)&((CMy6046DryEndDlg*)AfxGetMainWnd())->m_xmlParsor;
	m_xml->GetMaxRange(&rangeMax);
	//m_xml->GetMinRange(&rangeMin);

	//check if the values are ok
	if(m_fMaxRange < m_fMinRange)
	{
		AfxMessageBox("The Max Range is LOWER then the Min Range");	
		temp.Format("%.1f", rangeMax);
		m_ctrlRangeMax.SetWindowText(temp);
		return;
	}
	if(m_fMaxRange > rangeMax)
	{
		AfxMessageBox("The Max Range is higher than the Max range for this Sonar");
		temp.Format("%.1f", rangeMax);
		m_ctrlRangeMax.SetWindowText(temp);
		return;
	}
	if(m_fMinRange > rangeMax)
	{
		AfxMessageBox("The Min Range is higher than the max range for this Sonar");
		temp.Format("%.1f", rangeMax);
		m_ctrlRangeMin.SetWindowText(temp);
		return;
	}
	if(m_fMinRange < rangeMin)
	{
		AfxMessageBox("The Min Range is LOWER than the Min range for this Sonar");
		temp.Format("%.1f", rangeMin);
		m_ctrlRangeMin.SetWindowText(temp);
		return;
	}
	if(m_fMaxRange < rangeMin)
	{
		AfxMessageBox("The Max Range is LOWER than the Min range for this Sonar");
		temp.Format("%.1f", rangeMin);
		m_ctrlRangeMax.SetWindowText(temp);
		return;
	}
	
	if(m_fMaxDepth < m_fMinDepth)
	{
		AfxMessageBox("Max Depth is LOWER than the Min depth");
		temp.Format("%.1f", rangeMax);
		m_ctrlDepthMax.SetWindowText(temp);
		return;
	}
	if(m_fMaxDepth > rangeMax)
	{
		AfxMessageBox("Max Depth cannot be greater than Sonar Max Range");
		temp.Format("%.1f", rangeMax);
		m_ctrlDepthMax.SetWindowText(temp);
		return;
	}
	if(m_fMinDepth > rangeMax)
	{
		AfxMessageBox("Min Depth cannot be greater than Sonar Max Range");
		temp.Format("%.1f", rangeMax);
		m_ctrlDepthMin.SetWindowText(temp);
		return;
	}
	if(m_fMinDepth < rangeMin)
	{
		AfxMessageBox("The Min Depth is LOWER than the Min range for this Sonar");
		temp.Format("%.1f", rangeMin);
		m_ctrlDepthMin.SetWindowText(temp);
		return;
	}
	if(m_fMaxDepth < rangeMin)
	{
		AfxMessageBox("The Max Range is LOWER than the Min range for this Sonar");
		temp.Format("%.1f", rangeMin);
		m_ctrlDepthMax.SetWindowText(temp);
		return;
	}

	//Now send all this filter info to Wayne somehow
	m_pMainWnd->RemoteRangeDepthFilters(m_uiSensorIndex,m_fMinRange,m_fMaxRange,
										m_fMinDepth,m_fMaxDepth);


	ULONG flags = 0;
	if(m_ctrlCheckFilters.GetCheck() > 0)
	{
		flags = 768;
	}
	m_pMainWnd->RemoteBottomDetectFlag(m_uiSensorIndex, flags);


	SaveSettings();

}

BOOL CDlg7kSonarParamsMore::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		return TRUE ;
	}
	if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_CANCEL)
	{
		return TRUE ;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

void CDlg7kSonarParamsMore::OnChangeEditRangemin() 
{
	CString temp;
	m_ctrlRangeMin.GetWindowText(temp);
	m_fMinRange = atof(temp);
	m_pConfig->fMinRange = m_fMinRange;
}

void CDlg7kSonarParamsMore::OnChangeEditRangemax() 
{
	CString temp;
	m_ctrlRangeMax.GetWindowText(temp);
	m_fMaxRange = atof(temp);
	m_pConfig->fMaxRange = m_fMaxRange;
}

void CDlg7kSonarParamsMore::OnChangeEditDepthmin() 
{
	CString temp;
	m_ctrlDepthMin.GetWindowText(temp);
	m_fMinDepth = atof(temp);
	m_pConfig->fMinDepth = m_fMinDepth;
}

void CDlg7kSonarParamsMore::OnChangeEditDepthmax() 
{
	CString temp;
	m_ctrlDepthMax.GetWindowText(temp);
	m_fMaxDepth = atof(temp);
	m_pConfig->fMaxDepth = m_fMaxDepth;
}

void CDlg7kSonarParamsMore::SetSensorIndex(int	index )
{
	m_uiSensorIndex = index;

}

void CDlg7kSonarParamsMore::UpdateFilters(float minRange,float maxRange,float minDepth,float maxDepth, BOOL state)
{
	CString text;

	text.Format("%.1f", minRange);
	m_ctrlRangeMin.SetWindowText(text);

	text.Format("%.1f", maxRange);
	m_ctrlRangeMax.SetWindowText(text);

	text.Format("%.1f", minDepth);
	m_ctrlDepthMin.SetWindowText(text);

	text.Format("%.1f", maxDepth);
	m_ctrlDepthMax.SetWindowText(text);

	if(state)
	{
		m_ctrlCheckFilters.SetCheck(1);
		m_ctrlRangeMin.EnableWindow(TRUE);
		m_ctrlRangeMax.EnableWindow(TRUE);
		m_ctrlDepthMin.EnableWindow(TRUE);
		m_ctrlDepthMax.EnableWindow(TRUE);
	}
	else
	{
		m_ctrlCheckFilters.SetCheck(0);
		m_ctrlRangeMin.EnableWindow(FALSE);
		m_ctrlRangeMax.EnableWindow(FALSE);
		m_ctrlDepthMin.EnableWindow(FALSE);
		m_ctrlDepthMax.EnableWindow(FALSE);
	}

}

void CDlg7kSonarParamsMore::OnCheckFilters() 
{
	if(m_ctrlCheckFilters.GetCheck())
	{
		m_ctrlRangeMin.EnableWindow(TRUE);
		m_ctrlRangeMax.EnableWindow(TRUE);
		m_ctrlDepthMin.EnableWindow(TRUE);
		m_ctrlDepthMax.EnableWindow(TRUE);
	}
	else
	{
		m_ctrlRangeMin.EnableWindow(FALSE);
		m_ctrlRangeMax.EnableWindow(FALSE);
		m_ctrlDepthMin.EnableWindow(FALSE);
		m_ctrlDepthMax.EnableWindow(FALSE);
	}
	
}

void CDlg7kSonarParamsMore::OnCheckFlip() 
{
	//notify main dialog window
	m_pMainWnd->FlipOrientation(m_ctrlCheckFlip.GetCheck());

	//save the setting
	m_pConfig->bFlip = m_ctrlCheckFlip.GetCheck();
	
}
