/*
 * Copyright 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with thi1s program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "whitelistmanager.h"
#include <QStandardPaths>
#include <QDir>
#include <QFile>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
#include "utils/misc.h"

WhiteListManager::WhiteListManager(QObject *parent)
    : QObject(parent)
    , m_jsonFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) +
               "/.config/kylin-app-manager/whitelists")
{
    initJsonFile();
}

bool WhiteListManager::addToWhiteList(const QString &desktopFile, const QString &option)
{
    if (!m_jsonObj.contains(option)) {
        qWarning() << "unkwon option: " << option << m_jsonObj.keys();
        return false;
    }
    QJsonArray arr = m_jsonObj[option].toArray();
    if (arr.contains(desktopFile)) {
        return true;
    }
    arr.append(desktopFile);

    m_jsonObj[option] = arr;
    utils::Misc::writeJsonFoFile(m_jsonObj, m_jsonFile);
    return true;
}

bool WhiteListManager::removeFromWhiteList(const QString &desktopFile, const QString &option)
{
    qDebug() << __FUNCTION__ << m_jsonObj[option];
    if (!m_jsonObj.contains(option)) {
        qWarning() << "unkwon option: " << option << m_jsonObj.keys();
        return false;
    }
    QJsonArray arr = m_jsonObj[option].toArray();
    if (!arr.contains(desktopFile)) {
        return true;
    }

    for (int i=0; i<arr.size(); ++i) {
        if (arr[i] == desktopFile) {
            arr.removeAt(i);
            break;
        }
    }
    m_jsonObj[option] = arr;
    utils::Misc::writeJsonFoFile(m_jsonObj, m_jsonFile);
    return true;
}

bool WhiteListManager::isExists(const QString &desktopFile, const QString &option)
{
    qDebug() << "---WhiteListManager::isExists---" << desktopFile << "-" << option;
    m_jsonObj = utils::Misc::readJsonFromFile(m_jsonFile);
    qDebug() << "---WhiteListManager::isExists---" << m_jsonObj[option];
    if (m_jsonObj[option].toArray().contains(desktopFile)) {
        return true;
    }
    return false;
}

QStringList WhiteListManager::whiteListsOfApp(const QString &desktopFile)
{
    QStringList options;
    for (auto &option : m_jsonObj.keys()) {
        if (m_jsonObj[option].toArray().contains(desktopFile)) {
            options.append(option);
        }
    }
    return options;
}

void WhiteListManager::initJsonFile()
{
    QString filePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/kylin-app-manager/";
    QString jsonFileName = filePath + "/whitelists";
    if (QFile::exists(jsonFileName)) {
        m_jsonObj = utils::Misc::readJsonFromFile(jsonFileName);
        return;
    }

    if (!utils::Misc::ensurePathExists(filePath) ||
            !utils::Misc::ensureFileExists(jsonFileName)) {
        return;
    }

    m_jsonObj["Notification"] = QJsonArray();
    m_jsonObj["PowerSaving"] = QJsonArray();
    m_jsonObj["AppManager"] = QJsonArray();

    // 初始化whitelist列表，添加默认的应用，维护一个m_desktopList列表
    QJsonArray appManagerArray;
    m_desktopList << "eye-protection-center.desktop" << "mdm-acpanel.desktop"
                  << "sogouImeService.desktop" << "ukui-search-menu.desktop";
    for (int i=0; i<m_desktopList.size(); ++i) {
        appManagerArray.append(m_desktopList[i]);
    }
    m_jsonObj["AppManager"] = appManagerArray;
    utils::Misc::writeJsonFoFile(m_jsonObj, m_jsonFile);
}
