#include "dialog.h"
#include "qlog.h"
#include "ui_dialog.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QMutex>
#include <QMutexLocker>

Dialog::Dialog(QString desktopName) :
    m_desktopName(desktopName),
    ui(new Ui::Dialog)
{
    qInstallMessageHandler(myMessageOutput);
    ui->setupUi(this);

    QDir envDir("/");
    envDir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
    QStringList filter;
    filter << "fs.old-*" << ".fs.old-*";
    QStringList entries = envDir.entryList(filter);
    if (!entries.isEmpty())
    {
        if (entries.contains("fs.old-u16") || entries.contains(".fs.old-u16"))
        {
            ui->comboBox->addItem("UbuntuKylin16.04");
        }
        if (entries.contains("fs.old-u18") || entries.contains(".fs.old-u18"))
        {
            ui->comboBox->addItem("UbuntuKylin18.04");
        }
        if (entries.contains("fs.old-u20") || entries.contains(".fs.old-u20"))
        {
            ui->comboBox->addItem("UbuntuKylin20.04");
        }
        if (entries.contains("fs.old-u22") || entries.contains(".fs.old-u22"))
        {
            ui->comboBox->addItem("UbuntuKylin22.04");
        }
        if (entries.contains("fs.old-ok1") || entries.contains(".fs.old-ok1"))
        {
            ui->comboBox->addItem("openKylin1.0");
        }
        if (entries.contains("fs.old-ok2") || entries.contains(".fs.old-ok2"))
        {
            ui->comboBox->addItem("openKylin2.0");
        }
        if (entries.contains("fs.old-v4") || entries.contains(".fs.old-v4"))
        {
            ui->comboBox->addItem("V4");
        }
        if (entries.contains("fs.old-v10") || entries.contains(".fs.old-v10"))
        {
            ui->comboBox->addItem("V10");
        }
        if (entries.contains("fs.old-v10sp1") || entries.contains(".fs.old-v10sp1"))
        {
            ui->comboBox->addItem("V10SP1");
        }
    }
    else
    {
        ui->comboBox->addItem("基础环境目录遭到破坏");
    }

    QString homeDir = QDir::homePath();
    QString iniDir = homeDir + "/.config/compatible-layer/";
    qDebug() << "DEBUG - 配置文件所在目录:" << iniDir;
    if (!QDir(iniDir).exists())
    {
        qDebug() << "WARNING - 配置文件目录不存在，正在创建";
        QDir().mkpath(iniDir);
    }
    QString iniFile = iniDir + "yorNcompatbleMode.ini";
    if (!QFile(iniFile).exists())
    {
        qDebug() << "WARNING - 配置文件不存在，正在创建";
        QFile(iniFile).open(QIODevice::ReadWrite | QIODevice::Text);
        QFile(iniFile).close();
    }

    configIniWrite = new QSettings(iniFile, QSettings::IniFormat);
    configIniWrite->setIniCodec("UTF-8");
    QString key = "Gate/" + desktopName;
    QString res = configIniWrite->value(key).toString();
    if (res == "yes")
    {
        ui->checkBox->setChecked(true);
    }
    else
    {
        ui->checkBox->setChecked(false);
    }
    m_env = ui->comboBox->currentText();
    connect(ui->comboBox, &QComboBox::currentTextChanged, [this]()
    {
        m_env = ui->comboBox->currentText();
    });
    QString envKey = "Env/" + desktopName;
    QString preferText = configIniWrite->value(envKey).toString();
    if (!preferText.isEmpty())
    {
        ui->comboBox->setCurrentText(preferText);
    }
}

Dialog::~Dialog()
{
    delete ui;
    if (configIniWrite)
    {
        configIniWrite = nullptr;
        delete configIniWrite;
    }
}

QString Dialog::getItem()
{
    qDebug() << "DEBUG - 用户选择的兼容环境为: " << m_env;
    return m_env;
}

void Dialog::on_checkBox_stateChanged(int arg1)
{
    m_flag = arg1;
}
bool Dialog::storePrefer()
{
    QMutex mutex;
    QMutexLocker locker(&mutex);
    QString key = "Gate/" + m_desktopName;
    QString envKey = "Env/" + m_desktopName;
    if (m_flag == 2)
    {
        configIniWrite->setValue(key, "yes");
        configIniWrite->setValue(envKey, m_env);
        return true;
    }
    //    取消选中
    else if (m_flag == 0)
    {
        configIniWrite->setValue(key, "no");
        configIniWrite->setValue(envKey, "");
        return false;
    }
    qDebug() << "ERROR - 选项框状态有误: " << m_flag;
    return false;
}
