// DlgSensorSetup.cpp : implementation file
//

#include "stdafx.h"
#include "DlgSensorSetup.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgSensorSetup dialog


CDlgSensorSetup::CDlgSensorSetup(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSensorSetup::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSensorSetup)
	m_bScaleHFSS = FALSE;
	m_bScaleLFSS = FALSE;
	m_bScaleSBP = FALSE;
	//}}AFX_DATA_INIT

	m_dSelectedSensor  = 0;
	bLoadHFSS = bLoadLFSS = bLoadSBP = bLoadBlueFin = bLoadSeaBat = FALSE;
	m_bPlaybackMode = FALSE;
}


void CDlgSensorSetup::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSensorSetup)
	DDX_Control(pDX, IDC_RESET_SBP, m_ctrlResetSBP);
	DDX_Control(pDX, IDC_RESET_LFSS, m_ctrlResetLFSS);
	DDX_Control(pDX, IDC_RESET_HFSS, m_ctrlResetHFSS);
	DDX_Control(pDX, IDC_LIST_SENSORS_LOADED, m_ctrlListSensors);
	DDX_Control(pDX, IDC_BTN_ADD, m_ctrlAdd);
	DDX_Control(pDX, IDC_BTN_REMOVE, m_ctrlRemove);
	DDX_Control(pDX, IDC_COMBO_SENSORS, m_ctrlComboSensors);
	DDX_Check(pDX, IDC_CHECK_HFSS, m_bScaleHFSS);
	DDX_Check(pDX, IDC_CHECK_LFSS, m_bScaleLFSS);
	DDX_Check(pDX, IDC_CHECK_SBP, m_bScaleSBP);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSensorSetup, CDialog)
	//{{AFX_MSG_MAP(CDlgSensorSetup)
	ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
	ON_BN_CLICKED(IDC_BTN_REMOVE, OnBtnRemove)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_SENSORS_LOADED, OnDblclkListSensorsLoaded)
	ON_NOTIFY(NM_CLICK, IDC_LIST_SENSORS_LOADED, OnClickListSensorsLoaded)
	ON_BN_CLICKED(IDC_RESET_SBP, OnResetSbp)
	ON_BN_CLICKED(IDC_RESET_LFSS, OnResetLfss)
	ON_BN_CLICKED(IDC_RESET_HFSS, OnResetHfss)
	ON_BN_CLICKED(IDC_CHECK_HFSS, OnCheckHfss)
	ON_BN_CLICKED(IDC_CHECK_LFSS, OnCheckLfss)
	ON_BN_CLICKED(IDC_CHECK_SBP, OnCheckSbp)
	ON_WM_DESTROY()
	ON_WM_CANCELMODE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSensorSetup message handlers


