#include "icontheme.h"

#include <KIconTheme>
#include <QGSettings>
#include <QIcon>

#include <QDebug>

namespace UKUI {

static IconTheme* g_icon_theme;

IconTheme::IconTheme()
{
    initConnect();
}

IconTheme* IconTheme::getInstance()
{
    if (!g_icon_theme)
        g_icon_theme = new IconTheme();
    return g_icon_theme;
}

QString IconTheme::getPathFromName(QString iconName, uint iconSize)
{
    QGSettings iconStyle("org.ukui.style");
    QString iconThemeName;

    if (iconStyle.keys().contains("iconThemeName")) {
        iconThemeName = iconStyle.get("iconThemeName").toString();
    }
    KIconTheme iconInfo(iconThemeName);
    return iconInfo.iconPathByName(iconName, iconSize, KIconLoader::MatchBest);
}

void IconTheme::initConnect()
{
    if (!QGSettings::isSchemaInstalled("org.ukui.style")) {
        qWarning() << __FUNCTION__ << __FILE__ << "can not find schema org.ukui.style";
        return;
    }
    QGSettings* iconThemeSettings = new QGSettings("org.ukui.style", "/org/ukui/style/");
    connect(iconThemeSettings, &QGSettings::changed, [&](const QString& value) {
        if (value == "iconThemeName") {
            emit iconThemeChanged();
        }
    });
}

} // namespace UKUI
