#include "config.h"

Environment envPC = Environment::NOMAL;
//全局变量，判定机型架构
Config::Config(QObject *parent, QString name)
    :QObject(parent)
    ,setting_name(name)
{
    if (QGSettings::isSchemaInstalled(setting_name.toUtf8())) {
        gsetting = new QGSettings(setting_name.toUtf8());
    }
}

Config::~Config()
{
    delete gsetting;
}

void Config::SendNotifyMessage(QString message)
{
    QDBusInterface iface("org.freedesktop.Notifications",
                             "/org/freedesktop/Notifications",
                             "org.freedesktop.Notifications",
                             QDBusConnection::sessionBus());
    QList<QVariant> args;
    args<<tr("Bluetooth")
        <<((unsigned int) 0)
        <<"bluetooth"
        <<tr("Bluetooth Message") //显示的是什么类型的信息
        <<message //显示的具体信息
        <<QStringList()
        <<QVariantMap()
        <<(int)-1;
    QDBusMessage msg = iface.callWithArgumentList(QDBus::AutoDetect,"Notify",args);
    qDebug() << Q_FUNC_INFO << msg.errorMessage();
}

const QPixmap Config::loadSvg(const QPixmap &source, const PixmapColor &cgColor)
{
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                switch (cgColor) {
                case PixmapColor::WHITE:
                    color.setRed(255);
                    color.setGreen(255);
                    color.setBlue(255);
                    img.setPixelColor(x, y, color);
                    break;
                case PixmapColor::BLACK:
                    color.setRed(0);
                    color.setGreen(0);
                    color.setBlue(0);
                    img.setPixelColor(x, y, color);
                    break;
                case PixmapColor::GRAY:
                    color.setRed(152);
                    color.setGreen(163);
                    color.setBlue(164);
                    img.setPixelColor(x, y, color);
                    break;
                case PixmapColor::BLUE:
                    color.setRed(61);
                    color.setGreen(107);
                    color.setBlue(229);
                    img.setPixelColor(x, y, color);
                    break;
                default:
                    return source;
                    break;
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

void Config::setKdkGeometry(QWindow *windowHandle, int width, int height, bool center) {
    QDBusInterface iface("org.ukui.panel",
                         "/panel/position",
                         "org.ukui.panel",
                         QDBusConnection::sessionBus());
    if (iface.isValid()) {
        QDBusReply<QVariantList> reply=iface.call("GetPrimaryScreenGeometry");
        if (center)
            kdk::WindowManager::setGeometry(windowHandle,
                                            QRect((reply.value().at(2).toInt()-width)/2,
                                                  (reply.value().at(3).toInt()-height)/2,
                                                  width, height));
        else
            kdk::WindowManager::setGeometry(windowHandle,
                                            QRect(reply.value().at(0).toInt()+reply.value().at(2).toInt()-width-4,
                                                  reply.value().at(1).toInt()+reply.value().at(3).toInt()-height-4,
                                                  width, height));
    }
}

void Config::OpenBluetoothSettings()
{
    QProcess *process = new QProcess();
    QString cmd = "ukui-control-center";
    QStringList arg;
    qDebug() << Q_FUNC_INFO;
    arg << "--bluetooth";
    process->start(cmd,arg);
}
