/*
 * Copyright (C) 2025, 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 3 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: maomao <baijunjie@kylinos.cn>
 */


#include "security-control-config.h"
#include <function-control.h>
#include <QJsonObject>

using namespace UkuiNotification;
using namespace UkuiQuick;

static const QString CONFIG_NAME = "ukui-notification-v11";
static const QString NOTIFICATION_DISABLE_KEY = "disableAllNotifications";

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

SecurityControlConfig::SecurityControlConfig(QObject *parent) : QObject(parent)
{
    initData();
    connect(FunctionControl::instance(), &FunctionControl::functionControlChanged, this, &SecurityControlConfig::onControlConfigChanged);
    connect(FunctionControl::instance(), &FunctionControl::functionControlExit, this, &SecurityControlConfig::onControlExited);
}

void SecurityControlConfig::initData() {
    FunctionControl::instance()->addWatch(CONFIG_NAME);
    m_config = FunctionControl::instance()->functionControlObject(CONFIG_NAME).value(CONFIG_NAME).toObject();
    qDebug() << "Init SecurityCtrl config data" << m_config;
}

void SecurityControlConfig::onControlExited() {
    m_config = {};
    Q_EMIT exitControl();
}

void SecurityControlConfig::onControlConfigChanged(const QString &name) {
    if (name == CONFIG_NAME) {
        m_config = FunctionControl::instance()->functionControlObject(CONFIG_NAME).value(CONFIG_NAME).toObject();
        qDebug() << "Refresh SecurityCtrl config" << m_config;
        Q_EMIT configChanged();
    }
}

bool SecurityControlConfig::isNotificationEnable() const {
    return m_config.isEmpty() || !m_config.value(NOTIFICATION_DISABLE_KEY).toBool();
}