#include "WindConverTools.h"
#include "WidgetSettingClass.h"
#include <QFileInfo>
#include <QProcess>

nc2windataTool::nc2windataTool(QString Inncfilepath, QString InwindBinPath, LAMPDataManagerDialog* IndatamanagerDiglog)
{
	this->ncfilepath = Inncfilepath;
	this->windBinPath = InwindBinPath;
	this->datamanagerDiglog = IndatamanagerDiglog;

}

nc2windataTool::~nc2windataTool()
{
}

int32_t nc2windataTool::Excute()
{
	WidgetSettingClass& config = WidgetSettingClass::instance();
	QString pythonPath = config.getWindTool_PythonEnvPath();
	QString nc2windToolPath = config.getWindTool_nc2WindDataToolPath();
	QString dllPath = config.getWindReaddLLApplicationPath();



	// 2. 检查文件是否存在
	if (!QFileInfo(pythonPath).exists()) {
		qDebug() << u8"错误：Python解释器路径不存在 -" << pythonPath;
		return -1;
	}
	if (!QFileInfo(nc2windToolPath).exists()) {
		qDebug() << u8"错误：Python脚本路径不存在 -" << nc2windToolPath;
		return -1;
	}
	if (!QFileInfo(ncfilepath).exists()) {
		qDebug() << u8"错误：参数文件路径不存在 -" << ncfilepath;
		return -1;
	}
	if (!QFileInfo(dllPath).exists()) {
		qDebug() << u8"错误：程序库文件路径不存在 -" << dllPath;
		return -1;
	}
	// 构建参数列表
	QStringList arguments;
	arguments 
		//<< "/c" 
		//<<  pythonPath
		<< nc2windToolPath // Python脚本路径
		<< "-i" << ncfilepath
		<< "-o" << windBinPath
		<< "-d" << dllPath;
	// 连接finished信号到成员槽函数
	//connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
	//	this, &nc2windataTool::on_process_finished);
	m_process= new QProcess(this);
	//m_process->execute("cmd.exe", arguments);
	m_process->start(pythonPath, arguments);
	if (!m_process->waitForStarted(3000)) { // 等待进程启动，超时3秒
		qDebug() << u8"启动失败：" << m_process->errorString();
	}
	else {
		qDebug() << u8"进程已启动...";
	}

	return 0;
}

void nc2windataTool::on_process_finished(int exitCode, QProcess::ExitStatus exitStatus)
{
	if (!QFileInfo(windBinPath).exists()) {
		qDebug() << u8"错误：结果文件不存在 -" << windBinPath;
		return ;
	}

	// 数据加载
	// LAMPDataManagerDialog* IndatamanagerDiglog；


}
