// DlgSBPParams.cpp : implementation file
//

#include "stdafx.h"
#include "6046dryend.h"
#include "DlgSBPParams.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int	  CDlgSBPParams::m_aiGain[GAIN_CELLS] = {1,2,4,8,16,32,64,128,256,512,1024,2048};

/////////////////////////////////////////////////////////////////////////////
// CDlgSBPParams dialog


CDlgSBPParams::CDlgSBPParams(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSBPParams::IDD, pParent)
{

	m_dSensorIndex = 0;	//default

	m_bPingEnabled = TRUE;

	//{{AFX_DATA_INIT(CDlgSBPParams)
	m_strGain = _T("");
	m_strPower = _T("");
	m_fDuration = 1.0f;
	//}}AFX_DATA_INIT
}


void CDlgSBPParams::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSBPParams)
	DDX_Control(pDX, IDC_COMBO_PULSEFILES, m_ctrlComboPulse);
	DDX_Control(pDX, IDC_DURATION, m_ctrlDuration);
	DDX_Control(pDX, IDC_ACCEPTDURATION, m_ctrlAcceptDuration);
	DDX_Control(pDX, IDC_UPDATE_SBP, m_ctrlUpdate);
	DDX_Control(pDX, IDC_POWERUP_SBP, m_ctrlPowerUp);
	DDX_Control(pDX, IDC_POWERDOWN_SBP, m_ctrlPowerDown);
	DDX_Control(pDX, IDC_PE_SBP, m_ctrlPESBP);
	DDX_Control(pDX, IDC_GAINUP_SBP, m_ctrlGainUp);
	DDX_Control(pDX, IDC_GAINDOWN_SBP, m_ctrlGainDown);
	DDX_Control(pDX, IDC_POWER_SBP, m_ctrlPower);
	DDX_Control(pDX, IDC_GAIN_SBP, m_ctrlGain);
	DDX_Text(pDX, IDC_GAIN_SBP, m_strGain);
	DDX_Text(pDX, IDC_POWER_SBP, m_strPower);
	DDX_Text(pDX, IDC_DURATION, m_fDuration);
	DDV_MinMaxFloat(pDX, m_fDuration, 0.f, 1.e+015f);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSBPParams, CDialog)
	//{{AFX_MSG_MAP(CDlgSBPParams)
	ON_BN_CLICKED(IDC_GAINDOWN_SBP, OnGaindownSbp)
	ON_BN_CLICKED(IDC_GAINUP_SBP, OnGainupSbp)
	ON_BN_CLICKED(IDC_PE_SBP, OnPeSbp)
	ON_BN_CLICKED(IDC_POWERDOWN_SBP, OnPowerdownSbp)
	ON_BN_CLICKED(IDC_POWERUP_SBP, OnPowerupSbp)
	ON_BN_CLICKED(IDC_UPDATE_SBP, OnUpdateSbp)
	ON_BN_DOUBLECLICKED(IDC_GAINDOWN_SBP, OnDoubleclickedGaindownSbp)
	ON_BN_DOUBLECLICKED(IDC_GAINUP_SBP, OnDoubleclickedGainupSbp)
	ON_BN_DOUBLECLICKED(IDC_POWERDOWN_SBP, OnDoubleclickedPowerdownSbp)
	ON_BN_DOUBLECLICKED(IDC_POWERUP_SBP, OnDoubleclickedPowerupSbp)
	ON_WM_DESTROY()
	ON_WM_CANCELMODE()
	ON_BN_CLICKED(IDC_ACCEPTDURATION, OnAcceptduration)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSBPParams message handlers


