#include "QSimulationBPImageMultiProduction.h"
#include <QFileDialog>
#include <QMessageBox>
#include "TBPImageAlgCls.h"
#include "EchoDataFormat.h"
#include <boost/thread.hpp>
#include <thread>
#include "ui_QSimulationBPImageMultiProduction.h"
#include "ImageNetOperator.h"

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

	QObject::connect(ui->pushButtonEchoSelect, SIGNAL(clicked()), this, SLOT(onpushButtonEchoSelectClicked()));
	QObject::connect(ui->LookTableBtn, SIGNAL(clicked()), this, SLOT(onpushButtonLookTableBtnClicked()));
	QObject::connect(ui->GridNetBtn, SIGNAL(clicked()), this, SLOT(onpushButtonGridNetBtnSelectClicked()));
	QObject::connect(ui->L1ASelectBtn, SIGNAL(clicked()), this, SLOT(onpushButtonL1ASelectBtnClicked()));
	QObject::connect(ui->L2SelectSelect, SIGNAL(clicked()), this, SLOT(onpushButtonL2SelectSelectClicked()));
	QObject::connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbtnaccepted()));
	QObject::connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbtnrejected()));
 




}

QSimulationBPImageMultiProduction::~QSimulationBPImageMultiProduction()
{}

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

	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		QString message = "选择的文件有：\n";
		this->ui->lineEditEchoPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

 
void QSimulationBPImageMultiProduction::onpushButtonLookTableBtnClicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择查找表文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"All Files(*)")  // 文件过滤器
	);

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

void QSimulationBPImageMultiProduction::onpushButtonGridNetBtnSelectClicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择成像网格文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"All Files(*)")  // 文件过滤器
	);

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

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

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

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

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

void QSimulationBPImageMultiProduction::onbtnaccepted()
{
	QString L1ADataPath = this->ui->lineEditL1AProductionPath->text().trimmed();
	QString L2DataPath = this->ui->lineEditL2ProductionPath->text().trimmed();
	QString looktablePath = this->ui->lineEditLookTablePath->text().trimmed();
	QString imgNetPath = this->ui->lineEditImageNetPath->text().trimmed();
	QString echoDataPath = this->ui->lineEditEchoPath->text().trimmed();

	this->hide();
	QString echofile = echoDataPath;
	QString outImageFolder = getParantFromPath(L2DataPath);
	QString imagename = getFileNameFromPath(L2DataPath);
	std::shared_ptr<EchoL0Dataset> echoL0ds(new EchoL0Dataset);
	echoL0ds->Open(echofile);

	std::shared_ptr< SARSimulationImageL1Dataset> imagL1(new SARSimulationImageL1Dataset);
	imagL1->setCenterAngle(echoL0ds->getCenterAngle());
	imagL1->setCenterFreq(echoL0ds->getCenterFreq());
	imagL1->setNearRange(echoL0ds->getNearRange());
	imagL1->setRefRange((echoL0ds->getNearRange() + echoL0ds->getFarRange()) / 2);
	imagL1->setFarRange(echoL0ds->getFarRange());
	imagL1->setFs(echoL0ds->getFs());
	imagL1->setLookSide(echoL0ds->getLookSide());

	gdalImage imgxyzimg(imgNetPath);
	imagL1->OpenOrNew(outImageFolder, imagename, imgxyzimg.height, imgxyzimg.width);

	qDebug() << u8"成像中";
	TBPImageAlgCls TBPimag;
	TBPimag.setEchoL0(echoL0ds);
	TBPimag.setImageL1(imagL1);
	long cpucore_num = std::thread::hardware_concurrency();
	TBPimag.setGPU(true);
	TBPimag.ProcessWithGridNet(cpucore_num, imgNetPath);
	qDebug() << u8"系统几何校正中";

	// 处理成像映射
	std::shared_ptr< SARSimulationImageL1Dataset> imagL2(new SARSimulationImageL1Dataset);
	imagL2->setCenterAngle(echoL0ds->getCenterAngle());
	imagL2->setCenterFreq(echoL0ds->getCenterFreq());
	imagL2->setNearRange(echoL0ds->getNearRange());
	imagL2->setRefRange((echoL0ds->getNearRange() + echoL0ds->getFarRange()) / 2);
	imagL2->setFarRange(echoL0ds->getFarRange());
	imagL2->setFs(echoL0ds->getFs());
	imagL2->setLookSide(echoL0ds->getLookSide());


	QString outL1AImageFolder = getParantFromPath(L1ADataPath);
	QString L1Aimagename = getFileNameFromPath(L1ADataPath);

	gdalImage Looktableimg(looktablePath);
	imagL2->OpenOrNew(outL1AImageFolder, L1Aimagename, Looktableimg.height, Looktableimg.width);

	QString  L1AEchoDataPath =imagL2->getImageRasterPath();

	ResampleEChoDataFromGeoEcho(imagL1->getImageRasterPath(), looktablePath, L1AEchoDataPath);


	this->show();

	QMessageBox::information(this,u8"成像",u8"成像结束");
 
}

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