#include "QImageSARRTPC.h"
#include <QFileDialog>
#include <QMessageBox>
#include "RTPCProcessCls.h"
#include <boost/thread.hpp>
#include <thread>

QImageSARRTPC::QImageSARRTPC(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);


	QObject::connect(ui.pushButtonRP, SIGNAL(clicked()), this, SLOT(onpushButtonRPClieck()));
	QObject::connect(ui.pushButtonTP, SIGNAL(clicked()), this, SLOT(onpushButtonTPClieck()));
	QObject::connect(ui.pushButtonEcho, SIGNAL(clicked()), this, SLOT(onpushButtonEchoClieck()));
	QObject::connect(ui.pushButtongpxml, SIGNAL(clicked()), this, SLOT(onpushButtongpxmlClieck()));
	QObject::connect(ui.pushButtonTaskxml, SIGNAL(clicked()), this, SLOT(onpushButtonTaskxmlClieck()));
	QObject::connect(ui.pushButtondem, SIGNAL(clicked()), this, SLOT(onpushButtondemClieck()));
	QObject::connect(ui.pushButtonlandcover, SIGNAL(clicked()), this, SLOT(onpushButtonlandcoverClieck()));
	QObject::connect(ui.pushButtonHHSigma0, SIGNAL(clicked()), this, SLOT(onpushButtonHHSigma0Clieck()));
	QObject::connect(ui.pushButtonHVSigma0, SIGNAL(clicked()), this, SLOT(onpushButtonHVSigma0Clieck()));
	QObject::connect(ui.pushButtonVHSigma0, SIGNAL(clicked()), this, SLOT(onpushButtonVHSigma0Clieck()));
	QObject::connect(ui.pushButtonVVSigma0, SIGNAL(clicked()), this, SLOT(onpushButtonVVSigma0Clieck()));


	QObject::connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(onBtnaccept()));
	QObject::connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(onBtnReject()));


}

QImageSARRTPC::~QImageSARRTPC()
{}

void QImageSARRTPC::onpushButtonRPClieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"接收方向图",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"csv Files (*.csv);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onpushButtonTPClieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"发射方向图",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"csv Files (*.csv);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onpushButtonEchoClieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getExistingDirectory(this, u8"选择回波存放路径", "");

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

}

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

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

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

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

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

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

void QImageSARRTPC::onpushButtonlandcoverClieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"地表覆盖数据",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"Image Files (*.tif);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onpushButtonHHSigma0Clieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"HH后向散射系数",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"Image Files (*.tif);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onpushButtonHVSigma0Clieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"HV后向散射系数",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"Image Files (*.tif);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onpushButtonVHSigma0Clieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"VH后向散射系数",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"Image Files (*.tif);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onpushButtonVVSigma0Clieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"VV后向散射系数",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"Image Files (*.tif);;All Files (*)"); // 文件类型过滤器

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

void QImageSARRTPC::onBtnaccept()
{


	QString GPSXmlPath = ui.gpsXmlPathEdit->text().trimmed();// u8"D:/Programme/vs2022/RasterMergeTest/TestData/GF3_Simulation_GPSNode.xml";
	QString TaskXmlPath = ui.taskXmlPathEdit->text().trimmed();//u8"D:/Programme/vs2022/RasterMergeTest/TestData/GF3_Simulation_Setting.xml";
	QString demTiffPath = ui.demTiffPathEdit->text().trimmed();//u8"D:/Programme/vs2022/RasterMergeTest/TestData/115E39N_COP30_clip.tif";
	QString landConverPath = ui.landCoverPathEdit->text().trimmed();// u8"D:/Programme/vs2022/RasterMergeTest/TestData/landcover_aligned.tiff";
	QString HHSigmaPath = ui.hhSigmaPathEdit->text().trimmed();// u8"D:/Programme/vs2022/RasterMergeTest/TestData/PALSAR-2/N45E116_23_MOS_F02DAR/N45E116_2023_sl_HH_F02DAR.tif";
	QString HVSigmaPath = ui.hvSigmaPathEdit->text().trimmed();// u8"D:/Programme/vs2022/RasterMergeTest/TestData/PALSAR-2/N45E116_23_MOS_F02DAR/N45E116_2023_sl_HV_F02DAR.tif";
	QString VHSigmaPath = ui.vhSigmaPathEdit->text().trimmed();// HVSigmaPath;
	QString VVSigmaPath = ui.vvSigmaPathEdit->text().trimmed();//HHSigmaPath;
	QString OutEchoPath = ui.outEchoPathEdit->text().trimmed();// u8"D:/Programme/vs2022/RasterMergeTest/TestData/outData/";
	QString simulationtaskName = ui.simulationTaskNameEdit->text().trimmed();// u8"GF3_Simulation";
	// 天线方向图
	QString TransAntPatternFilePath = ui.transformPatternFilePathEdit->text().trimmed();// "D:/Programme/vs2022/RasterMergeTest/TestData/ant/ant_model_setting_Horn_conical1_FarField-theta.csv"; // 
	QString ReceiveAntPatternFilePath = ui.receivePatternFilePathEdit->text().trimmed();//"D:/Programme/vs2022/RasterMergeTest/TestData/ant/ant_model_setting_Horn_conical1_FarField-phi.csv";

	// 打印参数
	// 打印解析的参数
	std::cout << "GPS XML Path: " << GPSXmlPath.toStdString() << "\n"
		<< "Task XML Path: " << TaskXmlPath.toStdString() << "\n"
		<< "DEM TIFF Path: " << demTiffPath.toStdString() << "\n"
		<< "Land Cover Path: " << landConverPath.toStdString() << "\n"
		<< "HH Sigma Path: " << HHSigmaPath.toStdString() << "\n"
		<< "HV Sigma Path: " << HVSigmaPath.toStdString() << "\n"
		<< "VH Sigma Path: " << VHSigmaPath.toStdString() << "\n"
		<< "VV Sigma Path: " << VVSigmaPath.toStdString() << "\n"
		<< "Trans AntPattern Path: " << TransAntPatternFilePath.toStdString() << "\n"
		<< "Reception AntPattern Path: " << ReceiveAntPatternFilePath.toStdString() << "\n"
		<< "Output Path: " << OutEchoPath.toStdString() << "\n"
		<< "Simulation Task Name: " << simulationtaskName.toStdString() << "\n";

	long cpucore_num = std::thread::hardware_concurrency();

	RTPCProcessMain(cpucore_num, TransAntPatternFilePath, ReceiveAntPatternFilePath, simulationtaskName, OutEchoPath, GPSXmlPath, TaskXmlPath, demTiffPath, landConverPath, HHSigmaPath, HVSigmaPath, VHSigmaPath, VVSigmaPath);

}

void QImageSARRTPC::onBtnReject()
{
	this->close();
}
