#include "ImagePlaneAtiInterpDialog.h"
#include "ui_ImagePlaneAtiInterpDialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QCheckBox>
#include "BaseConstVariable.h"
#include "ImageNetOperator.h"

ImagePlaneAtiInterpDialog::ImagePlaneAtiInterpDialog(QWidget *parent)
	: QDialog(parent)
	, ui(new Ui::ImagePlaneAtiInterpDialogClass)
{
	ui->setupUi(this);
	
	connect(ui->pushButtonImageLLASelect, SIGNAL(clicked()), this, SLOT(onpushButtonImageLLASelect_clicked()));
	connect(ui->pushButtonImageNet0Select, SIGNAL(clicked()), this, SLOT(onpushButtonImageNet0Select_clicked()));
	connect(ui->pushButtonRefRangeDEMSelect, SIGNAL(clicked()), this, SLOT(onpushButtonRefRangeDEMSelect_clicked()));
	connect(ui->pushButtonEchoGPSPointDataSelect, SIGNAL(clicked()), this, SLOT(onpushButtonEchoGPSPointSelect_clicked()));
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbuttonBoxAccepted()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbuttonBoxRejected()));


}

ImagePlaneAtiInterpDialog::~ImagePlaneAtiInterpDialog()
{}

void ImagePlaneAtiInterpDialog::onpushButtonImageNet0Select_clicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                            
		tr(u8"选择成像粗平面文件"),              
		QString(),                       
		tr(ENVI_FILE_FORMAT_FILTER)   
	);
	 
	if (!fileNames.isEmpty()) {
		QString message = "选中文件\n";
		this->ui->lineEditImageNet0Path->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"提示"), tr(u8"没有选中文件"));
	}
}

void ImagePlaneAtiInterpDialog::onpushButtonEchoGPSPointSelect_clicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择回波GPS坐标点文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"GPS坐标点文件(*.gpspos.data);;All Files(*.*)")  // 文件过滤器
	);
	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		QString message = "选择的文件有：\n";
		this->ui->lineEditGPSPointsPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void ImagePlaneAtiInterpDialog::onpushButtonRefRangeDEMSelect_clicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                           
		tr(u8"选择参考DEM"),              
		QString(),                     
		tr(ENVI_FILE_FORMAT_FILTER)  
	);
	if (!fileNames.isEmpty()) {
		QString message = "选中文件\n";
		this->ui->lineEditRefRangeDEMPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"提示"), tr(u8"没有选中文件"));
	}
}

void ImagePlaneAtiInterpDialog::onpushButtonImageLLASelect_clicked()
{
	QString  fileNames = QFileDialog::getSaveFileName(
		this,                           
		tr(u8"提示"),               
		QString(),                      
		tr(ENVI_FILE_FORMAT_FILTER)  
	);
	
	if (!fileNames.isEmpty()) {
		QString message = "选中文件\n";
		this->ui->lineEditImageLLAPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"提示"), tr(u8"没有选中文件"));
	}
}

void ImagePlaneAtiInterpDialog::onbuttonBoxAccepted()
{
	QString imageNet0Path = this->ui->lineEditImageNet0Path->text().trimmed();
	QString refRangeDEMPath = this->ui->lineEditRefRangeDEMPath->text().trimmed();
	QString imageLLAPath = this->ui->lineEditImageLLAPath->text().trimmed();
	QString echoGPSDataPath = this->ui->lineEditGPSPointsPath->text().trimmed();
	if (imageNet0Path.isEmpty() || refRangeDEMPath.isEmpty() || imageLLAPath.isEmpty()||echoGPSDataPath.isEmpty()) {
		QMessageBox::warning(this, tr(u8"提示"), tr(u8"没有选中文件"));
		return;
	}
	else {

	}

	if (GPSPointsNumberEqualCheck(imageNet0Path, echoGPSDataPath)) {  
	}
	else {
		QMessageBox::warning(nullptr, u8"警告", u8"回波GPS坐标点数目不一致");
		return;
	}
	
	bool checkflag= this->ui->checkBoxOverLap->isChecked();
	if (checkflag) {
		InterploateClipAtiByRefDEM(imageNet0Path, refRangeDEMPath, imageLLAPath, echoGPSDataPath);
		QMessageBox::information(this, tr(u8"提示"), tr(u8"完成"));
		return;
	}
	else {
		if (OverlapCheck(imageNet0Path, refRangeDEMPath)) { // ????DEM???
			InterploateAtiByRefDEM(imageNet0Path, refRangeDEMPath, imageLLAPath, echoGPSDataPath);

			QMessageBox::information(nullptr, u8"提示", u8"completed!!");
			return;

		}
		else {
			QMessageBox::warning(nullptr, u8"警告", u8"DEM影像小于成像粗平面采样要求");
			return;
		}
	}
}

void ImagePlaneAtiInterpDialog::onbuttonBoxRejected()
{
	this->close();
}

 