#include "keyboardinterface.h"

#include <QDebug>

KeyBoardInterface::KeyBoardInterface()
{
    keyboardGsettings = UniversalInterface::self()->ukuiKeyBoardGsettings();
    ukccOsdGsettings  = UniversalInterface::self()->ukccOsdGsettings();

    initConnect();
}

KeyBoardInterface::~KeyBoardInterface()
{

}

void KeyBoardInterface::initConnect()
{
    connect(keyboardGsettings, &QGSettings::changed, this, [=](QString key) {
        if (key == KEYREPEAT_KEY) {
            Q_EMIT changed("repeat");
        } else if (key == DELAY_KEY) {
            Q_EMIT changed("delay");
        } else if (key == SPEED_KEY) {
            Q_EMIT changed("rate");
        }
    });

    connect(ukccOsdGsettings, &QGSettings::changed, this, [=](QString key) {
        if (key == KEYTIPS_KEY) {
            Q_EMIT changed("show-lock-tip");
        }
    });
}

const bool KeyBoardInterface::getKeyRepeat()
{
    if (keyboardGsettings->keys().contains(KEYREPEAT_KEY)) {
        return keyboardGsettings->get(KEYREPEAT_KEY).toBool();
    } else {
        qCritical() << "keyboardGsettings not contains the key: " << KEYREPEAT_KEY;
    }
    return bool();
}

const int KeyBoardInterface::getDelay()
{
    if (keyboardGsettings->keys().contains(DELAY_KEY)) {
        return keyboardGsettings->get(DELAY_KEY).toInt();
    } else {
        qCritical() << "keyboardGsettings not contains the key: " << DELAY_KEY;
    }
    return int();
}

const int KeyBoardInterface::getRate()
{
    if (keyboardGsettings->keys().contains(SPEED_KEY)) {
        return keyboardGsettings->get(SPEED_KEY).toInt();
    } else {
        qCritical() << "keyboardGsettings not contains the key: " << SPEED_KEY;
    }
    return int();
}

const bool KeyBoardInterface::getShowLockTip()
{
    if (ukccOsdGsettings->keys().contains(KEYTIPS_KEY)) {
        return ukccOsdGsettings->get("show-lock-tip").toBool();
    } else {
        qCritical() << "ukccOsdGsettings not contains the key: " << KEYTIPS_KEY;
    }
    return bool();
}

void KeyBoardInterface::setKeyRepeat(const bool checked)
{
    if (keyboardGsettings->keys().contains(KEYREPEAT_KEY)) {
        keyboardGsettings->set(KEYREPEAT_KEY, checked);
    } else {
        qCritical() << "keyboardGsettings not contains the key: " << KEYREPEAT_KEY;
    }
}

void KeyBoardInterface::setDelay(const int value)
{
    if (keyboardGsettings->keys().contains(DELAY_KEY)) {
        keyboardGsettings->set(DELAY_KEY, value);
    } else {
        qCritical() << "keyboardGsettings not contains the key: " << DELAY_KEY;
    }
}

void KeyBoardInterface::setSpeed(const int value)
{
    if (keyboardGsettings->keys().contains(SPEED_KEY)) {
        keyboardGsettings->set(SPEED_KEY, value);
    } else {
        qCritical() << "keyboardGsettings not contains the key: " << SPEED_KEY;
    }
}

void KeyBoardInterface::setKeyTips(const bool checked)
{
    if (ukccOsdGsettings->keys().contains(KEYTIPS_KEY)) {
        ukccOsdGsettings->set("show-lock-tip", checked);
    } else {
        qCritical() << "ukccOsdGsettings not contains the key: " << KEYTIPS_KEY;
    }
}
