#include "LAMPImageCreateClass.h"

LAMPImageCreateClass::LAMPImageCreateClass(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	this->simulationparams = std::make_shared<FEKOBase::FEKOSimulationDataparams>();

	this->ui.comboBox_ImageMode->addItem(FEKOBase::FEKOImageModeenumToString(FEKOBase::FEKOImageMode::Strip));
	this->ui.comboBox_ImageMode->addItem(FEKOBase::FEKOImageModeenumToString(FEKOBase::FEKOImageMode::Scane));
	this->ui.comboBox_ImageMode->addItem(FEKOBase::FEKOImageModeenumToString(FEKOBase::FEKOImageMode::ISAR));
	this->ui.comboBox_ImageMode->addItem(FEKOBase::FEKOImageModeenumToString(FEKOBase::FEKOImageMode::CircleSAR));



	// 限制lineedit输入为数值
	QDoubleValidator* doubleValidator = new QDoubleValidator();
	this->ui.lineEdit_minX->setValidator(doubleValidator);
	this->ui.lineEdit__maxX->setValidator(doubleValidator);
	this->ui.lineEdit__minY->setValidator(doubleValidator);
	this->ui.lineEdit__maxY->setValidator(doubleValidator);
	this->ui.lineEdit_planeZ->setValidator(doubleValidator);
	QIntValidator* intValidator = new QIntValidator(1,100000000000000);
	this->ui.lineEdit_height->setValidator(intValidator);
	this->ui.lineEdit_width->setValidator(intValidator);


	this->ui.lineEdit_minX->setText("-1.0");
	this->ui.lineEdit__maxX->setText("1.0");
	this->ui.lineEdit__minY->setText("-1.0");
	this->ui.lineEdit__maxY->setText("1.0");
	this->ui.lineEdit_planeZ->setText("0");
	this->ui.lineEdit_height->setText("101");
	this->ui.lineEdit_width->setText("101");
	this->ui.checkBoxTheta->setChecked(true);
	this->ui.checkBox_phi->setChecked(true);
}

LAMPImageCreateClass::~LAMPImageCreateClass()
{}

Q_INVOKABLE FEKOBase::FEKOImageSettingParams LAMPImageCreateClass::getFEKOImageSettingParams()
{

	FEKOBase::FEKOImageSettingParams result;
	result.min_x = this->ui.lineEdit_minX->text().toDouble();
	result.max_x = this->ui.lineEdit__maxX->text().toDouble();
	result.min_y = this->ui.lineEdit__minY->text().toDouble();
	result.max_y = this->ui.lineEdit__maxY->text().toDouble();
	result.plane_z = this->ui.lineEdit_planeZ->text().toDouble();
	result.ImageHeight = this->ui.lineEdit_height->text().toDouble();
	result.ImageWidth = this->ui.lineEdit_width->text().toDouble();
	
	return Q_INVOKABLE result;
}

void LAMPImageCreateClass::on_pushButton_theta_clicked()
{
	QString thetaechopath = getOpenFilePath(this, u8"打开theta回波文件", u8"theta Files (*.theta)");
	this->ui.lineEdit_thetaechopath->setText(thetaechopath);
}

void LAMPImageCreateClass::on_pushButton_phi_clicked()
{
	QString phiechopath = getOpenFilePath(this, u8"打开phi回波文件", u8"theta Files (*.phi)");
	this->ui.lineEdit_phiechopath->setText(phiechopath);
}

void LAMPImageCreateClass::on_pushButton_thetaimage_clicked()
{
	QString thetaimagePath = getSaveFilePath(this, u8"保存theta极化回波成像结果", u8"ENVI (*.dat)");
	this->ui.lineEdit_thetaimagepath->setText(thetaimagePath);
}

void LAMPImageCreateClass::on_pushButton_phiImage_clicked()
{
	QString phiimagePath = getSaveFilePath(this, u8"保存theta极化回波成像结果", u8"ENVI (*.dat)");
	this->ui.lineEdit_phiimagepath->setText(phiimagePath);
}

void LAMPImageCreateClass::on_checkBoxTheta_toggled(bool flag)
{
	this->ui.pushButton_theta->setEnabled(this->ui.checkBoxTheta->isChecked());
	this->ui.pushButton_thetaimage->setEnabled(this->ui.checkBoxTheta->isChecked());
	this->ui.lineEdit_thetaechopath->setEnabled(this->ui.checkBoxTheta->isChecked());
	this->ui.lineEdit_thetaimagepath->setEnabled(this->ui.checkBoxTheta->isChecked());
}

void LAMPImageCreateClass::on_checkBox_phi_toggled(bool flag)
{
	this->ui.pushButton_phi->setEnabled(this->ui.checkBox_phi->isChecked());
	this->ui.pushButton_phiImage->setEnabled(this->ui.checkBox_phi->isChecked());
	this->ui.lineEdit_phiechopath->setEnabled(this->ui.checkBox_phi->isChecked());
	this->ui.lineEdit_phiimagepath->setEnabled(this->ui.checkBox_phi->isChecked());

}

