// ModifyLinePointDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "ModifyLinePointDlg.h"
#include "afxdialogex.h"
#include <cstdio>
#include "string"
#include <iostream>
#include <sstream>
#include <regex>

// CModifyLinePointDlg 对话框

IMPLEMENT_DYNAMIC(CModifyLinePointDlg, CBCGPDialog)

CModifyLinePointDlg::CModifyLinePointDlg(CWnd* pParent /*=NULL*/)
	: CBCGPDialog(CModifyLinePointDlg::IDD, pParent)
	, m_bCirclingPoint(0)
{
	EnableVisualManagerStyle(TRUE, TRUE);
	m_gLat = 0.0;
	m_gLon = 0.0;

	m_bCheck = false;

	splitSymbol = "EWNSs+\/-,;°度′'’分\""”秒、,; ";
	dSymbol = "°";
	mSymbol = "′";
	sSymbol = "\"";

	m_bCirclingPointVisible = true;

	m_selectedPointID = 0;
}

CModifyLinePointDlg::~CModifyLinePointDlg()
{
}

void CModifyLinePointDlg::DoDataExchange(CDataExchange* pDX)
{
	CBCGPDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_TYPE, m_PositionType);
	DDX_Control(pDX, IDC_EDIT_LON, m_strLon);
	DDX_Control(pDX, IDC_EDIT_LAT, m_strLat);
	DDX_Control(pDX, IDC_COMBO_POINTID, m_PointID);
}


BEGIN_MESSAGE_MAP(CModifyLinePointDlg, CBCGPDialog)
	ON_CBN_SELCHANGE(IDC_COMBO_PT, &CModifyLinePointDlg::OnCbnSelchangeComboPt)
	ON_CBN_SELCHANGE(IDC_COMBO_TYPE, &CModifyLinePointDlg::OnCbnSelchangeComboType)
	ON_BN_CLICKED(IDOK, &CModifyLinePointDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CModifyLinePointDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_SETCIRCLINGPOINT, &CModifyLinePointDlg::OnBnClickedSetcirclingpoint)
	ON_EN_KILLFOCUS(IDC_EDIT_LON, &CModifyLinePointDlg::OnEnKillfocusEditLon)
	ON_EN_KILLFOCUS(IDC_EDIT_LAT, &CModifyLinePointDlg::OnEnKillfocusEditLat)
	ON_CBN_SELCHANGE(IDC_COMBO_POINTID, &CModifyLinePointDlg::OnCbnSelchangeComboPointid)
END_MESSAGE_MAP()



//设置盘旋点按钮是否可见
void CModifyLinePointDlg::SetCirclingPointRBTNVisible(bool isVisible)
{
	m_bCirclingPointVisible = isVisible;
};

//勾选盘旋点
void CModifyLinePointDlg::CheckedCirclingPoint()
{
	m_bCheck = true;
};

void CModifyLinePointDlg::SetLonLat(double lon,double lat)
{
	m_gLat = lat;
	m_gLon = lon;
}

void CModifyLinePointDlg::GetLonLat(double &lon,double &lat, int &pointID)
{
	lon = m_gLon;
	lat = m_gLat;
	pointID = m_selectedPointID;
}

// 经纬度转字符串度分秒
void CModifyLinePointDlg::getDMSString(double lon,double lat,string &str_lon,string &str_lat)
{
	int lon_d,lon_m,lat_d,lat_m;
	double lon_s,lat_s;
	angle.DegtoDms(lon,lon_d,lon_m,lon_s);
	angle.DegtoDms(lat,lat_d,lat_m,lat_s);

	string str_lat_s = angle.DoubleToString(lat_s,4);
	string str_lon_s = angle.DoubleToString(lon_s,4);

	str_lon = angle.IntToString(lon_d) + dSymbol + angle.IntToString(lon_m) + mSymbol + str_lon_s + sSymbol;
	str_lat = angle.IntToString(lat_d) + dSymbol + angle.IntToString(lat_m) + mSymbol + str_lat_s + sSymbol;
}

double CModifyLinePointDlg::getEditValue(const int ID)
{
	double value = -200.0;
	CString cstr;
	GetDlgItemText(ID,cstr);

	int typeID = m_PositionType.GetCurSel();

	if (typeID==0) //度
	{
		value = _tstof(cstr);
	}
	else if (typeID==1) //度分秒
	{
		vector<string> vec_str = angle.split(cstr.GetBuffer(),splitSymbol);

		int d = stoi(vec_str.at(0));
		int m = stoi(vec_str.at(1));
		double s = stod(vec_str.at(2));
		angle.DmstoDeg(d,m,s,value);
	}
	return value;
}

bool CModifyLinePointDlg::IsCirclingPoint()
{
	return m_bCirclingPoint;
}

