#include "dbusmanager.h"

//! 任务栏
Q_GLOBAL_STATIC(PanelDbus, paneldbus)

PanelDbus::PanelDbus()
{
    m_panelInterface = new  QDBusInterface( PANEL_DBUS_SERVICE,
                                            PANEL_DBUS_PATH,
                                            PANEL_DBUS_INTERFACE,
                                            QDBusConnection::sessionBus());
    if (m_panelInterface == nullptr) {
        qWarning()<< "PanelDbus init error";
    }
}

PanelDbus::~PanelDbus()
{
    if (m_panelInterface != nullptr) {
        delete  m_panelInterface;
        m_panelInterface = nullptr;
    }
}

PanelDbus *PanelDbus::getInstance()
{
    return paneldbus();
}

int PanelDbus::getPanelHeight()
{
    if (m_panelInterface != nullptr) {
        QDBusMessage msg = m_panelInterface->call("GetPanelSize", QVariant("Hight"));
        return msg.arguments().at(0).toInt();
    } else {
        return 46;
    }
}

int PanelDbus::getPanelPosition()
{
    if (m_panelInterface != nullptr) {
        QDBusMessage msg = m_panelInterface->call("GetPanelPosition", QVariant("Site"));
        return msg.arguments().at(0).toInt();
    } else {
        return 0;
    }
}


//! 模式切换接口
Q_GLOBAL_STATIC(StatusManagerDbus, statusmanagerdbus)

StatusManagerDbus::StatusManagerDbus()
{
    m_statusManagerface = new  QDBusInterface( STATUS_MANAGER_DBUS_SERVICE,
                                               STATUS_MANAGER_DBUS_PATH,
                                               STATUS_MANAGER_DBUS_INTERFACE,
                                               QDBusConnection::sessionBus());

    if (m_statusManagerface == nullptr) {
        qWarning()<< "StatusManagerDbus init error";
    }
}

StatusManagerDbus::~StatusManagerDbus()
{
    if (m_statusManagerface != nullptr) {
        delete  m_statusManagerface;
        m_statusManagerface = nullptr;
    }
}

StatusManagerDbus *StatusManagerDbus::getInstance()
{
    return statusmanagerdbus();
}

void StatusManagerDbus::setCurrentTabletMode(bool state)
{
    if (m_statusManagerface != nullptr) {
        m_statusManagerface->call("set_tabletmode",state,"ukui-sidebar","changemode");
    } else {
        qWarning()<< "set Current Tablet Mode error";
    }
}

bool StatusManagerDbus::getCurrentTabletMode()
{
    if (m_statusManagerface != nullptr) {
        QDBusReply<bool> result = m_statusManagerface->call("get_current_tabletmode");
        return result.value();
    } else {
        qWarning()<< "get Current Tablet Mode error";
        return false;
    }
}

bool StatusManagerDbus::getAutoRotation()
{
    if (m_statusManagerface != nullptr) {
        QDBusReply<bool> result = m_statusManagerface->call("get_auto_rotation");
        return result.value();
    } else {
        qWarning()<< "get Auto Rotation error";
        return false;
    }
}

void StatusManagerDbus::setAutoRotation(bool state)
{
    if (m_statusManagerface != nullptr) {
        m_statusManagerface->call("set_auto_rotation",state,"ukui-sidebar","setautorotation");
    } else {
        qWarning()<< "set Auto Rotation error";
    }
}



//! 消息通知
Q_GLOBAL_STATIC(NotificationsDbus, notificationsDbus)

NotificationsDbus::NotificationsDbus()
{
    m_notificationInterface = new  QDBusInterface(NOTIFICATION_DBUS_SERVICE,
                                                  NOTIFICATION_DBUS_PATH,
                                                  NOTIFICATION_DBUS_INTERFACE,
                                                  QDBusConnection::sessionBus());

    if (m_notificationInterface == nullptr) {
        qWarning()<< "NotificationsDbus init error";
    }
}

NotificationsDbus::~NotificationsDbus()
{
    if (m_notificationInterface != nullptr) {
        delete  m_notificationInterface;
        m_notificationInterface = nullptr;
    }
}

