#include "QtCreateGPSPointsDialog.h"
#include "ui_QtCreateGPSPointsDialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDateTime>
#include <QTime>
#include <QDate>
#include <fstream>
#include <QDebug>
#include "SPG4Function.h"


QtCreateGPSPointsDialog::QtCreateGPSPointsDialog(QWidget *parent)
	: QDialog(parent),ui(new Ui::QtCreateGPSPointsDialogClass)
{
	ui->setupUi(this);

	// 控件绑定槽函数
	connect(ui->LTESelectBtn, SIGNAL(clicked()), this, SLOT(onLTESelectBtnClicked()));
	connect(ui->outGPSxmlSelectBtn, SIGNAL(clicked()), this, SLOT(onoutGPSxmlSelectBtnClicked()));
	connect(ui->dialogBtn, SIGNAL(accepted()), this, SLOT(onaccepted()));
	connect(ui->dialogBtn, SIGNAL(rejected()), this, SLOT(onrejected()));


}

QtCreateGPSPointsDialog::~QtCreateGPSPointsDialog()
{}

void QtCreateGPSPointsDialog::onLTESelectBtnClicked()
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"TLE File(*.TLE);;Txt File(*.txt);;All File(*,*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->lineEditLTE->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtCreateGPSPointsDialog::onoutGPSxmlSelectBtnClicked()
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择影像文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"xml Files (*.xml)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->lineEditoutGPSxml->setText(fileName);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtCreateGPSPointsDialog::onaccepted()
{
	QString inLTEPath = this->ui->lineEditLTE->text();
	QString outGPSxmlPath = this->ui->lineEditoutGPSxml->text();

	QDateTime startTime = this->ui->StartimedateTimeEdit->dateTime().toUTC();
	QDateTime endTime = this->ui->EndimedateTimeEdit->dateTime().toUTC();

	double start = startTime.toTime_t();
	double end = endTime.toTime_t();
	// 转换为utc时间



	long gpsoint = this->ui->spinBoxGPSPoints->value();

	qDebug() << "inLTEPath:" << inLTEPath;
	qDebug() << "outGPSxmlPath:" << outGPSxmlPath;
	qDebug() << "start:" << start << " str:" << this->ui->StartimedateTimeEdit->text();
	qDebug() << "end:" << end << " str:" << this->ui->EndimedateTimeEdit->text();
	qDebug() << "gpsoint:" << gpsoint;

	// 读取TLE文件
	std::ifstream ifs(inLTEPath.toLocal8Bit().constData());
	if (!ifs.is_open()) {
		QMessageBox::warning(this, tr(u8"提示"), tr(u8"open file failed!!"));
		return;
	}

	// 读取TLE文件
	std::string line;
	std::string tle1, tle2;
	while (std::getline(ifs, line)) {
		if (line.find("1 ") != std::string::npos) {
			tle1 = line;
		}
		else if (line.find("2 ") != std::string::npos) {
			tle2 = line;
		}
	}

	ifs.close();

	// 根据两行根数计算时间偏移情况
	// 这里假设tle1和tle2包含时间信息，并且可以解析为时间偏移量
	double tle1TimeOffset = parseTLETimeOffset(tle1);
	//double tle2TimeOffset = parseTLETimeOffset(tle2);

	start = start- tle1TimeOffset;
	end = end- tle1TimeOffset;

	double inc = (end - start) / (gpsoint-1);

	start = start / 60.0; // 转换为分钟
	end = end / 60.0;
	inc = inc / 60.0;


	std::vector<SatelliteAntPos> gpspoints = getGPSPoints(tle1, tle2, start, end, inc, true,true,false);

	qDebug() << "create gpspoints size:" << gpspoints.size();
	// 计算GPS点
	std::ofstream ofs(outGPSxmlPath.toLocal8Bit().constData());
	if (!ofs.is_open()) {
		QMessageBox::warning(this, tr(u8"提示"), tr(u8"open file failed!!"));
		return;
	}

	ofs << "<root>\n<GPS>\n";
	for (const auto& point : gpspoints) { // 缩放单位
		ofs << "<GPSParam>\n";
		ofs << "<TimeStamp>" << QDateTime::fromTime_t(point.time*60.0+ tle1TimeOffset, Qt::UTC).toLocalTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toStdString() << "</TimeStamp>\n";
		ofs << "<xPosition>" << point.Px*1000.0 << "</xPosition>\n";
		ofs << "<yPosition>" << point.Py*1000.0 << "</yPosition>\n";
		ofs << "<zPosition>" << point.Pz*1000.0 << "</zPosition>\n";
		ofs << "<xVelocity>" << point.Vx*1000.0 << "</xVelocity>\n";
		ofs << "<yVelocity>" << point.Vy*1000.0 << "</yVelocity>\n";
		ofs << "<zVelocity>" << point.Vz*1000.0 << "</zVelocity>\n";
		ofs << "</GPSParam>\n";
	}
	ofs << "</GPS>\n</root>\n";
	ofs.flush();
	ofs.close();

	QMessageBox::warning(this, tr(u8"提示"), tr(u8"completed!!"));

}

void QtCreateGPSPointsDialog::onrejected()
{
	this->close();
}


 