void CModifyLinePointDlg::InitEidtText()
{
	string str_lon,str_lat;
	if (posTypeID==0)  //度
	{
		str_lon = angle.DoubleToString(m_gLon,7);
		str_lat = angle.DoubleToString(m_gLat,7);
	}
	else if (posTypeID == 1)  //度分秒
	{
		getDMSString(m_gLon,m_gLat,str_lon,str_lat);
	}

	// 编辑框中默认显示的经纬度   
	SetDlgItemText(IDC_EDIT_LON,str_lon.c_str());
	SetDlgItemText(IDC_EDIT_LAT,str_lat.c_str());
}

bool CModifyLinePointDlg::checkLon(string strLon,int type)
{
	if (type==0)  //度
	{
		string str_match = "^\\-?(\\d{1,2}\\.?\\d*|1[0-7]?\\d{1}\\.?\\d*|180\\.?0*)$";
		regex regexNumber1(str_match);
		return regex_match(strLon,regexNumber1);
	}
	else if (type==1)  //度分秒,xx;xx;xx.xx
	{
		wstring str_match = L"^[EW+-]?((\\d|[1-9]\\d|1[0-7]\\d)[s\\-,;°度/、;](\\d|[0-5]\\d)[s\\-,;/′、;'’ 分](\\d|[0-5]\\d)(\\.\\d{1,})?[s\\-,;/\""” 秒]?$)";
		wregex regexNumber1(str_match);
		return regex_match(angle.String2WString(strLon),regexNumber1);
	}

}

bool CModifyLinePointDlg::checkLat(string strLat,int type)
{
	if (type==0) //度
	{
		string sm = "^\\-?([0-8]?\\d{1}\\.?\\d*|90\\.?0*)$";
		regex regexNumber(sm);
		return regex_match(strLat,regexNumber);
	}
	else if (type==1)  //度分秒,xx;xx;xx.xx
	{
		wstring str_match2 = L"^[NS+-]?((\\d|[1-8]\\d)[s\\-,;°度/、;](\\d|[0-5]\\d)[s\\-,;/′、;'’ 分](\\d|[0-5]\\d)(\\.\\d{1,})?[s\\-,;/\""” 秒]?$)";
		wregex regexNumber2(str_match2);
		return regex_match(angle.String2WString(strLat),regexNumber2);
	}

}


// CModifyLinePointDlg 消息处理程序

BOOL CModifyLinePointDlg::OnInitDialog()
{
	CBCGPDialog::OnInitDialog();

	CenterWindow();

	m_PositionType.AddString(_T("度分秒"));
	m_PositionType.AddString(_T("度"));
	m_PositionType.SetCurSel(0);
	posTypeID = 0;
	lastTypeID = 0;
	m_bCirclingPoint = 0;

	// 编辑框中默认显示第一项的文字“谷歌”   
	InitEidtText();
	//SetDlgItemText(IDC_EDIT_LON,_T("119.623546"));
	//SetDlgItemText(IDC_EDIT_LAT,_T("32.562147"));

	GetDlgItem(IDC_SETCIRCLINGPOINT)->ShowWindow(m_bCirclingPointVisible);

	if (m_bCheck) //勾选盘旋点
	{
		((CButton *)GetDlgItem(IDC_SETCIRCLINGPOINT))->SetCheck(true);
		m_bCheck = true;
	}

	//在下拉列表中添加航点号
	CString cstr;
	for (int i=0;i<m_editLineDataGroup.pointNum;i++)
	{
		cstr.Format(_T("%d"), i+1);
		m_PointID.InsertString(i,cstr);
	}
	m_PointID.SetCurSel(m_selectedPointID-1);
	cstr.Format(_T("%f"), m_editLineDataGroup.pts[m_selectedPointID-1].dX);
	SetDlgItemText(IDC_EDIT_LON,cstr);
	cstr.Format(_T("%f"), m_editLineDataGroup.pts[m_selectedPointID-1].dY);
	SetDlgItemText(IDC_EDIT_LAT,cstr);

	return TRUE;
}

void CModifyLinePointDlg::OnCbnSelchangeComboPt()
{
	// TODO: 在此添加控件通知处理程序代码

}

