/*
 * UKUI Framework
 *
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: Yue Lan <lanyue@kylinos.cn>
 *
 */

#include "effect.h"

#include <QGSettings/QGSettings>

#undef signals
#include <gio/gio.h>

bool g_effectServiceAvailable = true;

Effect::Effect(QObject *parent)
    : QObject{parent}
{
    if (QGSettings::isSchemaInstalled("org.ukui.control-center.personalise")) {
        auto settings = new QGSettings("org.ukui.control-center.personalise");

        qreal transparency = settings->get("transparency").toReal();
        WindowOpacity = transparency * 100;

        WindowBlur = settings->get("effect").toBool();

        connect(settings, &QGSettings::changed, this, [=](const QString &key){
            if (key == "transparency") {
                qreal transparency = settings->get("transparency").toReal();
                WindowOpacity = transparency * 100;
                emit WindowOpacityChanged();
            } else if (key == "effect") {
                WindowBlur = settings->get("effect").toBool();
                emit WindowBlurChanged();
            }
        });
    } else {
        // 特效服务不可用
        g_effectServiceAvailable = false;
    }
}

Effect::~Effect()
{

}

int Effect::GetWindowOpacity() const
{
    if (!g_effectServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Effect service is not available"));
        }
    }
    return WindowOpacity;
}

bool Effect::SetWindowOpacity(int opacity)
{
    if (!g_effectServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Effect service is not available"));
        }
        return false;
    }

    if (WindowOpacity == opacity)
        return false;

    if (WindowOpacity < 0 || WindowOpacity > 100) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidArgs, tr("Invalid opacity, must be in range [0, 100]"));
        }
        return false;
    }

    qreal transparency;
    transparency = opacity/100.0;
    g_autoptr (GSettings) scopedSettings = g_settings_new("org.ukui.control-center.personalise");
    if (g_settings_set_double(scopedSettings, "transparency", transparency)) {
        return true;
    }

    return false;
}

bool Effect::GetWindowBlur() const
{
    if (!g_effectServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Effect service is not available"));
        }
    }
    return WindowBlur;
}

bool Effect::SetWindowBlur(bool blur)
{
    if (!g_effectServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Effect service is not available"));
        }
        return false;
    }

    if (WindowBlur == blur)
        return false;

    g_autoptr (GSettings) scopedSettings = g_settings_new("org.ukui.control-center.personalise");
    if (g_settings_set_boolean(scopedSettings, "effect", true)) {
        return true;
    }

    return false;
}
