#include "QtLookTableCorrectOffsetDialog.h"
#include "ui_QtLookTableCorrectOffsetDialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include "ImageOperatorBase.h"
#include "GeoOperator.h"
#include <QFile>
#include "BaseConstVariable.h"
#include <QProgressDialog>
#include "BaseTool.h"


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

    QPushButton* OutRasterBtn = ui->OutRasterBtn;
    QPushButton* InRasterBtn = ui->InRasterBtn;
    QDialogButtonBox* dialogBtn = ui->dialogBtn;

     connect(OutRasterBtn, SIGNAL(clicked()), this, SLOT(onOutRasterBtnClicked()));
    connect(InRasterBtn, SIGNAL(clicked()), this, SLOT(onInRasterBtnClicked()));
    connect(dialogBtn, SIGNAL(accepted()), this, SLOT(onAcceptBtnClicked()));
    connect(dialogBtn, SIGNAL(rejected()), this, SLOT(onRejectBtnClicked()));
 
}

QtLookTableCorrectOffsetDialog::~QtLookTableCorrectOffsetDialog()
{}

void QtLookTableCorrectOffsetDialog::onOutRasterBtnClicked()
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择影像文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->OutRasterlineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

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

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->InRasterlineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtLookTableCorrectOffsetDialog::onAcceptBtnClicked()
{
	this->ui->progressBar->setValue(0);
	QString OutRasterPath = this->ui->OutRasterlineEdit->text();
	QString InRasterPath = this->ui->InRasterlineEdit->text();
	double offsetRow = this->ui->OffsetRowdoubleSpinBox->value();
	double offsetCol = this->ui->OffsetColdoubleSpinBox->value();

	QFile::copy(InRasterPath, OutRasterPath); 

	gdalImage outds(OutRasterPath);
	gdalImage inds(InRasterPath);

	if (outds.height != inds.height || outds.width != inds.width) {
		QMessageBox::information(this, tr(u8"tip"), tr(u8"size not match!!"));
		return;
	}
	this->ui->progressBar->setMaximum(inds.height);
	this->ui->progressBar->setValue(0);

	long blocksize = Memory1MB / 8.0 / outds.width * 2000;
	for (long i = 0; i < outds.height; i = i + blocksize) {
		Eigen::MatrixXd Rowdata = inds.getData(i, 0, blocksize, outds.width, 1);
		Eigen::MatrixXd Coldata = inds.getData(i, 0, blocksize, outds.width, 2);
		Rowdata = Rowdata.array() + offsetRow;
		Coldata = Coldata.array() + offsetCol;

		outds.saveImage(Rowdata, i, 0, 1);
		outds.saveImage(Coldata, i, 0, 2);
		this->ui->progressBar->setValue(i);
	}
	this->ui->progressBar->setValue(inds.height);

	QMessageBox::information(this, tr(u8"tip"), tr(u8"completed!!"));
}

void QtLookTableCorrectOffsetDialog::onRejectBtnClicked()
{
	this->close();
}