void LAMPImageCreateClass::on_pushButton_OK_clicked()
{

	FEKOBase::FEKOImageSettingParams imageparams = this->getFEKOImageSettingParams();
	// 计算回波
	if (this->ui.checkBoxTheta->isChecked()) { // theta
		 
		QString thetafileptah= this->ui.lineEdit_thetaechopath->text().trimmed();
		QString theta_tiff_filepath=this->ui.lineEdit_thetaimagepath->text().trimmed();
 
		FEKOBase::EchoDataClass data;
		data.loadEchoData(thetafileptah);
		if (this->simulationparams&&(this->simulationparams->imagemode==FEKOBase::FEKOImageMode::Strip|| this->simulationparams->imagemode == FEKOBase::FEKOImageMode::Scane)) {
			FEKOBase::FEKOImageProcess(data, imageparams, theta_tiff_filepath,FEKOBase::FEKOImageAlgorithm::TBP_FREQ,FEKOBase::ImageAlgWindowFun::HANMMING);
		}
		else {
			FEKOBase::FEKOImageProcess(data, imageparams, theta_tiff_filepath);
		}
	}
	else {

	}

	if (this->ui.checkBox_phi->isChecked()) { // phi
 
		QString phifileptah = this->ui.lineEdit_phiechopath->text().trimmed();
		QString phi_tiff_filepath = this->ui.lineEdit_phiimagepath->text().trimmed();
 
		FEKOBase::EchoDataClass data;
		data.loadEchoData(phifileptah);
		//FEKOBase::FEKOImageProcess(data, imageparams, phi_tiff_filepath);
		if (this->simulationparams && (this->simulationparams->imagemode == FEKOBase::FEKOImageMode::Strip || this->simulationparams->imagemode == FEKOBase::FEKOImageMode::Scane)) {
			FEKOBase::FEKOImageProcess(data, imageparams, phi_tiff_filepath, FEKOBase::FEKOImageAlgorithm::TBP_FREQ, FEKOBase::ImageAlgWindowFun::HANMMING);
		}
		else {
			FEKOBase::FEKOImageProcess(data, imageparams, phi_tiff_filepath);
		}
	}
	else {

	}
	
	QMessageBox::StandardButton  reply = QMessageBox::question(this, u8"提示", u8"是否查看图像", QMessageBox::Yes | QMessageBox::No);
	if (reply == QMessageBox::Yes) {
		LAMPDataShowClass* datashow=new LAMPDataShowClass();
		datashow->show();
		if (this->ui.checkBoxTheta->isChecked()) { // theta
			QString theta_tiff_filepath = this->ui.lineEdit_thetaimagepath->text().trimmed();
			datashow->load_complex_data(theta_tiff_filepath);

		}
		if (this->ui.checkBox_phi->isChecked()) { // phi
			QString phi_tiff_filepath = this->ui.lineEdit_phiimagepath->text().trimmed();
			datashow->load_complex_data(phi_tiff_filepath);
		}

	}
	else {
		return;
	}

 
}

void LAMPImageCreateClass::on_pushButton_cancel_clicked()
{
	this->close();
}

void LAMPImageCreateClass::on_pushButton_loadfekosimulationxml_clicked()
{
	QString  xmlpath = getOpenFilePath(this, u8"仿真任务xml",  u8"xml文件 (*.xml)");
	if (isExists(xmlpath)) {
		this->OpenFEKOSimulationImageSettingXml(xmlpath);
	}
	else {

	}
}

void LAMPImageCreateClass::OpenFEKOSimulationImageSettingXml(QString& xmlpath)
{
	this->simulationparams->loadXml(xmlpath);
	this->ui.lineEdit_TaskName->setText(this->simulationparams->taskName);
	this->ui.comboBox_ImageMode->setCurrentText(FEKOBase::FEKOImageModeenumToString(this->simulationparams->imagemode));
	this->ui.lineEdit_minX->setText(QString::number(this->simulationparams->x_min));
	this->ui.lineEdit__maxX->setText(QString::number(this->simulationparams->x_max));
	this->ui.lineEdit__minY->setText(QString::number(this->simulationparams->y_min));
	this->ui.lineEdit__maxY->setText(QString::number(this->simulationparams->y_max));
	this->ui.lineEdit_planeZ->setText(QString::number(this->simulationparams->z_plane));
	this->ui.lineEdit_height->setText(QString::number(this->simulationparams->imageheight));
	this->ui.lineEdit_width->setText(QString::number(this->simulationparams->imagewidth));
}

void LAMPImageCreateClass::bandingsetFEKOSimulationDataparams()
{
}

 
