// SetupDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UDPServ.h"
#include "SetupDlg.h"

#include "UserDefs.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetupDlg dialog


CSetupDlg::CSetupDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupDlg)
	m_nPortnum = 0;
	m_nIPaddrA = 0;
	m_nIPaddrB = 0;
	m_nIPaddrC = 0;
	m_nIPaddrD = 0;
	m_bRemoteIP = FALSE;
	m_bMapUpdate = FALSE;
	m_dLongCorrection = 0.0;
	m_dLatCorrection = 0.0;
	m_nServPortNum = 0;
	m_bTCPMapEnable = FALSE;
	m_nTCPPortNum = 0;
	m_LatMean = 0;
	m_LatLongDev = 0;
	m_LongMean = 0;
	m_bSaveAllData = FALSE;
	m_nGMTTime = 0;
	m_bSaveReport = FALSE;
	//}}AFX_DATA_INIT
}


void CSetupDlg::DoDataExchange(CDataExchange* pDX)
{ //DoDataExchange
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetupDlg)
	DDX_Text(pDX, IDC_PORTNUM, m_nPortnum);
	DDV_MinMaxUInt(pDX, m_nPortnum, 0, 10000);
	DDX_Text(pDX, IDC_IP_CLASSA, m_nIPaddrA);
	DDV_MinMaxUInt(pDX, m_nIPaddrA, 0, 255);
	DDX_Text(pDX, IDC_IP_CLASSB, m_nIPaddrB);
	DDV_MinMaxUInt(pDX, m_nIPaddrB, 0, 255);
	DDX_Text(pDX, IDC_IP_CLASSC, m_nIPaddrC);
	DDV_MinMaxUInt(pDX, m_nIPaddrC, 0, 255);
	DDX_Text(pDX, IDC_IP_CLASSD, m_nIPaddrD);
	DDV_MinMaxUInt(pDX, m_nIPaddrD, 0, 255);
	DDX_Check(pDX, IDC_REMOTE_IP, m_bRemoteIP);
	DDX_Check(pDX, IDC_MAPUPDATE, m_bMapUpdate);
	DDX_Text(pDX, IDC_LONG_CORR, m_dLongCorrection);
	DDV_MinMaxDouble(pDX, m_dLongCorrection, -0.1, 0.1);
	DDX_Text(pDX, IDC_LAT_CORR, m_dLatCorrection);
	DDV_MinMaxDouble(pDX, m_dLatCorrection, -0.1, 0.1);
	DDX_Text(pDX, IDC_PORT, m_nServPortNum);
	DDV_MinMaxUInt(pDX, m_nServPortNum, 1024, 10000);
	DDX_Check(pDX, IDC_TCPMAPENABLE, m_bTCPMapEnable);
	DDX_Text(pDX, IDC_TCPMAPPORT, m_nTCPPortNum);
	DDV_MinMaxUInt(pDX, m_nTCPPortNum, 1024, 10000);
	DDX_Text(pDX, IDC_LAT_MEAN, m_LatMean);
	DDV_MinMaxInt(pDX, m_LatMean, -180, 180);
	DDX_Text(pDX, IDC_LATLONG_DEV, m_LatLongDev);
	DDV_MinMaxUInt(pDX, m_LatLongDev, 0, 180);
	DDX_Text(pDX, IDC_LONG_MEAN, m_LongMean);
	DDV_MinMaxInt(pDX, m_LongMean, -180, 180);
	DDX_Check(pDX, IDC_SAVE_ENABLED, m_bSaveAllData);
	DDX_Text(pDX, IDC_GMT_TIME, m_nGMTTime);
	DDX_Check(pDX, IDC_REPORT, m_bSaveReport);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetupDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupDlg)
	ON_BN_CLICKED(IDC_REMOTE_IP, OnRemoteIp)
	ON_BN_CLICKED(IDC_MAPUPDATE, OnMapupdate)
	ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetupDlg message handlers

