#include "notificationDbus.h"
#include "notificationPlugin.h"
#include <QDBusConnection>
#include <QDateTime>

NotificationDbus::NotificationDbus(NotificationPlugin *parent)
{
    m_parent = parent;

    connect(ControlCenterNotiGsetting::getInstance(),&ControlCenterNotiGsetting::Sig_appNotifySettingChanged, \
            this,&NotificationDbus::appNotifySettingChangedSlot);

    fromSettingsGetInfoToList();

    /* 注册dbus */
    QDBusConnection::sessionBus().unregisterService("org.ukui.Sidebar");
    QDBusConnection::sessionBus().registerService("org.ukui.Sidebar");
    /* 注册对象路径，导出所有此对象的插槽 */
    /* registerObject参数：路径，interface,对象，options */
    QDBusConnection::sessionBus().registerObject("/org/ukui/Sidebar/notification",this,
                                                 QDBusConnection::ExportAllSlots|QDBusConnection::ExportAllSignals);

    /* 将消息添加到通知中心 */
    connect(this, SIGNAL(Sig_Notify(QString, QString, QString, QString, QString, QString, QDateTime, int)),
            m_parent, SLOT(onAddSingleNotify(QString, QString, QString, QString, QString, QString, QDateTime, int)));

    /* 配置实时关闭通知消息 */
    connect(this, SIGNAL(Sig_CloseAppMsg(QString)), m_parent, SLOT(onCloseAppMsg(QString)));

    /* 配置实时更新通知消息最大数 */
    connect(this, SIGNAL(Sig_UpdateAppMaxNum(QString, int)), m_parent, SLOT(onUpdateAppMaxNum(QString, int)));
}

/**
 * @brief NotificationDbus::sidebarNotification
 * 创建一条侧边栏通知
 * @param appName 消息名称
 * @param appIcon 图标地址
 * @param summary 消息概述
 * @param body 消息体
 * @param urlStr url
 * @param actions 执行动作
 */
void NotificationDbus::sidebarNotification(const QString &appName, const QString &appIcon,
                                           const QString &summary, const QString &body,
                                           const QString &urlStr,  const QString &actions)
{
    qInfo()<<"--------------->"<<appName<<appIcon<<summary<<body<<urlStr<<actions;

    QMap<QString, int>::const_iterator iter = m_nAppMaxNum.find(appName);
    int nMaxNum = 20;
    if ((iter != m_nAppMaxNum.end()) && (iter.value() > 0)) { //找到
        nMaxNum = iter.value();
    }

    QDateTime dateTime(QDateTime::currentDateTime());
    QMap<QString, bool>::const_iterator iter1 = m_mapAppSwitch.find(appName);
    if (iter1 == m_mapAppSwitch.end()) {
        /* 未被屏蔽的消息，正常写入通知列表 */
        emit Sig_Notify(appName, appIcon, summary, body, urlStr, actions, dateTime, nMaxNum);
    }
}

/**
 * @brief NotificationDbus::fromSettingsGetInfoToList
 * 获取列表信息
 */
void NotificationDbus::fromSettingsGetInfoToList()
{
    QString strAppName = ControlCenterNotiGsetting::getInstance()->getNotificationName();
    int maxNum = ControlCenterNotiGsetting::getInstance()->getMixNum();
    m_nAppMaxNum.insert(strAppName, maxNum);

    bool powerstatus = ControlCenterNotiGsetting::getInstance()->getMessage();
    if (false == powerstatus) {
        m_mapAppSwitch.insert(strAppName, powerstatus);
    }
}

/**
 * @brief NotificationDbus::appNotifySettingChangedSlot
 * 更新信息
 */
void NotificationDbus::appNotifySettingChangedSlot()
{

    QString strAppName = ControlCenterNotiGsetting::getInstance()->getNotificationName();
    int maxNum = ControlCenterNotiGsetting::getInstance()->getMixNum();
    m_nAppMaxNum[strAppName] = maxNum;
    emit Sig_UpdateAppMaxNum(strAppName, maxNum);

    bool powerstatus= ControlCenterNotiGsetting::getInstance()->getMessage();
    QMap<QString, bool>::const_iterator iter1 = m_mapAppSwitch.find(strAppName);
    if (iter1 == m_mapAppSwitch.end()) {
        /* 没找到，没在黑名单 */
        if(false == powerstatus) {
            /* 对于没在黑名单的，新增黑名单关闭消息要求实时更新至通知列表 */
            m_mapAppSwitch.insert(strAppName, powerstatus);
            emit Sig_CloseAppMsg(strAppName);
        }
    } else {
        if (true == powerstatus) {
            m_mapAppSwitch.remove(strAppName);
        }
    }
}

void NotificationDbus::setNotificationCenterHeight(int height)
{
    m_NotificationCenterHeight = height;
}

void NotificationDbus::setNotificationCenterWidth(int width)
{
    m_NotificationCenterWidth = width;
}

int NotificationDbus::getNotificationCenterHeight()
{
    return m_NotificationCenterHeight;
}

int NotificationDbus::getNotificationCenterWidth()
{
    return m_NotificationCenterWidth;
}

void NotificationDbus::sendNotificationNumChangeSignal()
{
    QDBusMessage message = QDBusMessage::createSignal("/org/ukui/Sidebar/notification",
                                                      "org.ukui.Sidebar.notification",
                                                      "Sig_NotificationNumChange");

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

void NotificationDbus::addNotificationNum()
{
    m_NotificationNum ++;
    sendNotificationNumChangeSignal();
}

void NotificationDbus::subNotificationNum()
{
    if (m_NotificationNum > 0) {
        m_NotificationNum --;
        sendNotificationNumChangeSignal();
    }
}

void NotificationDbus::clearNotificationNum()
{
    m_NotificationNum = 0;
    sendNotificationNumChangeSignal();
}

int NotificationDbus::getNotificationNum()
{
    return m_NotificationNum;
}
