#include "QDEMResampleDialog.h"
#include "ui_QDEMResampleDialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include "ImageOperatorBase.h"

QDEMResampleDialog::QDEMResampleDialog(QWidget *parent)
	: QDialog(parent),ui(new Ui::QDEMResampleDialogClass)
{
	ui->setupUi(this);
	connect(ui->dialogBtn, SIGNAL(accepted()), this, SLOT(onAccepted()));
	connect(ui->dialogBtn, SIGNAL(rejected()), this, SLOT(onRejected()));

	connect(ui->DEMWSG84SelectBtn, SIGNAL(clicked(bool)), this, SLOT(onDEMWSG84SelectBtnClicked(bool)));
	connect(ui->outDEMSelectBtn, SIGNAL(clicked(bool)), this, SLOT(onoutDEMSelectBtnClicked(bool)));


}

QDEMResampleDialog::~QDEMResampleDialog()
{}

void QDEMResampleDialog::onDEMWSG84SelectBtnClicked(bool flag)
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getOpenFileName(this,
		u8"DEM Raster Select",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"tiff Files (*.tiff);;tif Files (*.tif);;dat Files (*.dat);;All Files (*.*)"); // 文件类型过滤器

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

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

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

void QDEMResampleDialog::onAccepted()
{
	double gridx = ui->doubleSpinBoxGridX->value();
	double gridy = ui->doubleSpinBoxGridY->value();

	QString inDEMPath = ui->lineEditDEMLLA->text();
	QString outDEMPath = ui->lineEditoutDEM->text();

	gdalImage demimg(inDEMPath);

	Eigen::MatrixXd srcgt = demimg.gt;
	// 
 
	qDebug() << "----- src gt ------------------------------------";
	qDebug() << QString("%1\t%2\t%3").arg(srcgt(0, 0)).arg(srcgt(0, 1)).arg(srcgt(0, 2));
	qDebug() << QString("%1\t%2\t%3").arg(srcgt(1, 0)).arg(srcgt(1, 1)).arg(srcgt(1, 2));
	qDebug() << "-----------------------------------------";

	double Xscale = srcgt(0, 1)/gridx;
	double Yscale = srcgt(1, 2)/gridy;

	int new_width = ceil(Xscale * demimg.width) + 1;
	int new_height = ceil(Yscale * demimg.height) + 1;

	srcgt(0, 1) = gridx;
	srcgt(1, 2) = gridy;


	qDebug() << "target gt";
	qDebug() << "----- target gt ------------------------------------";
	qDebug() << QString("%1\t%2\t%3").arg(srcgt(0, 0)).arg(srcgt(0, 1)).arg(srcgt(0, 2));
	qDebug() << QString("%1\t%2\t%3").arg(srcgt(1, 0)).arg(srcgt(1, 1)).arg(srcgt(1, 2));
	qDebug() << "-----------------------------------------";
	 
	std::shared_ptr<double> gt(new double[6]);
	gt.get()[0] = srcgt(0, 0);
	gt.get()[1] = srcgt(0, 1);
	gt.get()[2] = srcgt(0, 2);
	gt.get()[3] = srcgt(1, 0);
	gt.get()[4] = srcgt(1, 1);
	gt.get()[5] = srcgt(1, 2);

	qDebug() << "DEM ReSample start ...";
	qDebug() << "height:\t" << new_height;
	qDebug() << "width:\t" << new_width;

	qDebug() << ResampleGDAL(inDEMPath.toLocal8Bit().constData(),
		outDEMPath.toLocal8Bit().constData(), gt.get(), new_width, new_height, GDALResampleAlg::GRA_Bilinear);

	qDebug() << "DEM ReSample finished!!!";
}

void QDEMResampleDialog::onRejected()
{
	this->close();
}






