#include "QSARSimulationComplexEchoDataDialog.h"
#include "ui_QSARSimulationComplexEchoDataDialog.h"
#include <QFileDialog>		
#include <QMessageBox>
#include "BaseConstVariable.h"
#include "BaseTool.h"
#include "ImageNetOperator.h"
#include <QDebug>
#include "FileOperator.h"
#include "ImageOperatorBase.h"


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


	connect(ui->pushButtonEchoDataSelect, SIGNAL(clicked()), this, SLOT(onpushButtonEchoDataSelect_clicked()));
	connect(ui->pushButtonLookTableSelect, SIGNAL(clicked()), this, SLOT(onpushButtonLookTableSelect_clicked()));
	connect(ui->pushButtonL1AEchoDataSelect, SIGNAL(clicked()), this, SLOT(onpushButtonL1AEchoDataSelect_clicked()));
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbuttonBox_accepted()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbuttonBox_rejected()));

}

QSARSimulationComplexEchoDataDialog::~QSARSimulationComplexEchoDataDialog()
{


}

void QSARSimulationComplexEchoDataDialog::onpushButtonEchoDataSelect_clicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                            // 父窗口
		tr(u8"选择地距数据文件"),               // 标题
		QString(),                       // 默认路径
		tr(ENVI_FILE_FORMAT_FILTER)  // 文件过滤器
	);
	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		QString message = "选择的文件有：\n";
		this->ui->lineEditEchoDataPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QSARSimulationComplexEchoDataDialog::onpushButtonLookTableSelect_clicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                            // 父窗口
		tr(u8"选择查找表回波数据文件"),               // 标题
		QString(),                       // 默认路径
		tr(ENVI_FILE_FORMAT_FILTER)  // 文件过滤器
	);
	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		QString message = "选择的文件有：\n";
		this->ui->lineEditLookTablePath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QSARSimulationComplexEchoDataDialog::onpushButtonL1AEchoDataSelect_clicked()
{
	QString  fileNames = QFileDialog::getSaveFileName(
		this,                            // 父窗口
		tr(u8"选择斜距数据文件"),               // 标题
		QString(),                       // 默认路径
		tr(ENVI_FILE_FORMAT_FILTER)  // 文件过滤器
	);
	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		QString message = "选择的文件有：\n";
		this->ui->lineEditL1AEchoDataPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QSARSimulationComplexEchoDataDialog::onbuttonBox_accepted()
{
	QString echoDataPath = this->ui->lineEditEchoDataPath->text().trimmed();
	QString RangelookTablePath = this->ui->lineEditLookTablePath->text().trimmed();
	QString l1AEchoDataPath = this->ui->lineEditL1AEchoDataPath->text().trimmed();

	if (isExists(echoDataPath) && isExists(RangelookTablePath)) {
		gdalImage echoData(echoDataPath);
		gdalImage RangelookTable(RangelookTablePath);
		if (echoData.getDataType() == GDT_CFloat32
			||echoData.getDataType()==GDT_CFloat64
			|| echoData.getDataType() == GDT_CInt16
			|| echoData.getDataType()==GDT_CInt32
			) {

			CreategdalImageComplex(l1AEchoDataPath, RangelookTable.height, RangelookTable.width,1, RangelookTable.gt, RangelookTable.projection, true, true);


			ResampleEChoDataFromGeoEcho(echoDataPath, RangelookTablePath, l1AEchoDataPath);
		}
		else {
			CreategdalImage(l1AEchoDataPath, RangelookTable.height, RangelookTable.width, 1, RangelookTable.gt, RangelookTable.projection, true, true);
			ResampleRangeDataFromGeoImage(echoDataPath, RangelookTablePath, l1AEchoDataPath);
		}
		
		QMessageBox::information(this, tr(u8"提示"), tr(u8"完成"));
	}
	else {
		QMessageBox::information(this, tr(u8"提示"), tr(u8"没有选择任何文件。"));
	}
	
}

void QSARSimulationComplexEchoDataDialog::onbuttonBox_rejected()
{
	this->close();
}
