

#include "QSimulationSARPolynomialOrbitModel.h"
#include "ui_QSimulationSARPolynomialOrbitModel.h"
#include <QFileDialog>
#include "OribtModelOperator.h"
#include "LogInfoCls.h"
#include "SARSatelliteSimulationAbstractCls.h"
#include "SatelliteOribtModel.h"

QSimulationPolynomialSAROrbitModel::QSimulationPolynomialSAROrbitModel(QWidget *parent)
	: QDialog(parent),ui(new Ui::QSimulationSARPolynomialOrbitModelClass)
{
	ui->setupUi(this);
	connect(ui->pushButtongpxml, SIGNAL(clicked(bool)), this, SLOT(onpushButtongpxmlClicked(bool)));
	connect(ui->pushButtonOrbitModel, SIGNAL(clicked(bool)), this, SLOT(onpushButtonOrbitModelClicked(bool)));
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbtnaccepted()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbtnrejected()));
}

QSimulationPolynomialSAROrbitModel::~QSimulationPolynomialSAROrbitModel()
{}

void QSimulationPolynomialSAROrbitModel::onpushButtongpxmlClicked(bool)
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getOpenFileName(this,
		u8"GPS xml",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"xml Files (*.xml)"); // 文件类型过滤器

	if (!fileName.isEmpty()) {
		this->ui->gpsXmlPathEdit->setText(fileName);
	}
	else {
		QMessageBox::information(this, u8"没有选择文件", u8"没有选择任何文件");
	}
}
void QSimulationPolynomialSAROrbitModel::onpushButtonOrbitModelClicked(bool)
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"Polynomial GPS Model xml",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"xml Files (*.xml)"); // 文件类型过滤器

	if (!fileName.isEmpty()) {
		this->ui->modelxmlPathEdit->setText(fileName);
	}
	else {
		QMessageBox::information(this, u8"没有选择文件", u8"没有选择任何文件");
	}
}

void QSimulationPolynomialSAROrbitModel::onbtnaccepted()
{
	QString gpsxmlpath = this->ui->gpsXmlPathEdit->text();
	QString outmodelpath = this->ui->modelxmlPathEdit->text();
	qDebug() << "Input GPS Xml:\t" << gpsxmlpath;
	qDebug() << "out Model Xml:\t" << outmodelpath;

	//2. 读取GPS节点
	std::vector<SatelliteOribtNode> nodes;
	ErrorCode stateCode = ReadSateGPSPointsXML(gpsxmlpath, nodes);

	if (stateCode != ErrorCode::SUCCESS)
	{
		qWarning() << QString::fromStdString(errorCode2errInfo(stateCode));
		return;
	}
	else {}
	std::shared_ptr<PolyfitSatelliteOribtModel> ployfitOribtModel = std::make_shared< PolyfitSatelliteOribtModel>();
	double startTime = 0;
	for (long i = 0; i < nodes.size(); i++) {
		ployfitOribtModel->addOribtNode(nodes[i]);
		startTime += nodes[i].time / nodes.size();
	}
	long polynum = 3;
	qDebug() << "refrence time " << startTime;
	qDebug() << "poly degree " << polynum;
	
	ployfitOribtModel->setSatelliteOribtStartTime(startTime);
	stateCode = ployfitOribtModel->polyFit(polynum, false);
	if (stateCode != ErrorCode::SUCCESS) {
		qDebug() << QString::fromStdString(errorCode2errInfo(stateCode));
		return ;
	}

	ployfitOribtModel->saveToXml(outmodelpath);
	qDebug() << "PolynomialSAROrbitModel fit finished!!";
	QMessageBox::information(this, u8"信息", u8"模型拟合成功");
}

void QSimulationPolynomialSAROrbitModel::onbtnrejected()
{
	this->close();
}