BOOL CDlgSensorSetup::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_dSelectedSensor = -1;

	for(int i = 0; i<MAX_SENSORS; i++)
	{
		sensorID[i] = -1;
	}

	m_ctrlAdd.SetShade(SHS_METAL,8,20,2,RGB(55,255,55));
	m_ctrlRemove.SetShade(SHS_METAL,8,20,2,RGB(255,55,55));
	m_ctrlResetHFSS.SetShade(SHS_METAL,8,20,2,RGB(0,55,0));
	m_ctrlResetLFSS.SetShade(SHS_METAL,8,20,2,RGB(0,55,0));
	m_ctrlResetSBP.SetShade(SHS_METAL,8,20,2,RGB(0,55,0));
	
	m_ctrlListSensors.SetBkColor(RGB(0,35,0));
	m_ctrlListSensors.SetTextColor(RGB(0,255,55));
	m_ctrlListSensors.SetTextBkColor(RGB(0,35,0));

	m_ctrlListSensors.InsertColumn(0,"Currently Displayed Data:",LVCFMT_CENTER,436);

	m_ctrlComboSensors.InsertString(0,"NO SYNC");
	m_ctrlComboSensors.SetCurSel(0);

	m_pMainWnd = (CMy6046DryEndDlg*)AfxGetMainWnd();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSensorSetup::OnBtnAdd() 
{
	if(m_pMainWnd->m_bRemoteConnected == FALSE)
		return;

	if(m_bPlaybackMode == TRUE)
		return;

	if(m_ctrlComboSensors.GetCount() == 0)
		return;

	//Get Sensor index #
	int selected = m_ctrlComboSensors.GetCurSel();
	int sensor = -1;

	CString itemtext;

	//Figure out which sensor was selected using the local class enums
	m_ctrlComboSensors.GetLBText(selected, itemtext);
	
	if(itemtext.Find("SeaBat",0) > -1)
		sensor = E7K;
	if(itemtext.Find("SSSHF",0) > -1)
		sensor = EHFSS;
	if(itemtext.Find("SSSLF",0) > -1)
		sensor = ELFSS;
	if(itemtext.Find("SBP",0) > -1)
		sensor = ESBP;
	if(itemtext.Find("All",0) > -1)
		sensor = EALL;
	if(itemtext.Find("BlueFin",0) > -1)
		sensor = EBLUEFIN;

	if(sensor == -1)
		return; //we should never get here!

	//Enable and update edits, 
	int listsize = m_ctrlListSensors.GetItemCount();
	CString str;

	switch(sensor)
	{
		case EALL:
		{
			//Add all of them
			bLoadHFSS = bLoadLFSS = bLoadSBP = bLoadBlueFin = bLoadSeaBat = TRUE;
			
			for(int i = 0;i < listsize;i++)
			{
				str = m_ctrlListSensors.GetItemText(i,0);
				if(str.Find("High") >= 0)
					bLoadHFSS = FALSE;
				else if(str.Find("Low") >= 0)
					bLoadLFSS = FALSE;
				else if(str.Find("Sub") >= 0)
					bLoadSBP  = FALSE;
				else if(str.Find("Blue") >= 0)
					bLoadBlueFin = FALSE;
				else if(str.Find("SeaBat") >= 0)
					bLoadSeaBat = FALSE;
			}
			
			ExitPlaybackMode();
			break;
		}
		case EHFSS:
		{
			for(int i = 0;i < listsize;i++)
			{
				str = m_ctrlListSensors.GetItemText(i,0);
				if(str.Find("High") >= 0){
					return;
				}
			}
			if(m_pMainWnd->RemoteSubscribe(sensorID[HFSS],recordTypeEdgeTechFSDWSSS))
			{
				m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "EdgeTech High Frequency Side Scan Data ");
				bLoadHFSS = TRUE;
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		case ELFSS:
		{
			
			for(int i = 0;i < listsize;i++)
			{
				str = m_ctrlListSensors.GetItemText(i,0);
				if(str.Find("Low") >= 0){
					return;
				}
			}
			if(m_pMainWnd->RemoteSubscribe(sensorID[LFSS],recordTypeEdgeTechFSDWSSS))
			{
				m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "EdgeTech Low  Frequency Side Scan Data ");
				bLoadLFSS = TRUE;
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		case ESBP:
		{
			
			for(int i = 0;i < listsize;i++)
			{
				str = m_ctrlListSensors.GetItemText(i,0);
				if(str.Find("Sub") >= 0){
					return;
				}
			}
			if(m_pMainWnd->RemoteSubscribe(sensorID[SBPSS],recordTypeEdgeTechFSDWSBP)) 
			{
				m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "EdgeTech Sub  Bottom Profiler Data ");
				bLoadSBP = TRUE;
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		case EBLUEFIN:
		{
			for(int i = 0;i < listsize;i++)
			{
				str = m_ctrlListSensors.GetItemText(i,0);
				if(str.Find("Blue") >= 0){
					return;
				}
			}
			if(m_pMainWnd->RemoteSubscribe(sensorID[BLUEFIN],recordTypeBlueFinVC)) 
			{
				m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "Blue Fin Navigation Data ");
				bLoadBlueFin = TRUE;
			}
			else
				AfxMessageBox("Error requesting data");

			break;

		}
		case E7K:
		{
			for(int i = 0;i < listsize;i++)
			{
				str = m_ctrlListSensors.GetItemText(i,0);
				if(str.Find("SeaBat") >= 0){
					return;
				}
			}
			if(m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7000)) //one time request //hardcoded for now
			{	
				m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7004);
				m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7006);
				m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7011);

				m_pMainWnd->RemoteGetParameters(sensorID[SYS7K]);
				
				m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "SeaBat Data");
				bLoadSeaBat = TRUE;
			}
			else
				AfxMessageBox("Error requesting data");
			break;
		}
		default:{break;}
	}
}

