/*
 * 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 "font.h"

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

bool g_fontServiceAvailable = true;

Font::Font(QObject *parent)
    : QObject{parent}
{
    if (QGSettings::isSchemaInstalled("org.ukui.style")) {
        auto settings = new QGSettings("org.ukui.style");
        settings->setParent(this);

        FontFamily = settings->get("systemFont").toString();
        FontSize = settings->get("systemFontSize").toDouble();

        connect(settings, &QGSettings::changed, this, [=](const QString &key){
            qDebug()<<key;
            if (key == "systemFont") {
                FontFamily = settings->get("systemFont").toString();
                emit FontFamilyChanged();
            } else if (key == "systemFontSize") {
                FontSize = settings->get("systemFontSize").toDouble();
                emit FontSizeChanged();
            }
        });
    } else {
        // 字体服务不可用
        g_fontServiceAvailable = false;
    }
}

Font::~Font()
{

}

const QString Font::GetFontFamily() const
{
    if (!g_fontServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Font service is not available"));
        }
    }
    return FontFamily;
}

bool Font::SetFontFamily(const QString &family)
{
    if (!g_fontServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Font service is not available"));
        }
        return false;
    }

    if (FontFamily == family)
        return false;

    g_autoptr (GSettings) scopedSettings = g_settings_new("org.ukui.style");
    if (g_settings_set_string(scopedSettings, "system-font", family.toLocal8Bit().constData())) {
        return true;
    }

    return false;
}

double Font::GetFontSize() const
{
    if (!g_fontServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Font service is not available"));
        }
    }
    return FontSize;
}

bool Font::SetFontSize(double size)
{
    if (size < 10 || size > 15) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidArgs, tr("Font size are InvalidArgs"));
        }
        return false;
    }

    if (!g_fontServiceAvailable) {
        if (calledFromDBus()) {
            sendErrorReply(QDBusError::InvalidService, tr("Font service is not available"));
        }
        return false;
    }

    if (FontSize == size)
        return false;

    g_autoptr (GSettings) scopedSettings = g_settings_new("org.ukui.style");
    QString sizeString = QString::number(size);
    if (g_settings_set_string(scopedSettings, "system-font-size", sizeString.toLocal8Bit().constData())) {
        return true;
    }

    return false;
}

bool Font::SetFontSizeWithString(QString size)
{
    if (!m_fontSizeMap.contains(size)) {
         sendErrorReply(QDBusError::InvalidArgs, tr("size %1 are invalid args").arg(size));
        return false;
    }

    g_autoptr (GSettings) scopedSettings = g_settings_new("org.ukui.style");
    QString sizeString = QString::number(m_fontSizeMap[size]);
    if (g_settings_set_string(scopedSettings, "system-font-size", sizeString.toLocal8Bit().constData())) {
        return true;
    }

    return false;
}

bool Font::SetFontSizeWithType(QString size)
{
    if (!m_fontSizeMap.contains(size)) {
         sendErrorReply(QDBusError::InvalidArgs, tr("size %1 are invalid args").arg(size));
        return false;
    }

    g_autoptr (GSettings) scopedSettings = g_settings_new("org.ukui.style");
    QString sizeString = QString::number(m_fontSizeMap[size]);
    if (g_settings_set_string(scopedSettings, "system-font-size", sizeString.toLocal8Bit().constData())) {
        return true;
    }
    return false;
}

QString Font::GetFontSizeWithType()
{
//    FontSize = settings->get("systemFontSize").toDouble();
    if (!m_fontSizeMap.values().contains(FontSize)) {
        sendErrorReply(QDBusError::InvalidArgs, tr("size %1 are invalid type").arg(FontSize));
        return "";
    }

    qDebug()<<FontSize;
    return m_fontSizeMap.key(FontSize);
}
