#include "config.h"

#include <QDebug>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QDir>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>

const int TimeHour = 1000 * 60 * 60;

class ConfigData
{
public:
    QString areacode = "420923";
    QString baseURL = "https://status.deepin.com";

    QString infoPostAPI = "/api/v2/legalization/machines";
    QString logPostAPI = "/api/v2/upload/files";
    QString softwareAPI = "/api/v2/update/softwares";
    QString versionAPI = "/api/v2/update/apps";
    QString multivariateAPI = "/api/v2/update/multivariate";

    bool autoUpgrade = true;
    bool delayCommit = true;

    int autoUpgradeInterval = TimeHour * 24 * 2;
    int autoCommitInterval = TimeHour * 24;
    int autoMultivariateInterval = TimeHour * 1;
    QList<Organization> organizations;
};

static auto sData = ConfigData();

static inline QString agentConfigPath()
{
    return QString("%1/agent").arg(qApp->applicationDirPath());
}

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

static inline  QString agentCachePath()
{
#ifdef Q_OS_LINUX
    QString appConfigPath = "/var/cache/deepin/" + QCoreApplication::applicationName();
#else
    QString appConfigPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
#endif
    QDir(appConfigPath).mkpath(".");
    return appConfigPath;
}

Config::Config(QObject *parent) : QObject(parent)
{
}

void Config::load()
{
    QFile configFile(ConfigPath());
    if (!configFile.open(QIODevice::ReadOnly)) {
        qCritical() << "can not open config file" << configFile.fileName();
    }
    formJson(configFile.readAll());
    configFile.close();
}

QString Config::InterSoftwareFilterPath()
{
    return QString("%1/%2").arg(agentConfigPath()).arg("software.json");
}

QString Config::SoftwareFilterPath()
{
    return QString("%1/software.json").arg(agentDataPath());
}

QString Config::HistoryPath()
{
    return QString("%1/histroy.json").arg(agentDataPath());
}

QString Config::ConfigPath()
{
    return QString("%1/agent/config.json").arg(qApp->applicationDirPath());
}

QString Config::LogPath()
{
    return QString("%1/%2.log").arg(agentCachePath()).arg(qApp->applicationName());
}

QString Config::ClientDataPath()
{
    return QString("%1/client.json").arg(agentDataPath());
}

QString Config::UserDataPath()
{
    return QString("%1/data.json").arg(agentDataPath());
}

QString Config::UserDataPathLessThanV_1_2()
{
    QString appConfigPath = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation).at(0);
    QDir(appConfigPath).mkpath(appConfigPath);
    return QString("%1/form.json").arg(appConfigPath);
}

QString Config::UserDataBackupPathLessThanV_1_2()
{
    return QString("%1/data_v1.0.json").arg(agentDataPath());
}

void Config::formJson(const QByteArray &data)
{
    auto cfgObj = QJsonDocument::fromJson(data).object();
    qDebug() << cfgObj;
    if (!cfgObj.value("areacode").toString().isEmpty()) {
        sData.areacode = cfgObj.value("areacode").toString();
    }
    if (!cfgObj.value("base_url").toString().isEmpty()) {
        sData.baseURL = cfgObj.value("base_url").toString();
    }
    if (!cfgObj.value("info_post_api").toString().isEmpty()) {
        sData.infoPostAPI = cfgObj.value("info_post_api").toString();
    }
    if (!cfgObj.value("log_post_api").toString().isEmpty()) {
        sData.logPostAPI = cfgObj.value("log_post_api").toString();
    }
    if (!cfgObj.value("version_api").toString().isEmpty()) {
        sData.versionAPI = cfgObj.value("version_api").toString();
    }
    if (!cfgObj.value("software_api").toString().isEmpty()) {
        sData.softwareAPI = cfgObj.value("software_api").toString();
    }
    if (!cfgObj.value("multivariate_api").toString().isEmpty()) {
        sData.multivariateAPI = cfgObj.value("multivariate_api").toString();
    }
    if (cfgObj.contains("auto_upgrade")) {
        sData.autoUpgrade = cfgObj.value("auto_upgrade").toBool();
    }
    if (cfgObj.contains("delay_commit")) {
        sData.delayCommit = cfgObj.value("delay_commit").toBool();
    }
    if (cfgObj.value("auto_upgrade_interval").toInt() > 1000 * 60 * 5) {
        sData.autoUpgradeInterval = cfgObj.value("auto_upgrade_interval").toInt();
    }
    if (cfgObj.value("auto_commit_interval").toInt() > 1000 * 60 * 5) {
        sData.autoCommitInterval = cfgObj.value("auto_commit_interval").toInt();
    }
    if (cfgObj.value("auto_multivariate_interval").toInt() > 1000 * 60 * 5) {
        sData.autoMultivariateInterval =  cfgObj.value("auto_multivariate_interval").toInt();
    }

    sData.organizations.clear();
    for (auto orgVal : cfgObj.value("organizations").toArray()) {
        auto orgObj = orgVal.toObject();
        Organization org;
        org.id = orgObj.value("orgcode").toString();
        org.name = orgObj.value("name").toString();
        sData.organizations.push_back(org);
    }
}

QString Config::logPostURL()
{
    return sData.baseURL + sData.logPostAPI;
}

QString Config::infoPostURL()
{
    return sData.baseURL + sData.infoPostAPI;
}

QString Config::versionURL()
{
    return sData.baseURL + sData.versionAPI;
}

QString Config::multivariateURL()
{
    return sData.baseURL +  sData.multivariateAPI;
}

QString Config::softwareFilterURL()
{
    return sData.baseURL +  sData.softwareAPI;
}

QString Config::areacode()
{
    return sData.areacode;
}

bool Config::autoUpgrade()
{
    return sData.autoUpgrade;
}

bool Config::delayCommit()
{
    return  sData.delayCommit;
}

int Config::autoUpgradeInterval()
{
    return sData.autoUpgradeInterval;
}

int Config::autoCommitInterval()
{
    return sData.autoCommitInterval;
}

int Config::autoMultivariateInterval()
{
    return sData.autoMultivariateInterval;
}

QList<Organization> Config::organizations()
{
    return  sData.organizations;
}
