#include "DBusClient.h"

#include <QDebug>

//#include "ClientMethod.h"

DBusClient& DBusClient::getInstance()
{
    static DBusClient instance;
    return instance;
}

bool DBusClient::initDbusConnect()
{
    bool ret = true;
    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, AUDIO_INTERFACE,
                                              "volumeChanged",
                                              this, SLOT(volumeChangedSlots(int, int, const QDBusVariant&)))) {
        qDebug() << "Audio framwork interface error, connect " << "volumeChanged" << "failed!";
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, AUDIO_INTERFACE,
                                              "muteChanged",
                                              this, SLOT(muteChangedSlots(int, int, bool)))) {
        qDebug() << "Audio framwork interface error, connect " << "muteChanged" << "failed!";
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, AUDIO_INTERFACE,
                                              "deviceChanged",
                                              this, SLOT(deviceChangedSlots(int, const QString&, const QString&)))) {
        qDebug() << "Audio framwork interface error, connect " << "deviceChanged" << "failed!";
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, AUDIO_INTERFACE,
                                              "deviceAdjust",
                                              this, SLOT(deviceAdjustSlots(int)))) {
        qDebug() << "Audio framwork interface error, connect " << "deviceChanged" << "failed!";
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, SETTINGS_INTERFACE,
                                              "changed",
                                              this, SLOT(settingsChangedSlots(const QString&, const QDBusVariant&)))) {
        qDebug() << "Audio framwork interface error, connect " << "changed" << "failed!" ;
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, AUDIO_INTERFACE,
                                              "addStream",
                                              this, SLOT(addStreamSlots(int, int, const QString&, const QString&)))) {
        qDebug() << "Audio framwork interface error, connect " << "addStream" << "failed!" ;
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    if(!QDBusConnection::sessionBus().connect(AUDIO_OBJECT_SERVICE, AUDIO_OBJECT_PATH, AUDIO_INTERFACE,
                                              "removeStream",
                                              this, SLOT(removeStreamSlots(int)))) {
        qDebug() << "Audio framwork interface error, connect " << "removeStream" << "failed!" ;
        qDebug() << QDBusConnection::sessionBus().lastError().message();
        ret = false;
    }

    return ret;
}

void DBusClient::volumeChangedSlots(int type, int idx, const QDBusVariant& v)
{
    Q_EMIT volumeChangedSignal(type, idx, v);
}

void DBusClient::muteChangedSlots(int type, int index, bool mute)
{
    Q_EMIT muteChangedSignal(type, index, mute);
}

void DBusClient::deviceChangedSlots(int type, const QString& portName, const QString& cardName)
{
    Q_EMIT deviceChangedSignal(type, portName, cardName);
}

void DBusClient::deviceAdjustSlots(int type)
{
    Q_EMIT deviceAdjustSignal(type);
}

void DBusClient::settingsChangedSlots(const QString& key, const QDBusVariant& v)
{
    qDebug() << "DBusClient::settingsChangedSlots, key:" << key << " value: " << v.variant();
    Q_EMIT settingsChangedSignal(key, v);
}

void DBusClient::addStreamSlots(int idx, int value, const QString& iconName, const QString& descName)
{
    Q_EMIT addStreamSignal(idx, value, iconName, descName);
}

void DBusClient::removeStreamSlots(int idx)
{
    Q_EMIT removeStreamSignal(idx);
}
