#include "dialog.h"

#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QFile>
#include <QTimer>
#include <QDir>
#include <QCoreApplication>

#include "../agent/util/config.h"

static inline  QString agentDataPath()
{
    auto oldName = QCoreApplication::applicationName();
    QCoreApplication::setOrganizationName("deepin");
    QCoreApplication::setApplicationName("deepin-legalization-agent");

#ifdef Q_OS_LINUX
    QString appConfigPath = "/var/lib/deepin/" + QCoreApplication::applicationName();
#else
    QString appConfigPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
#endif
    QDir(appConfigPath).mkpath(".");
    QCoreApplication::setApplicationName(oldName);
    return appConfigPath;
}

static QString clientDataPath()
{
    return QString("%1/client.json").arg(agentDataPath());
}

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("正版化系统检查配置修复工具");
    setMinimumSize(300, 100);
    auto layout = new QVBoxLayout(this);
    auto hint = new QLabel(this);
    auto button = new QPushButton(this);

    layout->addWidget(hint, 0, Qt::AlignCenter);
    layout->addWidget(button, 0, Qt::AlignCenter);

    hint->setText("点击开始修复配置文件");
    button->setText("开始修复");

    connect(button, &QPushButton::clicked,
    this, [ = ]() {
        button->setDisabled(true);
        hint->setText("正在处理配置文件中，请稍候……");
        QStringList configPaths = {
            "C:/Windows/SysWOW64/config/systemprofile/AppData/Roaming/deepin/deepin-legalization-agent/client.json",
            "C:/Windows/System32/config/systemprofile/AppData/Roaming/deepin/deepin-legalization-agent/client.json",
        };

        configPaths.append(clientDataPath());

        for (auto path : configPaths) {
            QFile configFile(path);
            configFile.remove();
//            hint->setText("删除" + path);
        }
//        hint->setText("正在处理配置文件中，请稍候……");

        QTimer::singleShot(3000, [ = ]() {
            hint->setText("已修复配置文件");
            button->setDisabled(false);
        });
    });
}

Dialog::~Dialog()
{

}