NotificationsDbus *NotificationsDbus::getInstance()
{
    return notificationsDbus();
}

//暂时这样，以后輝耀把全部功能打開
void NotificationsDbus::SendMessage(QString title,QString describe,QString content)
{
    QList<QVariant> args;
    QStringList argg;
    QMap<QString, QVariant> pearMap;
    args<<title
        <<((unsigned int) 0)
        <<QString("")
        <<describe
        <<content
        <<argg
        <<pearMap
        <<(int)-1;
    m_notificationInterface->callWithArgumentList(QDBus::AutoDetect,"Notify",args);
}



//! 侧边栏 core
Q_GLOBAL_STATIC(SidebarCoreDbus, sidebarCoreDbus)

SidebarCoreDbus::SidebarCoreDbus()
{
    m_sidebarCoreInterface = new  QDBusInterface(SIDEBAR_CORE_DBUS_SERVICE,
                                                 SIDEBAR_CORE_DBUS_PATH,
                                                 SIDEBAR_CORE_DBUS_INTERFACE,
                                                 QDBusConnection::sessionBus());

    if (m_sidebarCoreInterface == nullptr) {
        qWarning()<< "SidebarCoreDbus init error";
    }
}

SidebarCoreDbus::~SidebarCoreDbus()
{
    if (m_sidebarCoreInterface != nullptr) {
        delete  m_sidebarCoreInterface;
        m_sidebarCoreInterface = nullptr;
    }
}

SidebarCoreDbus *SidebarCoreDbus::getInstance()
{
    return sidebarCoreDbus();
}

int SidebarCoreDbus::getSidebarHeight()
{
    if (m_sidebarCoreInterface != nullptr) {
        QDBusMessage msg = m_sidebarCoreInterface->call("getSidebarHeight");
        return msg.arguments().at(0).toInt();
    } else {
        return 1018;
    }
}

void SidebarCoreDbus::sendAnimationActionSignal(uint time,int size)
{
    QDBusMessage message = QDBusMessage::createSignal("/org/ukui/Sidebar",
                                                      "org.ukui.Sidebar",
                                                      "animationAction");
    uint utime = time;
    int distance = size;
    message << utime << distance;
    QDBusConnection::sessionBus().send(message); //发射信号
}

void SidebarCoreDbus::sendAnimationStatusSignal(QString arg)
{
    QDBusMessage message = QDBusMessage::createSignal("/org/ukui/Sidebar",
                                                      "org.ukui.Sidebar",
                                                      "sidebarStateChange");

    message << arg ;
    QDBusConnection::sessionBus().send(message); //发射信号

}


//! 侧边栏 快捷操作面板
Q_GLOBAL_STATIC(SidebarQuickOperationDbus, sidebarQuickOperationDbus)

SidebarQuickOperationDbus::SidebarQuickOperationDbus()
{
    m_quickOperationInterface = new  QDBusInterface(QUICK_OPERATION_DBUS_SERVICE,
                                                    QUICK_OPERATION_DBUS_PATH,
                                                    QUICK_OPERATION_DBUS_INTERFACE,
                                                    QDBusConnection::sessionBus());
    if (m_quickOperationInterface == nullptr) {
        qWarning()<< "SidebarQuickOperationDbus init error";
    }

    QDBusConnection::sessionBus().connect(QUICK_OPERATION_DBUS_SERVICE,
                                          QUICK_OPERATION_DBUS_PATH,
                                          QUICK_OPERATION_DBUS_INTERFACE,
                                          QUICK_OPERATION_DBUS_SIGNAL, this, SLOT(quickOperationSlot(QString) ) );
}

SidebarQuickOperationDbus::~SidebarQuickOperationDbus()
{
    if (m_quickOperationInterface != nullptr) {
        delete  m_quickOperationInterface;
        m_quickOperationInterface = nullptr;
    }
}

SidebarQuickOperationDbus *SidebarQuickOperationDbus::getInstance()
{
    return sidebarQuickOperationDbus();
}

