#include "WidgetSettingClass.h"
#include <QFileInfo>
#include <QDir>


WidgetSettingClass::~WidgetSettingClass()
{
}

WidgetSettingClass& WidgetSettingClass::instance()
{
    static WidgetSettingClass instance;
    return instance;
}

// 实现构造函数和其他方法...
WidgetSettingClass::WidgetSettingClass(QObject* parent)
    : QObject(parent), m_settings(new QSettings(this))
{
    lastFileDialogPath = QString();

    iniPath = this->getExeDirectionApplicationPath()+u8"\\LAMPWINDData.ini";

    if (!QFileInfo(iniPath).exists()) {
        QSettings writeSettings(iniPath, QSettings::IniFormat);
        writeSettings.setValue("Application/Name", u8"风场处理软件");
        writeSettings.setValue("Application/Version", 1.0);
        writeSettings.setValue("Application/LastfilePath", this->getExeDirectionApplicationPath());
    }


    QSettings readSettings(iniPath, QSettings::IniFormat);
    QString appName = readSettings.value("Application/Name").toString();
    double version = readSettings.value("Application/Version").toDouble();
    this->lastFileDialogPath= readSettings.value("Application/LastfilePath").toString();

}

QString WidgetSettingClass::getWindTool_nc2WindDataToolPath()
{
    QString appDir = QCoreApplication::applicationDirPath();
    QDir baseDir(appDir);
    QString toolPyPath = baseDir.absoluteFilePath(u8"tools\\windTools\\ERA5ToWindDataConverter.py");
    toolPyPath = QDir::cleanPath(toolPyPath);
    return toolPyPath;
}

QString WidgetSettingClass::getWindTool_PythonEnvPath()
{
    QString appDir = QCoreApplication::applicationDirPath();
    QDir baseDir(appDir);
    QString pythonExePath = baseDir.absoluteFilePath(u8"pyEnv\\lampprocess\\python.exe");
    pythonExePath = QDir::cleanPath(pythonExePath);

    qDebug() << "Python executable path:" << pythonExePath;
    return pythonExePath;
}

QString WidgetSettingClass::getExeApplicationPath()
{
    // 先得到完整的可执行文件路径
    QString appFilePath = QCoreApplication::applicationFilePath();
    // 使用QFileInfo提取其所在的绝对路径
    QFileInfo fileInfo(appFilePath);
    return fileInfo.absolutePath(); // 返回目录路径
}

QString WidgetSettingClass::getExeDirectionApplicationPath()
{
    return QCoreApplication::applicationDirPath();
}

QString WidgetSettingClass::getWindReaddLLApplicationPath()
{
    QString appDir = QCoreApplication::applicationDirPath();
    QDir baseDir(appDir);
    QString toolPath = baseDir.absoluteFilePath(u8"WindDataOperator.dll");
    toolPath = QDir::cleanPath(toolPath);
    return toolPath;
}

QString WidgetSettingClass::getLastFileDialogPath()
{
    return this->lastFileDialogPath;
}

void WidgetSettingClass::setLastFileDialogPath(QString path)
{
    lastFileDialogPath = path;
    iniPath = this->getExeDirectionApplicationPath() + u8"\\LAMPWINDData.ini";

    if (!QFileInfo(iniPath).exists()) {
        QSettings writeSettings(iniPath, QSettings::IniFormat);
        writeSettings.setValue("Application/LastfilePath", lastFileDialogPath);
    }
    else {
        QSettings writeSettings(iniPath, QSettings::IniFormat);
		writeSettings.setValue("Application/LastfilePath", lastFileDialogPath);
    }


}

QString WidgetSettingClass::getTempFolder()
{
    QString tempPath2 = QDir::tempPath();
    qDebug() << u8"系统临时文件夹路径 (QDir): " << tempPath2;
    QString tempfolderpath= tempPath2 + "\\lampwind";

    QDir dir;

    // 首先检查路径是否已经存在
    if (dir.exists(tempfolderpath)) {
        qDebug() << "目录已存在：" << tempfolderpath;
    }
    else if (dir.mkpath(tempfolderpath)) {
        qDebug() << "目录创建成功：" << tempfolderpath;
    }
    else {
        qDebug() << "目录创建失败：" << tempfolderpath;
    }

    return tempfolderpath;
}
