#include <QVariant>
#include <QDebug>
#include <QFontDatabase>
#include <QFont>

#include "gsettings_monitor.h"

GsettingsMonitor::GsettingsMonitor()
{
    m_ukuiStyle = new QGSettings(UKUI_STYLE);
    if (gsettingsCheck()) {
        initGsettings();
    }
}

GsettingsMonitor::~GsettingsMonitor()
{

}

bool GsettingsMonitor::gsettingsCheck()
{
    if(!QGSettings::isSchemaInstalled(UKUI_STYLE)) {
        qCritical() << QString("gsettings Error: %1 not found!").arg(UKUI_STYLE);
        return false;
    }
    return true;
}


void GsettingsMonitor::initGsettings()
{
    connect(m_ukuiStyle, &QGSettings::changed, this, [=] (const QString &key) {
        if (key == UKUI_MENU_TRANSPARENCY) {
            getTransparency();
        }
        else if (key == UKUI_STYLE_NAME) {
            getStyleStatus();
        }
    });
    getTransparency();
    getStyleStatus();

    return ;
}

void GsettingsMonitor::getTransparency()
{
    if (!gsettingsCheck()) {
        return;
    }

    int tmpMenuTrans = m_ukuiStyle->get(UKUI_MENU_TRANSPARENCY).toInt();
    if (tmpMenuTrans != m_menuTrans) {
        if (m_menuTrans != -1) {
            emit transparencyUpdate(tmpMenuTrans);
        }
        m_menuTrans = tmpMenuTrans;
    }

    return ;
}

void GsettingsMonitor::getStyleStatus()
{
    if (!gsettingsCheck()) {
        return;
    }

    StyleStatus tmpStyleStatus = styleNameConvert(m_ukuiStyle->get(UKUI_STYLE_NAME).toString());
    if (tmpStyleStatus != m_styleStatus) {
        if (m_styleStatus != NONE) {
            emit styleStatusUpdate(tmpStyleStatus);
        }
        m_styleStatus = tmpStyleStatus;
    }
    return ;
}

GsettingsMonitor::StyleStatus GsettingsMonitor::styleNameConvert(const QString &styleName)
{
    if (styleName == "ukui-default"
     || styleName == "ukui-white"
     || styleName == "ukui-light"
     || styleName == "ukui") {
                
        return LIGHT;
    }
    
    if (styleName == "ukui-dark" || styleName == "ukui-black") {
        return DARK;
    }

    return NONE;
}

int GsettingsMonitor::transparency() const
{
    return m_menuTrans;
}

GsettingsMonitor::StyleStatus GsettingsMonitor::styleStatus() const
{
    return m_styleStatus;
}