void CDlgSensorSetup::OnBtnRemove() 
{
	if(m_pMainWnd->m_bRemoteConnected == FALSE)
		return;

	if(m_bPlaybackMode == TRUE)
		return;

	int selected = m_ctrlComboSensors.GetCurSel();	
	int listsize = m_ctrlListSensors.GetItemCount();
	CString str;

	int sensor = -1;

	CString itemtext;

	//Figure out which sensor was selected using the local class enums
	m_ctrlComboSensors.GetLBText(selected, itemtext);
	
	if(itemtext.Find("SeaBat",0) > -1)
		sensor = E7K;
	if(itemtext.Find("SSSHF",0) > -1)
		sensor = EHFSS;
	if(itemtext.Find("SSSLF",0) > -1)
		sensor = ELFSS;
	if(itemtext.Find("SBP",0) > -1)
		sensor = ESBP;
	if(itemtext.Find("All",0) > -1)
		sensor = EALL;
	if(itemtext.Find("BlueFin",0) > -1)
		sensor = EBLUEFIN;

	if(sensor == -1)
		return; //we should never get here!

	switch(selected)
	{
		case EALL:
		{
			//Remove All
			EnterPlaybackMode();
			m_bPlaybackMode = FALSE; //Not really in Playback mode, so override
			break;
		}
		case EHFSS:
		{
			if( m_pMainWnd->RemoteUnsubscribe(sensorID[HFSS],recordTypeEdgeTechFSDWSSS) == TRUE) 
			{
				for(int i = 0;i < listsize;i++)
				{
					str = m_ctrlListSensors.GetItemText(i,0);
					if(str.Find("High") >= 0){
						m_ctrlListSensors.DeleteItem(i);
						bLoadHFSS = FALSE;
						break;
					}
				}
				
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		case ELFSS:
		{
			if(m_pMainWnd->RemoteUnsubscribe(sensorID[LFSS],recordTypeEdgeTechFSDWSSS) == TRUE) 
			{
				for(int i =0;i<listsize;i++)
				{
					str = m_ctrlListSensors.GetItemText(i,0);
					if(str.Find("Low")>= 0){
						m_ctrlListSensors.DeleteItem(i);
						bLoadLFSS = FALSE;
						break;
					}
				}
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		case ESBP:
		{
			if(m_pMainWnd->RemoteUnsubscribe(sensorID[SBPSS],recordTypeEdgeTechFSDWSBP) == TRUE) 
			{
				for(int i =0; i< listsize;i++)
				{
					str = m_ctrlListSensors.GetItemText(i,0);
					if(str.Find("Sub") >= 0){
						m_ctrlListSensors.DeleteItem(i);
						bLoadSBP = FALSE;
						break;
					}
				}
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		case EBLUEFIN:
		{
			if(m_pMainWnd->RemoteUnsubscribe(sensorID[BLUEFIN],recordTypeBlueFinVC) == TRUE) 
			{
				for(int i =0; i< listsize;i++)
				{
					str = m_ctrlListSensors.GetItemText(i,0);
					if(str.Find("Blue") >= 0){
						m_ctrlListSensors.DeleteItem(i);
						bLoadBlueFin = FALSE;
						break;
					}
				}
			}
			else
				AfxMessageBox("Error requesting data");
		
			break;
		}
		case E7K:
		{
			if(m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7006) == TRUE) 
			{
				m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7000);
				m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7001);
				m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7005);
				m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7503);
				m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7004);
				m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7011);

				for(int i =0; i< listsize;i++)
				{
					str = m_ctrlListSensors.GetItemText(i,0);
					if(str.Find("SeaBat") >= 0){
						m_ctrlListSensors.DeleteItem(i);
						bLoadSeaBat = FALSE;
						break;
					}
				}
			}
			else
				AfxMessageBox("Error requesting data");

			break;
		}
		default:{break;}
	}
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