// 坐标类型改变事件
void CModifyLinePointDlg::OnCbnSelchangeComboType()
{
	CString strLat,strLon;
	m_strLat.GetWindowTextA(strLat);
	m_strLon.GetWindowTextA(strLon);

	posTypeID = m_PositionType.GetCurSel();
	if (lastTypeID == posTypeID) //类型未改变,不执行坐标变换
	{
		return;
	}
	else
	{
		lastTypeID = posTypeID;
	}

	if (posTypeID==0) //度
	{
		double tarLon,tarLat;

		vector<string> vec_lat = angle.split(strLat.GetBuffer(),splitSymbol);
		vector<string> vec_lon = angle.split(strLon.GetBuffer(),splitSymbol);

		int lon_d = stoi(vec_lon.at(0));
		int lon_m = stoi(vec_lon.at(1));
		double lon_s = stod(vec_lon.at(2));
		angle.DmstoDeg(lon_d,lon_m,lon_s,tarLon);

		int lat_d = stoi(vec_lat.at(0));
		int lat_m = stoi(vec_lat.at(1));
		double lat_s = stod(vec_lat.at(2));
		angle.DmstoDeg(lat_d,lat_m,lat_s,tarLat);


		SetDlgItemText(IDC_EDIT_LON,angle.DoubleToString(tarLon).c_str());
		SetDlgItemText(IDC_EDIT_LAT,angle.DoubleToString(tarLat).c_str());
	}
	else if (posTypeID==1) //度分秒
	{
		double lat,lon;
		lat = _tstof(strLat);
		lon = _tstof(strLon);

		string str_lat,str_lon;
		getDMSString(lon,lat,str_lon,str_lat);

		SetDlgItemText(IDC_EDIT_LON,str_lon.c_str());
		SetDlgItemText(IDC_EDIT_LAT,str_lat.c_str());
	}
}

//确认按钮
void CModifyLinePointDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	m_gLon = getEditValue(IDC_EDIT_LON);
	m_gLat = getEditValue(IDC_EDIT_LAT);
	// 编辑框中默认显示的经纬度   
	//SetDlgItemText(IDC_EDIT_LON,str_lon.c_str());
	//SetDlgItemText(IDC_EDIT_LAT,str_lat.c_str());
	m_bCirclingPoint = ((CButton *)GetDlgItem(IDC_SETCIRCLINGPOINT))->GetCheck();
	CBCGPDialog::OnOK();
}


void CModifyLinePointDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CBCGPDialog::OnCancel();
	//CBCGPDialog::OnClose();
}


void CModifyLinePointDlg::OnBnClickedSetcirclingpoint()
{
	if (!m_bCheck)
	{
		((CButton *)GetDlgItem(IDC_SETCIRCLINGPOINT))->SetCheck(true);
		m_bCheck = true;
	}
	else
	{
		((CButton *)GetDlgItem(IDC_SETCIRCLINGPOINT))->SetCheck(false);
		m_bCheck = false;
	}
}


void CModifyLinePointDlg::OnEnKillfocusEditLon()
{
	// TODO: 在此添加控件通知处理程序代码
	CString cstr;
	GetDlgItemText(IDC_EDIT_LON,cstr);
	int typeID = m_PositionType.GetCurSel();

	bool  b = checkLon(cstr.GetBuffer(),typeID);
	if (!b)
	{
		//BCGPMessageBox("经度范围或格式有误,请重新输入!");
		m_strLon.SetFocus();
	}

}


void CModifyLinePointDlg::OnEnKillfocusEditLat()
{
	// TODO: 在此添加控件通知处理程序代码
	CString cstr;
	GetDlgItemText(IDC_EDIT_LAT,cstr);
	int typeID = m_PositionType.GetCurSel();

	bool  b = checkLat(cstr.GetBuffer(),typeID);
	if (!b)
	{
		//BCGPMessageBox("纬度范围或格式有误,请重新输入!");
		m_strLat.SetFocus();
	}

}

void CModifyLinePointDlg::SetSelectedPointID(int PointID){
	m_selectedPointID = PointID + 1;
	//m_selectedPointID = PointID;
}

// 选择航点下拉列表变化时触发
void CModifyLinePointDlg::OnCbnSelchangeComboPointid()
{
	// TODO: 在此添加控件通知处理程序代码
	m_selectedPointID = m_PointID.GetCurSel() + 1;
	TRACE(_T("当前选中航点ID: %d \n"),m_selectedPointID);
	//修改经纬度文本框的值
	CString cstr;
	if(posTypeID==0)
	{ //度
		cstr.Format(_T("%f"), m_editLineDataGroup.pts[m_selectedPointID-1].dX);
		SetDlgItemText(IDC_EDIT_LON,cstr);
		cstr.Format(_T("%f"), m_editLineDataGroup.pts[m_selectedPointID-1].dY);
		SetDlgItemText(IDC_EDIT_LAT,cstr);
	}
	else if(posTypeID==1)
	{	// 度分秒
		string str_lon, str_lat;
		getDMSString(m_editLineDataGroup.pts[m_selectedPointID-1].dX, m_editLineDataGroup.pts[m_selectedPointID-1].dY,str_lon, str_lat);
		SetDlgItemText(IDC_EDIT_LON,str_lon.c_str());
		SetDlgItemText(IDC_EDIT_LAT,str_lat.c_str());
	}
	
}