#include "QComplex2AmpPhase.h"
#include <QtWidgets>
#include <QFileDialog>
#include "FileOperator.h"
#include "BaseTool.h"
#include "SARSimulationImageL1.h"
#include "GF3CalibrationAndGeocodingClass.h"

QComplex2AmpPhase::QComplex2AmpPhase(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	QObject::connect(ui.radioButtonAmp,SIGNAL(toggled(bool)),this,SLOT(radioButtonAmptoggled(bool))); 
	QObject::connect(ui.radioButtonPhase, SIGNAL(toggled(bool)), this, SLOT(radioButtonPhasetoggled(bool)));
	QObject::connect(ui.radioButtonSigma0, SIGNAL(toggled(bool)), this, SLOT(radioButtonSigma0toggled(bool)));
	QObject::connect(ui.buttonBox, SIGNAL(accept()), this, SLOT(accept()));
	QObject::connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
	//toggled(bool )
}

QComplex2AmpPhase::~QComplex2AmpPhase()
{


}

void QComplex2AmpPhase::accept()
{
	QProgressDialog progressDialog(u8"复数转换", u8"终止", 0, ui.listWidgetImg->count());
	progressDialog.setWindowTitle(u8"复数转换");
	progressDialog.setWindowModality(Qt::WindowModal);
	progressDialog.setAutoClose(true);
	progressDialog.setValue(0);
	progressDialog.setMaximum(ui.listWidgetImg->count());
	progressDialog.setMinimum(0);
	progressDialog.show();
	QString outworkdir = ui.lineEditWorkDir->text();
	for (long i = 0; i < ui.listWidgetImg->count(); i++) {
		QString imgfilepath = ui.listWidgetImg->item(i)->text();
		progressDialog.setLabelText(u8"复数转换影像：" + getFileNameWidthoutExtend(imgfilepath));
		QString filename = getFileNameWidthoutExtend(imgfilepath);
		QString folderpath = getParantFromPath(imgfilepath);
		SARSimulationImageL1Dataset slcl1(RasterLevel::RasterSLC);
		slcl1.Open(folderpath, filename);
		QString l2bfilename = filename + ui.lineEditHZ->text();
		SARSimulationImageL1Dataset l1B(RasterLevel::RasterL1B);
		slcl1.OpenOrNew(outworkdir, l2bfilename,slcl1.getrowCount(),slcl1.getcolCount());
		QString srcxmlpath = slcl1.getxmlFilePath();
		QString tarxmlpath = l1B.getxmlFilePath();
		copyAndReplaceFile(srcxmlpath, tarxmlpath);
		l1B.loadFromXml();
		if (ui.radioButtonAmp->isChecked()) {
			Complex2AmpRaster(imgfilepath, slcl1.getImageRasterPath());
		}
		else if (ui.radioButtonPhase->isChecked()) {
			Complex2PhaseRaster(imgfilepath, slcl1.getImageRasterPath());
		}
		else if (ui.radioButtonSigma0->isChecked()) {
			Complex2dBRaster(imgfilepath, slcl1.getImageRasterPath());
		}
		progressDialog.setValue(i);
	}
	progressDialog.close();
}

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

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

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

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

void QComplex2AmpPhase::onpushButtonWorkSpaceClicked(bool)
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getExistingDirectory(this, u8"选择工作空间路径", "");

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

void QComplex2AmpPhase::radioButtonAmptoggled(bool flag)
{
	if (ui.radioButtonAmp->isChecked()) {
		ui.lineEditHZ->setText("_amp");
	}
	else if (ui.radioButtonPhase->isChecked()) {
		ui.lineEditHZ->setText("_phase");
	}
	else if (ui.radioButtonSigma0->isChecked()) {
		ui.lineEditHZ->setText("_dB");
	}

}

void QComplex2AmpPhase::radioButtonPhasetoggled(bool flag)
{
	if (ui.radioButtonAmp->isChecked()) {
		ui.lineEditHZ->setText("_amp");
	}
	else if (ui.radioButtonPhase->isChecked()) {
		ui.lineEditHZ->setText("_phase");
	}
	else if (ui.radioButtonSigma0->isChecked()) {
		ui.lineEditHZ->setText("_dB");
	}
}

void QComplex2AmpPhase::radioButtonSigma0toggled(bool flag)
{
	if (ui.radioButtonAmp->isChecked()) {
		ui.lineEditHZ->setText("_amp");
	}
	else if (ui.radioButtonPhase->isChecked()) {
		ui.lineEditHZ->setText("_phase");
	}
	else if(ui.radioButtonSigma0->isChecked()) {
		ui.lineEditHZ->setText("_dB");
	}
}