BOOL CDlgSBPParams::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_pMainWnd = (CMy6046DryEndDlg*)AfxGetMainWnd();
	
	m_pConfig  = &((CMy6046DryEndApp*)AfxGetApp())->m_SystemConfig;

	LoadSettings();

	//fill in settings arrays
	for(int i=0; i<101; i++)
	{
		m_aiPower[i] = i;
	}

	m_strPower.Format("%d%%", m_aiPower[m_dPower]);
	m_strGain.Format("%d", m_aiGain[m_dGain]);

	CString duration;
	m_ctrlDuration.SetLimitText(6);
	duration.Format("%f", m_fDuration);
	m_ctrlDuration.SetWindowText(duration);

	//Duration
	m_ctrlAcceptDuration.SetShade(SHS_METAL,8,20,4,RGB(180,180,0));
	
	//Gain m_ctrlGain
	m_ctrlGain.SetBkColor(RGB(0,35,0));
	m_ctrlGain.SetTextColor(RGB(0,255,0));
	m_ctrlGainUp.SetShade(SHS_METAL,8,40,18,RGB(0,55,0));
	m_ctrlGainUp.SetIcon(IDI_ICON_ARROWUP, BS_CENTER, 0);
	m_ctrlGainDown.SetShade(SHS_METAL,8,40,18,RGB(0,55,0));
	m_ctrlGainDown.SetIcon(IDI_ICON_ARROWDOWN, BS_CENTER, 0);
	//Power 
	m_ctrlPower.SetBkColor(RGB(0,35,0));
	m_ctrlPower.SetTextColor(RGB(0,255,0));
	m_ctrlPowerUp.SetShade(SHS_METAL,8,40,18,RGB(0,55,0));
	m_ctrlPowerUp.SetIcon(IDI_ICON_ARROWUP, BS_CENTER, 0);
	m_ctrlPowerDown.SetShade(SHS_METAL,8,40,18,RGB(0,55,0));
	m_ctrlPowerDown.SetIcon(IDI_ICON_ARROWDOWN, BS_CENTER, 0);
	
	m_ctrlPESBP.SetShade(SHS_METAL,8,40,18,RGB(0,155,0));
	m_ctrlUpdate.SetShade(SHS_METAL,8,40,6,RGB(55,0,0));

	m_ctrlPESBP.SetCheck(m_bPingEnabled);
	m_ctrlPESBP.SetWindowText("ENABLED");

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgSBPParams::SetSensorIndex(UINT index)
{
//	TRACE("SETTING SBP INDEX, ID: %d\r\n", index);
	m_dSensorIndex = index;
	m_pMainWnd->RemoteGetPulseFiles(index);
}


DWORD CDlgSBPParams::SetCurrentRange( float range )
{
	for(int i = 0; i<GAIN_CELLS; i++)
	{
		if(m_aiGain[i] == range)
		{
			m_dGain = i;
			m_strGain.Format("%d", m_aiGain[m_dGain]);
			UpdateData(FALSE);
			return 1;
		}
	}

	return 0;
}

DWORD CDlgSBPParams::SetCurrentPower( UINT  power )
{

	return 1;
}


void CDlgSBPParams::OnOK() 
{
	// TODO: Add extra validation here
	return;
	//CDialog::OnOK();
}

void CDlgSBPParams::OnGaindownSbp() 
{
	if(m_dGain == 0)
		return;

	m_dGain	-= 1;

	m_strGain.Format("%d", m_aiGain[m_dGain]);
	UpdateData(FALSE);

}

void CDlgSBPParams::OnGainupSbp() 
{
	if(m_dGain == GAIN_CELLS-1)
		return;

	m_dGain	+= 1;

	m_strGain.Format("%d", m_aiGain[m_dGain]);
	UpdateData(FALSE);

}

void CDlgSBPParams::OnPeSbp() 
{
	m_bPingEnabled = !m_bPingEnabled;
	
	if(m_bPingEnabled)
	{
		m_ctrlPESBP.SetWindowText("ENABLED");
		m_ctrlPESBP.SetShade(SHS_METAL,8,40,18,RGB(0,155,0));
	}
	else
	{
		m_ctrlPESBP.SetWindowText("DISABLED");
		m_ctrlPESBP.SetShade(SHS_METAL,8,40,18,RGB(77,0,0));
	}
	
}

void CDlgSBPParams::OnPowerdownSbp() 
{
	if(m_dPower == 0)
		return;

	short state = GetKeyState(VK_CONTROL);

	if((state < 0) && (m_dPower-10 >= 0))
	{
		m_dPower	-= 10;

		m_strPower.Format("%d%%", m_aiPower[m_dPower]);
		UpdateData(FALSE);
		return;
	}

	m_dPower	-= 1;

	m_strPower.Format("%d%%", m_aiPower[m_dPower]);
	UpdateData(FALSE);

}