void CDlgSensorSetup::OnDblclkListSensorsLoaded(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;
}

void CDlgSensorSetup::OnClickListSensorsLoaded(NMHDR* pNMHDR, LRESULT* pResult) 
{
	POSITION p = m_ctrlListSensors.GetFirstSelectedItemPosition();
	m_dSelectedSensor = m_ctrlListSensors.GetNextSelectedItem(p);


	*pResult = 0;
}

void CDlgSensorSetup::OnOK() 
{
	return;
	//CDialog::OnOK();
}

void CDlgSensorSetup::OnResetSbp() 
{
	m_pMainWnd->ResetScaling(SBPSS);
}

void CDlgSensorSetup::OnResetLfss() 
{
	m_pMainWnd->ResetScaling(LFSS);	
}

void CDlgSensorSetup::OnResetHfss() 
{
	m_pMainWnd->ResetScaling(HFSS);	
}

void CDlgSensorSetup::OnCheckHfss() 
{
//	TRACE("CHECK BOOL = %d\r\n", m_bScaleHFSS);
	m_bScaleHFSS = !m_bScaleHFSS;
	m_pMainWnd->EnableEdgetechScaling(HFSS, m_bScaleHFSS);	
}

void CDlgSensorSetup::OnCheckLfss() 
{
	m_bScaleLFSS = !m_bScaleLFSS;
	m_pMainWnd->EnableEdgetechScaling(LFSS, m_bScaleLFSS);	
	
}

void CDlgSensorSetup::OnCheckSbp() 
{
	m_bScaleSBP = !m_bScaleSBP;
	m_pMainWnd->EnableEdgetechScaling(SBPSS, m_bScaleSBP);
	
}

void CDlgSensorSetup::LoadSettings()
{

}

void CDlgSensorSetup::SaveSettings()
{

}

void CDlgSensorSetup::OnDestroy() 
{
	m_ctrlResetSBP.~CxShadeButton();
	m_ctrlResetLFSS.~CxShadeButton();;
	m_ctrlResetHFSS.~CxShadeButton();;
	m_ctrlAdd.~CxShadeButton();;
	m_ctrlRemove.~CxShadeButton();;
	
	CDialog::OnDestroy();	
}

void CDlgSensorSetup::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CDlgSensorSetup::EnterPlaybackMode()
{
	m_ctrlListSensors.DeleteAllItems();

	if(bLoadHFSS)
		m_pMainWnd->RemoteUnsubscribe(sensorID[HFSS],recordTypeEdgeTechFSDWSSS);
	if(bLoadLFSS)
		m_pMainWnd->RemoteUnsubscribe(sensorID[LFSS],recordTypeEdgeTechFSDWSSS);
	if(bLoadSBP)
		m_pMainWnd->RemoteUnsubscribe(sensorID[SBPSS],recordTypeEdgeTechFSDWSBP);
	if(bLoadBlueFin)
		m_pMainWnd->RemoteUnsubscribe(sensorID[BLUEFIN],recordTypeBlueFinVC);
	if(bLoadSeaBat)
	{
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7006);
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7000);
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7001);
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7005);
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7503);
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7004);
		m_pMainWnd->RemoteUnsubscribe(sensorID[SYS7K],7011);
	}
	m_bPlaybackMode = TRUE;
}

