#include "QOrthSlrRaster.h"
#include <QtWidgets>
#include <QFileDialog>
#include "QToolProcessBarDialog.h"
#include "BaseTool.h"
#include "FileOperator.h"
#include "GF3CalibrationAndGeocodingClass.h"


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

	connect(ui.pushButtonAdd, SIGNAL(clicked(bool)), this, SLOT(onpushButtonAddClicked(bool)));
	connect(ui.pushButtonRemove, SIGNAL(clicked(bool)), this, SLOT(onpushButtonRemoveClicked(bool)));
	connect(ui.pushButtonDEMSelect, SIGNAL(clicked(bool)), this, SLOT(onpushButtonWorkSpaceClicked(bool)));
	connect(ui.pushButtonWorkSpace, SIGNAL(clicked(bool)), this, SLOT(pushButtonDEMSelectClicked(bool)));
	QObject::connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

QOrthSlrRaster::~QOrthSlrRaster()
{}

void QOrthSlrRaster::accept()
{
	QToolProcessBarDialog* processdialog = new QToolProcessBarDialog(this);
	processdialog->show();
	processdialog->showProcess(0.0, u8"正射影像生成");
	QVector<QString> infile(ui.listWidgetMetaxml->count());
	QString outworkdirstring = ui.lineEditWorkDir->text();
	QString inlooktablePath = ui.lineEditDEM->text();
	for (long i = 0; i < ui.listWidgetMetaxml->count(); i++) {
		infile[i] = ui.listWidgetMetaxml->item(i)->text();
		processdialog->showToolInfo(u8"输入影像：\t" + infile[i]);
		processdialog->showToolInfo(u8"输入查找表：\t" + inlooktablePath);
		processdialog->showToolInfo(u8"输出工作空间：\t" + outworkdirstring);
		QString outname = getFileNameWidthoutExtend(infile[i])+"_orth.tif";
		processdialog->showToolInfo(u8"正射文件名：\t" + outname);
		QString outstring = JoinPath(outworkdirstring, outname);

		if (GF3OrthSLC(infile[i], inlooktablePath, outstring) == SUCCESS) {
			processdialog->showToolInfo(u8"正射生成成功");
		}
		else {
			processdialog->showToolInfo(u8"正射生成失败");
		}
	}
	processdialog->close();
}

void QOrthSlrRaster::reject()
{
	this->close();
}

void QOrthSlrRaster::onpushButtonAddClicked(bool)
{
	QStringList fileNames = QFileDialog::getOpenFileNames(
		this,                           // 父窗口
		tr(u8"选择xml文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"xml Files (*.xml);;All Files (*)")  // 文件过滤器
	);

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

void QOrthSlrRaster::onpushButtonRemoveClicked(bool)
{
	QList<QListWidgetItem*> selectedItems = this->ui.listWidgetMetaxml->selectedItems();
	for (QListWidgetItem* item : selectedItems) {
		delete this->ui.listWidgetMetaxml->takeItem(this->ui.listWidgetMetaxml->row(item));
	}
}

void QOrthSlrRaster::onpushButtonWorkSpaceClicked(bool)
{
	QString fileName = QFileDialog::getExistingDirectory(this, u8"选择工作空间路径", "");

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

void QOrthSlrRaster::pushButtonDEMSelectClicked(bool)
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择tif文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;All Files (*)")  // 文件过滤器
	);

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

