#include "QImageSARRFPC.h"
#include <QFileDialog>
#include <QMessageBox>
#include "RFPCProcessCls.h"
#include <boost/thread.hpp>
#include <thread>
#include "ui_QImageSARRFPC.h"


QImageSARRFPC::QImageSARRFPC(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->pushButtonsloper, SIGNAL(clicked()), this, SLOT(onpushButtonSloperClieck()));
 


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


}

QImageSARRFPC::~QImageSARRFPC()
{}

void QImageSARRFPC::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 QImageSARRFPC::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 QImageSARRFPC::onpushButtonEchoClieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getExistingDirectory(this, u8"选择回波存放路径", "");

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

}

void QImageSARRFPC::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 QImageSARRFPC::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 QImageSARRFPC::onpushButtondemClieck()
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getOpenFileName(this,
		u8"dem文件",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)"); // 文件类型过滤器

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

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

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

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

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

 
void QImageSARRFPC::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 demsloperPath = ui->sloperPathEdit->text().trimmed();
	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";

	// 打印参数
	// 打印解析的参数
	qDebug() << "GPS XML Path: " << GPSXmlPath << "\n"
		<< "Task XML Path: " << TaskXmlPath << "\n"
		<< "DEM TIFF Path: " << demTiffPath<< "\n"
		<< "Land Cover Path: " << landConverPath << "\n"
 
		<< "Trans AntPattern Path: " << TransAntPatternFilePath << "\n"
		<< "Reception AntPattern Path: " << ReceiveAntPatternFilePath << "\n"
		<< "Output Path: " << OutEchoPath << "\n"
		<< "Simulation Task Name: " << simulationtaskName << "\n";

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

	RFPCProcessMain(cpucore_num, 
		TransAntPatternFilePath, ReceiveAntPatternFilePath, simulationtaskName,
		OutEchoPath, GPSXmlPath, TaskXmlPath, demTiffPath, demsloperPath, landConverPath);

}

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