void CDlgSensorSetup::ExitPlaybackMode()
{
	m_bPlaybackMode = FALSE;
	
	CString str;
	int index = 0, listsize = 0;

	listsize = m_ctrlComboSensors.GetCount();

	if(bLoadHFSS)
	{
		for(int i = 0;i < listsize;i++)
		{
			m_ctrlComboSensors.GetLBText(i,str);
			if(str.Find("SSSHF") > -1)
			{			
				if(m_pMainWnd->RemoteSubscribe(sensorID[HFSS],recordTypeEdgeTechFSDWSSS))
				{
					m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "EdgeTech High Frequency Side Scan Data ");	
					break;
				}
				else
				{
					bLoadHFSS = FALSE;
					AfxMessageBox("Error requesting data");
				}
			}
		}	
	}
	if(bLoadLFSS)
	{
		for(int i = 0;i < listsize;i++)
		{
			m_ctrlComboSensors.GetLBText(i,str);
			if(str.Find("SSSLF") > -1)
			{
			
				if(m_pMainWnd->RemoteSubscribe(sensorID[LFSS],recordTypeEdgeTechFSDWSSS))
				{
					m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "EdgeTech Low  Frequency Side Scan Data ");
					break;
				}
				else
				{
					bLoadLFSS = FALSE;
					AfxMessageBox("Error requesting data");
				}
			}
		}	
	}
	if(bLoadSBP)
	{
		for(int i = 0;i < listsize;i++)
		{
			m_ctrlComboSensors.GetLBText(i,str);
			if(str.Find("SBP") > -1)
			{
				if(m_pMainWnd->RemoteSubscribe(sensorID[SBPSS],recordTypeEdgeTechFSDWSBP)) 
				{
					m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "EdgeTech Sub  Bottom Profiler Data ");	
					break;
				}
				else
				{
					bLoadSBP = FALSE;
					AfxMessageBox("Error requesting data");
				}
			}
		}	
	}
	if(bLoadBlueFin)
	{
		for(int i = 0;i < listsize;i++)
		{
			m_ctrlComboSensors.GetLBText(i,str);
			if(str.Find("Blue") > -1)
			{
			
				if(m_pMainWnd->RemoteSubscribe(sensorID[BLUEFIN],recordTypeBlueFinVC)) 
				{
					m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "Blue Fin Navigation Data ");
					break;
					
				}
				else
				{
					bLoadBlueFin = FALSE;
					AfxMessageBox("Error requesting data");
				}
			}
		}	
	}
	if(bLoadSeaBat)
	{
		for(int i = 0;i < listsize;i++)
		{
			m_ctrlComboSensors.GetLBText(i,str);
			if(str.Find("SeaBat") > -1){
			
				if(m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7000)) //one time request //hardcoded for now
				{	
					m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7004);
					m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7006);
					m_pMainWnd->RemoteSubscribe(sensorID[SYS7K],7011);
					m_pMainWnd->RemoteGetParameters(sensorID[SYS7K]);			
					m_ctrlListSensors.InsertItem(m_ctrlComboSensors.GetCount(), "SeaBat Data");
					break;
				}
				else
				{
					bLoadSeaBat = FALSE;
					AfxMessageBox("Error requesting data");
				}
			}
		}		
	}

	m_ctrlComboSensors.SetCurSel(0);
}

void CDlgSensorSetup::AddSensorIndex( UINT type, UINT index )
{
	sensorID[type] = index;

	TRACE("ADDED SENSOR %d, index = %d\r\n", type, index);

	int myindex = m_ctrlComboSensors.FindString(0,"NO SYNC");
	if(myindex != LB_ERR)
	{
		m_ctrlComboSensors.DeleteString(myindex);
	}

	//Add the data available data type 
	//to the combo box
	switch(type)
	{
	
	case SBPSS:		{m_ctrlComboSensors.AddString("Edgetech SBP ");break;}
	case LFSS:		{m_ctrlComboSensors.AddString("Edgetech SSSLF");break;}
	case HFSS:		{m_ctrlComboSensors.AddString("Edgetech SSSHF");break;}
	case SYS7K:		{m_ctrlComboSensors.AddString("SeaBat 7K");break;}
	case BLUEFIN:	{m_ctrlComboSensors.AddString("BlueFin Sensors");break;}
	}

	if(m_ctrlComboSensors.GetCount() > 1)
	{
		int index = m_ctrlComboSensors.FindString(0,"Select All");
		m_ctrlComboSensors.DeleteString(index);
		m_ctrlComboSensors.InsertString(0,"Select All");
	}
	
	m_ctrlComboSensors.SetCurSel(0);

	
}

BOOL CDlgSensorSetup::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);
}

LRESULT CDlgSensorSetup::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	switch(message) 
	{
	case TCP_DISCONNECT: {	
							m_ctrlComboSensors.ResetContent();
							m_ctrlComboSensors.InsertString(0,"NO SYNC");
							m_ctrlComboSensors.SetCurSel(0);
							break; 
						 }
	}
	
	return CDialog::WindowProc(message, wParam, lParam);
}
