#include "OcctExportClass.h"
#include <qDebug>
#include <qmessagebox.h>


OcctExportClass::OcctExportClass(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	QObject::connect(this->ui.pushButton_saveFile, SIGNAL(pressed()), this, SLOT(SaveButonClick()));
	QObject::connect(this->ui.pushButton_export, SIGNAL(pressed()), this, SLOT(ExportButonClick()));


	ui.comboBox->clear();
	ui.comboBox->addItems(getOCCTShapeTypeEmnu());
	ui.comboBox->setCurrentIndex(0);


	ui.label_text->setText(u8"");
}

OcctExportClass::~OcctExportClass()
{}

void OcctExportClass::setDataShape(const TopoDS_Shape & data)
{
	this->Datashape = data;
}


void OcctExportClass::ExportButonClick() {

	this->Filepath = this->ui.lineEdit->text();
	// 开始保存模型对象
	QString comboxStr = ui.comboBox->itemText(ui.comboBox->currentIndex());
	OCCTShapeType type = str2OCCTShapeType(comboxStr);
	ui.label_text->setText(u8"数据正在导出中........");
	SaveTopoDs(this->Filepath, this->Datashape, type);
	QMessageBox::information(this, "info", u8"数据导出成功！！！");
	this->close();
}

void OcctExportClass::SaveButonClick() {
	qDebug() << u8"选择导出路径";
	QString filterStr;
	QString comboxStr = ui.comboBox->itemText(ui.comboBox->currentIndex());
	OCCTShapeType type = str2OCCTShapeType(comboxStr);
	filterStr = getOCCTShapeTypeFilterString(type);
	QString savePath = getSaveFilePath(
		nullptr,QString::fromUtf8(u8"导出路径") ,filterStr);
	this->Filepath = savePath;
	this->ui.lineEdit->setText(this->Filepath);
}

DialogBatchExport::DialogBatchExport(QWidget* parent)
{
	ui.setupUi(this);
	QObject::connect(this->ui.pushButton_saveFile, SIGNAL(pressed()), this, SLOT(SaveButonClick()));
	QObject::connect(this->ui.pushButton_export, SIGNAL(pressed()), this, SLOT(ExportButonClick()));

	ui.radioButton_Merged->setChecked(false);
	ui.comboBox->clear();
	ui.comboBox->addItems(getOCCTShapeTypeEmnu());
	ui.comboBox->setCurrentIndex(0);

	ui.label_text->setText(u8"");
	this->Datashapelist= QMap<QString, TopoDS_Shape>();
	this->Datashapelist.clear();

}

DialogBatchExport::~DialogBatchExport()
{
}

void DialogBatchExport::addDataShape(QString name, const TopoDS_Shape& data)
{
	this->Datashapelist.insert(name, data);
}

TopoDS_Shape DialogBatchExport::getMerged()
{
	std::vector< TopoDS_Shape> dsList;

	// 使用迭代器遍历 QMap 中的元素
	QMap<QString, TopoDS_Shape>::const_iterator it;
	for (it = this->Datashapelist.constBegin(); it != this->Datashapelist.constEnd(); ++it) {
		QString key = it.key();
		TopoDS_Shape value = it.value();
		dsList.push_back(value);
	}
	return MergedTopoShape(dsList);
}

void DialogBatchExport::ExportButonClick() { 
	// 分批
	QString comboxStr = ui.comboBox->itemText(ui.comboBox->currentIndex());
	OCCTShapeType type = str2OCCTShapeType(comboxStr);
	ui.label_text->setText(u8"数据正在导出中........");

	this->Filepath = this->ui.lineEdit->text();
	if (this->Filepath.isEmpty()) {
		QMessageBox::warning(this, "warning", u8"没有选中文件夹");
		return;
	}
	else {
		if (this->ui.radioButton_Split->isChecked()) { // 分批导出文件
			// 使用迭代器遍历 QMap 中的元素
			QMap<QString, TopoDS_Shape>::const_iterator it;
			for (it = this->Datashapelist.constBegin(); it != this->Datashapelist.constEnd(); ++it) {
				QString key = it.key();
				TopoDS_Shape value = it.value();
				QMessageBox::information(this, "info", u8"数据导出成功！！！");
				QString saveResultPath= QDir::cleanPath(this->Filepath +"\\" + key);
				SaveTopoDs(saveResultPath, value, type);
			}
		}
		else {
			TopoDS_Shape  Ds = this->getMerged();
			SaveTopoDs(this->Filepath, Ds, type);
		}
	}
}

void DialogBatchExport::SaveButonClick()
{

	qDebug() << u8"选择导出路径";
	if (this->ui.radioButton_Split->isChecked()) { // 分批导出文件
		// 弹出选择文件夹的对话框
		QString selectedDirectory = QFileDialog::getExistingDirectory(nullptr,
			"选择文件夹",
			".",
			QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

		// 用户点击了选择文件夹按钮
		if (!selectedDirectory.isEmpty()) {
			qDebug() << "文件夹路径是：" << selectedDirectory;

			this->Filepath = selectedDirectory;
		}
		else { 
			QMessageBox::warning(this, "warning", u8"没有选中文件夹");
			qDebug() << "取消文件夹操作。";
			this->Filepath.clear();
			return;
		}

	}
	else { // 选择合并文件
		QString filterStr;
		QString comboxStr = ui.comboBox->itemText(ui.comboBox->currentIndex());
		OCCTShapeType type = str2OCCTShapeType(comboxStr);
		filterStr = getOCCTShapeTypeFilterString(type);
		QString savePath = getSaveFilePath(
			nullptr, QString::fromUtf8(u8"合并模型导出路径"), filterStr);
		
		// 用户点击了选择文件夹按钮
		if (!savePath.isEmpty()) {
			qDebug() << "文件夹路径是：" << savePath;
			this->Filepath = savePath;
		}
		else {
			QMessageBox::warning(this, "warning", u8"没有选中文件夹");
			qDebug() << "取消文件夹操作。";
			this->Filepath.clear();
			return;
		}
		
		this->Filepath = savePath;
		this->ui.lineEdit->setText(this->Filepath);

	}

}