int SidebarQuickOperationDbus::getQuickOperationPanelHeight()
{
    if (m_quickOperationInterface != nullptr) {
        QDBusReply<int> quickOperationPanelHeight = m_quickOperationInterface->call("getQuickOperationPanelHeight");
        if (quickOperationPanelHeight.isValid()) {
            return quickOperationPanelHeight;
        } else {
            qWarning()<< "get QuickOperationPanelHeight is failed";
            return HEIGHT;
        }
    } else {
        return HEIGHT;
    }
}

int SidebarQuickOperationDbus::getQuickOperationPanelHideHeight()
{
    if (m_quickOperationInterface != nullptr) {
         QDBusReply<int> quickOperationPanelHideHeight = m_quickOperationInterface->call("getQuickOperationPanelHideHeight");
         if (quickOperationPanelHideHeight.isValid()) {
             return quickOperationPanelHideHeight;
         } else {
             qWarning()<< "get QuickOperationPanelHideHeight is failed";
             return HIDEHEIGHT;
         }
    } else {
        return HIDEHEIGHT;
    }
}

int SidebarQuickOperationDbus::getAnimationSpeed()
{
    if (m_quickOperationInterface != nullptr) {
        QDBusReply<int> animationSpeed = m_quickOperationInterface->call("getAnimationSpeed");
        if (animationSpeed.isValid()) {
            return animationSpeed;
        } else {
            qWarning()<<"get AnimationSpeed is failed";
            return ANIMATIONSPEED;
        }
    } else {
        return ANIMATIONSPEED;
    }
}

void SidebarQuickOperationDbus::quickOperationSlot(QString arg)
{
    emit Sig_quickOperationSignal(arg);
}


//! 侧边栏 消息通知
Q_GLOBAL_STATIC(SidebarNotificationDbus, sidebarNotificationDbus)

SidebarNotificationDbus::SidebarNotificationDbus()
{
    m_notificationInterface = new  QDBusInterface(SIDEBAR_NOTIFICATION_DBUS_SERVICE,
                                                    SIDEBAR_NOTIFICATION_DBUS_PATH,
                                                    SIDEBAR_NOTIFICATION_DBUS_INTERFACE,
                                                    QDBusConnection::sessionBus());
    if (m_notificationInterface == nullptr) {
        qWarning()<< "m_notificationInterface init error";
    }

    QDBusConnection::sessionBus().connect(SIDEBAR_NOTIFICATION_DBUS_SERVICE,
                                          SIDEBAR_NOTIFICATION_DBUS_PATH,
                                          SIDEBAR_NOTIFICATION_DBUS_INTERFACE,
                                          SIDEBAR_NOTIFICATION_DBUS_SIGNAL, this, SLOT(notificationSlot()));
}

SidebarNotificationDbus::~SidebarNotificationDbus()
{
    if (m_notificationInterface != nullptr) {
        delete  m_notificationInterface;
        m_notificationInterface = nullptr;
    }
}

SidebarNotificationDbus *SidebarNotificationDbus::getInstance()
{
    return sidebarNotificationDbus();
}

void SidebarNotificationDbus::addNotificationNum()
{
    if (m_notificationInterface != nullptr) {
        m_notificationInterface->call(SIDEBAR_NOTIFICATION_DBUS_ADD_NUM);
    } else {
        qWarning()<<" addNotificationNum is failed";
    }
}

void SidebarNotificationDbus::subNotificationNum()
{
    if (m_notificationInterface != nullptr) {
        m_notificationInterface->call(SIDEBAR_NOTIFICATION_DBUS_SUB_NUM);
    } else {
        qWarning()<<" subNotificationNum is failed";
    }
}

void SidebarNotificationDbus::clearNotificationNum()
{
    if (m_notificationInterface != nullptr) {
        m_notificationInterface->call(SIDEBAR_NOTIFICATION_DBUS_CLEAR_NUM);
    } else {
        qWarning()<<" clearNotificationNum is failed";
    }
}

int SidebarNotificationDbus::getNotificationNum()
{
    if (m_notificationInterface != nullptr) {
        QDBusReply<int> notificationNum = m_notificationInterface->call(SIDEBAR_NOTIFICATION_DBUS_GET_NUM);
        return notificationNum;
    } else {
        qWarning()<<" getNotificationNum is failed , return 0";
        return 0;
    }
}

void SidebarNotificationDbus::notificationSlot()
{
    emit Sig_notificationSignal();
}



