// DlgNavText.cpp : implementation file
//

#include "stdafx.h"
#include "6046dryend.h"
#include "6046DryEndDlg.h"

#include "DlgNavText.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgNavText dialog


CDlgNavText::CDlgNavText(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgNavText::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgNavText)
	//}}AFX_DATA_INIT

	radVal = 180/PI;
}


void CDlgNavText::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgNavText)
	DDX_Control(pDX, IDC_COMBO_LABELS, m_ctrlComboLabels);
	DDX_Control(pDX, IDC_BTN_CLEAR, m_ctrlBtnClear);
	DDX_Control(pDX, IDC_NAV_LIST, m_ctrlNavList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgNavText, CDialog)
	//{{AFX_MSG_MAP(CDlgNavText)
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
	ON_CBN_SELCHANGE(IDC_COMBO_LABELS, OnSelchangeComboLabels)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgNavText message handlers

void CDlgNavText::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//MAKE WINDOWS THINK WE CLICKED ON THE CAPTION BAR,
	//SO THE DIALOG CAN BE MOVED BY CLICKING ANYWHERE ON IT.
    PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CDlgNavText::OnBtnClear() 
{
	//Clear all
	m_ctrlNavList.DeleteAllItems();
}

BOOL CDlgNavText::OnInitDialog() 
{
	CDialog::OnInitDialog();

	HICON icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	SetIcon(icon, FALSE);
	
	m_pConfig  = &((CMy6046DryEndApp*)AfxGetApp())->m_SystemConfig;

	LoadSettings();

	m_ctrlBtnClear.SetShade(SHS_METAL,8,20,2,RGB(0,55,0));

	m_ctrlNavList.SetBkColor(RGB(0,25,0));
	m_ctrlNavList.SetTextBkColor(RGB(0,25,0));
	m_ctrlNavList.SetTextColor(RGB(0,245,55));
	m_ctrlNavList.InsertColumn(0,"Time",LVCFMT_CENTER,75,-1);
	m_ctrlNavList.InsertColumn(1,"Latitude",LVCFMT_CENTER, 125,-1);
	m_ctrlNavList.InsertColumn(2,"Longitude",LVCFMT_CENTER, 125,-1);
	 
	m_ctrlComboLabels.SetCurSel(m_pConfig->uiLabels);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgNavText::AddGroupData( NBlueFin7kMessages::PBLUEFINRECORDTYPEHEADER	    pRecHdr,
							    NBlueFin7kMessages::PBLUEFINNETWORKHEADER			pNavHdr,
  	 							NBlueFin7kMessages::PBLUEFINNAVIGATIONMESSAGE		pNavFrame )
{

	if(m_ctrlNavList.GetItemCount() >= 25)
		m_ctrlNavList.DeleteItem(m_ctrlNavList.GetItemCount()-1);

	PTIME7K time = (PTIME7K)pNavHdr->m_byTimeStamp;

	CString timestr,lat,longi;
	timestr.Format("%d %0ddays %02d:%02d:%02d.%d", time->m_unYear, time->m_unDay, time->m_ucHours, time->m_ucMinutes, (int)time->m_fSeconds, (int)((time->m_fSeconds-((int)time->m_fSeconds))*1000) ); 
	m_ctrlNavList.InsertItem(0,timestr,0);
	

	//Format out lat/long strings according to user defined units
	switch(m_ctrlComboLabels.GetCurSel())
	{
	case 0: //Degrees : Minutes : seconds
		{
			lat.Format("%s", RadToDegMinSec(pNavFrame->m_dLatitude, TRUE));
			longi.Format("%s", RadToDegMinSec(pNavFrame->m_dLongitude, FALSE));
			break;
		}
	case 1: //Degree.Minutes (decimal)
		{
			lat.Format("%s", RadToDegMin(pNavFrame->m_dLatitude, TRUE));
			longi.Format("%s", RadToDegMin(pNavFrame->m_dLongitude, FALSE));
			break;
		}
	case 2: //Radians, straight up
		{	
			lat.Format("%f", pNavFrame->m_dLatitude);
			longi.Format("%f", pNavFrame->m_dLongitude);
			break;
		}
	}
	
	m_ctrlNavList.SetItemText(0,1,lat);
	m_ctrlNavList.SetItemText(0,2,longi);

}


BOOL CDlgNavText::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		return TRUE ;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}


CString CDlgNavText::RadToDegMinSec( DOUBLE radians, BOOL lattitude )
{
	CString fin;

	char direction = 'N';

	if(lattitude)
	{
		if(radians < 0)
			direction = 'S';
	}
	else
	{
		if(radians > 0)
			direction = 'E';
		else
			direction = 'W';
	}
	
	double degrees	= radians*radVal;
	
	//make positive double
	if( degrees < 0 )
		degrees *= -1;

	int	   deg		= (int)degrees;

	float  minutes	= (degrees-deg)*60;
	int    min		= (int)(minutes);

	int    sec		= (int)floor(((minutes-min)*60));

	fin.Format("%d : %02d : %02d   %c", deg,min,sec,direction);
	return fin;

}

CString CDlgNavText::RadToDegMin( DOUBLE radians, BOOL lattitude )
{
	CString fin;

	char direction = 'N';

	if(lattitude)
	{
		if(radians < 0)
			direction = 'S';
	}
	else
	{
		if(radians > 0)
			direction = 'E';
		else
			direction = 'W';
	}

	double degrees	= radians*radVal;

	int	   deg		= (int)degrees;
	float  minutes	= (degrees-deg)*60;
	
	//make positive double
	if(degrees<0)
		degrees *= -1;

	fin.Format("%f %f  %c", degrees,minutes,direction);
	return fin;
}

void CDlgNavText::OnSelchangeComboLabels() 
{
	m_pConfig->uiLabels = m_ctrlComboLabels.GetCurSel();
}

LRESULT CDlgNavText::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if(message == WM_EXITSIZEMOVE)
	{
		CRect rect;
		GetWindowRect(&rect);
		m_pConfig->NAVWindowX	= rect.left;
		m_pConfig->NAVWindowY	= rect.top;

		SaveSettings();

	}
	
	return CDialog::WindowProc(message, wParam, lParam);
}

void CDlgNavText::LoadSettings()
{
	if(m_pConfig->Open(CSystemConfig::accessRead))
	{
		m_pConfig->Read();
		m_pConfig->Close();

		int x	= m_pConfig->NAVWindowX;
		int y	= m_pConfig->NAVWindowY;
		SetWindowPos(NULL,x,y,0,0,SWP_NOSIZE);
	}

}

void CDlgNavText::SaveSettings()
{
	if(m_pConfig->Open(CSystemConfig::accessWrite))
	{
		m_pConfig->Save();
		m_pConfig->Close();
	}
}

void CDlgNavText::OnTimer(UINT nIDEvent) 
{
	
	CDialog::OnTimer(nIDEvent);
}

void CDlgNavText::OnClose() 
{
	((CMy6046DryEndDlg*)AfxGetMainWnd())->ForceClose(DSPNAV);

	CDialog::OnClose();
}