void CSetupDlg::OnRemoteIp() 
{
	CWnd *pClassA = GetDlgItem(IDC_IP_CLASSA); // Get a pointer to the control's window.
	CWnd *pClassB = GetDlgItem(IDC_IP_CLASSB); // Get a pointer to the control's window.
	CWnd *pClassC = GetDlgItem(IDC_IP_CLASSC); // Get a pointer to the control's window.
	CWnd *pClassD = GetDlgItem(IDC_IP_CLASSD); // Get a pointer to the control's window.

	UpdateData();
	if(m_bRemoteIP)
	{
		// Enable the control.
        pClassA->EnableWindow(TRUE);
		pClassB->EnableWindow(TRUE);
		pClassC->EnableWindow(TRUE);
		pClassD->EnableWindow(TRUE);
	}
	else
	{   // Disable the control. And set to IP Loopback address
		m_nIPaddrA=127;
		m_nIPaddrB=0;
		m_nIPaddrC=0;
		m_nIPaddrD=1;
		pClassA->EnableWindow(FALSE);
		pClassB->EnableWindow(FALSE);
		pClassC->EnableWindow(FALSE);
		pClassD->EnableWindow(FALSE);
	}
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CSetupDlg::OnMapupdate() 
{
	CWnd *pMapUp = GetDlgItem(IDC_PORTNUM); // Get a pointer to the control's window.
	CWnd *pRmIP = GetDlgItem(IDC_REMOTE_IP);

	UpdateData();
	if(m_bMapUpdate)
	{
		// Enable the Port Number Control
		pMapUp->EnableWindow(TRUE);
		pRmIP->EnableWindow(TRUE);
	}
	else
	{
		// Disable the Port Number Control
		pMapUp->EnableWindow(FALSE);
		m_bRemoteIP=FALSE;
		OnRemoteIp();
		pRmIP->EnableWindow(FALSE);
	}

}

CString CSetupDlg::NumToIPString()
{
	char bufa[5];
	char bufb[5];
	char bufc[5];
	char bufd[5];

	itoa(m_nIPaddrA,bufa,10);
	itoa(m_nIPaddrB,bufb,10);
	itoa(m_nIPaddrC,bufc,10);
	itoa(m_nIPaddrD,bufd,10);

	CString ClassA(bufa);
	CString ClassB(bufb);
	CString ClassC(bufc);
	CString ClassD(bufd);

	CString IPString(ClassA+"."+ClassB+"."+ClassC+"."+ClassD);

	return (IPString);
}

void CSetupDlg::IPStringToNum()
{
	int i=0;  // Counter variable
	int j=1;  // Which part of dotted quad are we working on?
	int lastplace=0;
	CString tempbuf("");
	CString dot(".");

	for (i = 0; i < IPAddress.GetLength(); i++)
	{
		if(IPAddress.GetAt(i)==dot)
		{
			
			tempbuf=IPAddress.Mid(lastplace,(i-lastplace));
			switch(j)
			{
			case 1:
				m_nIPaddrA=atoi(tempbuf.GetBuffer(i-lastplace));
				break;
			case 2:
				m_nIPaddrB=atoi(tempbuf.GetBuffer(i-lastplace));
				break;
			case 3:
				m_nIPaddrC=atoi(tempbuf.GetBuffer(i-lastplace));
				break;
			case 4:
				m_nIPaddrD=atoi(tempbuf.GetBuffer(i-lastplace));
				break;
			default:  // Should not happen
				break;
			}
			j++;
			lastplace=i+1;

		}
	}
	tempbuf=IPAddress.Mid((lastplace),IPAddress.GetLength());
	m_nIPaddrD=atoi(tempbuf.GetBuffer(IPAddress.GetLength()-lastplace));


}

void CSetupDlg::OnOK() 
{
	UpdateData();
	IPAddress=NumToIPString();
	
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CSetupDlg::SetMapUpdate(BOOL update)
{
	m_bMapUpdate=update;  // TRUE if update is to occur
}

BOOL CSetupDlg::GetMapUpdate()
{
	return(m_bMapUpdate);
}


void CSetupDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	IPStringToNum();
	OnMapupdate();
	OnRemoteIp();
	// UpdateData(FALSE);
	// TODO: Add your message handler code here
	
}

void CSetupDlg::SetIPString(CString IPaddr)
{
	IPAddress=IPaddr;
	IPStringToNum();
}

CString CSetupDlg::GetIPString()
{
	return(IPAddress);
}

BOOL CSetupDlg::SetCorrection(int lat,int lon)
{
	if((lat<1000) && (lat>-1000))
		m_dLatCorrection=(double)lat/10000;
	else
		return(0);
	if(lon<1000 && lon>-1000)
		m_dLongCorrection=(double)lon/10000;
	else
		return(0);
	return(1);

}

void CSetupDlg::GetCorrection(int *lat,int *lon)
{
	*lat=(int)(m_dLatCorrection*10000);
	*lon=(int)(m_dLongCorrection*10000);
}

void CSetupDlg::SetServerPortNum(UINT port)
{
	if (port>1024 && port<10000)
		m_nServPortNum=port;
	else
		m_nServPortNum=DEFAULT_PORT;
}

UINT CSetupDlg::GetServerPortNum()
{
	return(m_nServPortNum);
}

void CSetupDlg::SetLatMean(int latmean)
{
	if(latmean>=-180 && latmean <=180)
		m_LatMean=latmean;
	else
		m_LatMean=49;  // Set it to a default value
}

void CSetupDlg::SetLongMean(int longmean)
{
	if(longmean>=-180 && longmean <=180)
		m_LongMean=longmean;
	else
		m_LongMean=-123;  // Set it to a default value

}

void CSetupDlg::SetLatLongDev(UINT dev)
{
	if(dev>=0 && dev<=180)
		m_LatLongDev=dev;
	else
		m_LatLongDev=2;  // Set it to a default value
}

void CSetupDlg::SetBackup(BOOL var)
{
	m_bSaveAllData=var;  // True if we want to use ODBC to save data to 
						// A backup database;
}