//! setting-daemon
Q_GLOBAL_STATIC(SettingDaemonDbus, settingDaemonDbus)

SettingDaemonDbus::SettingDaemonDbus()
{
    m_settingDaemonInterface = new  QDBusInterface(SETTING_DAEMON_DBUS_SERVICE,
                                                    SETTING_DAEMON_DBUS_PATH,
                                                    SETTING_DAEMON_DBUS_INTERFACE,
                                                    QDBusConnection::sessionBus());
    if (m_settingDaemonInterface == nullptr) {
        qWarning()<< "SettingDaemonDbus init error";
    }

    QDBusConnection::sessionBus().connect(SETTING_DAEMON_DBUS_SERVICE,
                                          SETTING_DAEMON_DBUS_PATH,
                                          SETTING_DAEMON_DBUS_INTERFACE,
                                          SETTING_DAEMON_DBUS_FLIGHT_SIGNAL, this, SLOT(flightStateChange(int)));
}

SettingDaemonDbus::~SettingDaemonDbus()
{
    if (m_settingDaemonInterface != nullptr) {
        delete  m_settingDaemonInterface;
        m_settingDaemonInterface = nullptr;
    }
}

SettingDaemonDbus *SettingDaemonDbus::getInstance()
{
    return settingDaemonDbus();
}

void SettingDaemonDbus::setFlightState(bool state)
{
    //1:开启，0:关闭
    if (m_settingDaemonInterface != nullptr) {
        if(state) {
            m_settingDaemonInterface->call(SETTING_DAEMON_DBUS_SET_FLIGHT_STATE,1);
        } else {
            m_settingDaemonInterface->call(SETTING_DAEMON_DBUS_SET_FLIGHT_STATE,0);
        }
    } else {
        qWarning()<<"set flightState is failed";
    }
}