void CDlgSBPParams::OnPowerupSbp() 
{
	if(m_dPower == POWER_CELLS-1)
		return;

	short state = GetKeyState(VK_CONTROL);

	if((state < 0) && (m_dPower+10 <= POWER_CELLS))
	{
		m_dPower	+= 10;

		m_strPower.Format("%d%%", m_aiPower[m_dPower]);
		UpdateData(FALSE);
		return;
	}

	m_dPower	+= 1;

	m_strPower.Format("%d%%", m_aiPower[m_dPower]);
	UpdateData(FALSE);

}

void CDlgSBPParams::OnUpdateSbp() 
{
	SaveSettings();

	//removed Deltas check
	m_pMainWnd->RemotePingEnable(m_dSensorIndex, m_bPingEnabled);
	m_pMainWnd->RemoteSetGain(m_dSensorIndex, m_aiGain[m_dGain]);
	m_pMainWnd->RemoteSetPower(m_dSensorIndex, m_aiPower[m_dPower]);
	CString pulse;
	int sel = m_ctrlComboPulse.GetCurSel();
	if(sel > -1)
	{
		m_ctrlComboPulse.GetLBText(sel, pulse);
		m_pMainWnd->RemoteSetPulse(m_dSensorIndex, pulse);
	}

	SETTINGS settings;
	ZeroMemory((void*)&settings, sizeof(SETTINGS));
	settings.duration = m_fDuration;
	settings.gain	  = m_aiGain[m_dGain];
	settings.power    = m_aiPower[m_dPower];

	m_pMainWnd->m_DspManager.SetWaterfallParams(WFSBP, &settings);

}

void CDlgSBPParams::OnAcceptduration() 
{

	CString str;
	m_ctrlDuration.GetWindowText(str);
	
	m_fDuration = atof(str);

	if(m_fDuration == 0.0)
	{
		m_fDuration = 1.0;
		m_ctrlDuration.SetWindowText("1");
		UpdateData(FALSE);
	}
	SETTINGS settings;
	ZeroMemory((void*)&settings, sizeof(SETTINGS));
	settings.duration = m_fDuration;
	settings.gain	  = m_aiGain[m_dGain];
	settings.power    = m_aiPower[m_dPower];

	m_pMainWnd->m_DspManager.SetWaterfallParams(WFSBP, &settings);

	m_pMainWnd->RemoteSetDuration(m_dSensorIndex, m_fDuration);
	
	SaveSettings();
}

void CDlgSBPParams::LoadSettings()
{
	if(m_pConfig->Open(CSystemConfig::accessRead))
	{	
		m_pConfig->Read();
		m_pConfig->Close();
		m_dGain		= m_pConfig->dSBPGain;
		m_dPower	= m_pConfig->dSBPPower;
		m_fDuration = m_pConfig->fSBPDuration;
		
	}
	
}

void CDlgSBPParams::SaveSettings()
{
	m_pConfig->fSBPDuration = m_fDuration;
	m_pConfig->dSBPGain		= m_dGain;
	m_pConfig->dSBPPower	= m_dPower;
	
	if(m_pConfig->Open(CSystemConfig::accessWrite))
	{	
		m_pConfig->Save();
		m_pConfig->Close();
	}
}

void CDlgSBPParams::OnDestroy() 
{
		
	SaveSettings();

	m_ctrlUpdate.~CxShadeButton();
	m_ctrlPowerUp.~CxShadeButton();
	m_ctrlPowerDown.~CxShadeButton();
	m_ctrlPESBP.~CxShadeButton();
	m_ctrlGainUp.~CxShadeButton();
	m_ctrlGainDown.~CxShadeButton();
	m_ctrlAcceptDuration.~CxShadeButton();

	CDialog::OnDestroy();
}

void CDlgSBPParams::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}
void CDlgSBPParams::OnDoubleclickedDurationdownSbp() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSBPParams::OnDoubleclickedDurationupSbp() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSBPParams::OnDoubleclickedGaindownSbp() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSBPParams::OnDoubleclickedGainupSbp() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSBPParams::OnDoubleclickedPowerdownSbp() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSBPParams::OnDoubleclickedPowerupSbp() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSBPParams::AddPulseFile(CString fileName)
{
	m_ctrlComboPulse.AddString(fileName);
}

BOOL CDlgSBPParams::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);
}