bool SettingDaemonDbus::getFlightState()
{
    if (m_settingDaemonInterface != nullptr) {
        QDBusReply<int> flightState = m_settingDaemonInterface->call(SETTING_DAEMON_DBUS_GET_FLIGHT_STATE);
        if (flightState.isValid()) {
            if(flightState == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            qWarning()<<"get flightState is failed";
            return FLIGHT_STATE;
        }
    } else {
        return FLIGHT_STATE;
    }
}

void SettingDaemonDbus::flightStateChange(int num)
{
    emit Sig_flightStateSignal(num);
}

//! KWin
Q_GLOBAL_STATIC(KwinDbus, kwinDbus)

KwinDbus::KwinDbus()
{
    m_kWinInterface = new  QDBusInterface(UKUI_KWIN_DBUS_SERVICE,
                                          UKUI_KWIN_DBUS_PATH,
                                          UKUI_KWIN_DBUS_INTERFACE,
                                          QDBusConnection::sessionBus());
    if (m_kWinInterface == nullptr) {
        qWarning()<< "m_kWinInterface init error";
    }
}

KwinDbus::~KwinDbus()
{
    if (m_kWinInterface != nullptr) {
        delete  m_kWinInterface;
        m_kWinInterface = nullptr;
    }
}

KwinDbus *KwinDbus::getInstance()
{
    return kwinDbus();
}

bool KwinDbus::getActive()
{
    if (m_kWinInterface != nullptr) {
        QDBusMessage msg=  m_kWinInterface->call("nightColorInfo");
        const QDBusArgument &dbusArg = msg.arguments().at( 0 ).value<QDBusArgument>();
        QMap<QString,QVariant > map;
        dbusArg >> map;
        for(QString outer_key : map.keys()){
            QVariant interMap = map.value(outer_key);
            if(outer_key=="Active") {
                return interMap.toBool();
            }
        }
    } else {
        qWarning()<<"get Active is failed,return false";
        return UKUI_KWIN_DBUS_ACTIVE;
    }
}

int KwinDbus::getNightTemperature()
{
    if (m_kWinInterface != nullptr) {
        QDBusMessage msg=  m_kWinInterface->call("nightColorInfo");
        const QDBusArgument &dbusArg = msg.arguments().at( 0 ).value<QDBusArgument>();
        QMap<QString,QVariant > map;
        dbusArg >> map;
        for(QString outer_key : map.keys()){
            QVariant interMap = map.value(outer_key);
            if(outer_key=="NightTemperature") {
                return interMap.toInt();
            }
        }
    } else {
        qWarning()<<"get  NightTemperature is failed,return 1";
        return UKUI_KWIN_DBUS_NIGHE;
    }
}

void KwinDbus::setNightMode(bool state)
{
    if (m_kWinInterface != nullptr) {
        QHash<QString, QVariant> data;
        if(state){
            data.insert("Active", true);
            data.insert("Mode", 3);
            data.insert("NightTemperature", getNightTemperature());
            m_kWinInterface->call("setNightColorConfig", data);
        } else {
            data.insert("Active", false);
            m_kWinInterface->call("setNightColorConfig", data);
        }
    } else {
        qWarning()<<"set Night Mode is failed ";
    }
}

//! bluetooth
Q_GLOBAL_STATIC(BluetoothDbus, bluetoothDbus)

BluetoothDbus::BluetoothDbus()
{
    m_bluetoothDbusInterface = new  QDBusInterface(KYLIN_BLUETOOTH_SERVER_NAME,
                                          KYLIN_BLUETOOTH_SERVER_PATH,
                                          KYLIN_BLUETOOTH_SERVER_INTERFACE,
                                          QDBusConnection::sessionBus());
    if (m_bluetoothDbusInterface == nullptr) {
        qWarning()<< "m_bluetoothDbusInterface init error";
    }
}

BluetoothDbus::~BluetoothDbus()
{
    if (m_bluetoothDbusInterface != nullptr) {
        delete  m_bluetoothDbusInterface;
        m_bluetoothDbusInterface = nullptr;
    }
}

BluetoothDbus *BluetoothDbus::getInstance()
{
    return bluetoothDbus();
}

bool BluetoothDbus::getDefaultAdapterPower()
{
    if (m_bluetoothDbusInterface != nullptr) {
        QDBusReply<bool> devStatus = m_bluetoothDbusInterface->call("getDefaultAdapterPower");
        return devStatus;
    } else {
        qWarning()<<"get DefaultAdapterPower is failed,return false";
        return false;
    }
}

void BluetoothDbus::setDefaultAdapterPower(bool state)
{
    if (m_bluetoothDbusInterface != nullptr) {
        m_bluetoothDbusInterface->call("setDefaultAdapterPower",state);
    } else {
        qWarning()<<"set DefaultAdapterPower is failed";
    }
}

//! 应用管理
Q_GLOBAL_STATIC(AppManagerDbus, appManagerDbus)

AppManagerDbus::AppManagerDbus()
{
    m_appManagerDbusInterface = new  QDBusInterface(KYLIN_APP_MANAGER_NAME,
                                                    KYLIN_APP_MANAGER_PATH,
                                                    KYLIN_APP_MANAGER_INTERFACE,
                                                    QDBusConnection::sessionBus());
    if (m_appManagerDbusInterface == nullptr) {
        qWarning()<< "m_appManagerDbusInterface init error";
    }
}

AppManagerDbus::~AppManagerDbus()
{
    if (m_appManagerDbusInterface != nullptr) {
        delete  m_appManagerDbusInterface;
        m_appManagerDbusInterface = nullptr;
    }
}

AppManagerDbus *AppManagerDbus::getInstance()
{
    return appManagerDbus();
}

bool AppManagerDbus::LaunchApp(QString desktopFile)
{
    if (m_appManagerDbusInterface != nullptr) {
        QDBusReply<bool> devStatus = m_appManagerDbusInterface->call("LaunchApp",desktopFile);
        return devStatus;
    } else {
        qWarning()<<"LaunchApp is failed,return false";
        return false;
    }
}

bool AppManagerDbus::LaunchAppWithArguments(QString desktopFile, QStringList args)
{
    if (m_appManagerDbusInterface != nullptr) {
        QDBusReply<bool> devStatus = m_appManagerDbusInterface->call("LaunchAppWithArguments",desktopFile,args);
        return devStatus;
    } else {
        qWarning()<<"LaunchAppWithArguments is failed,return false";
        return false;
    }
}
