#include "trayicon.h"

TrayIcon::TrayIcon(QWidget *parent)
    : QWidget(parent)
{
    if(QGSettings::isSchemaInstalled("org.ukui.bluetooth")){
        settings = new QGSettings("org.ukui.bluetooth");
        connect(settings, &QGSettings::changed,this,&TrayIcon::GSettings_value_chanage);
    }

    bluetooth_tray_icon = new QSystemTrayIcon(this);
    tray_Menu = new QMenu(this);
    QAction *settins_action = new QAction(tr("Set Bluetooth Item"),tray_Menu);
    settins_action->setIcon(QIcon::fromTheme("document-page-setup-symbolic", QIcon(":/res/x/setup.png")) );
    tray_Menu->addAction(settins_action);
    connect(tray_Menu, &QMenu::triggered, this, [=]{
        emit openBluetoothSettings();
    });

    //Create taskbar tray icon and connect to signal slot
    //创建任务栏托盘图标，并连接信号槽
    bluetooth_tray_icon = new QSystemTrayIcon(this);
    bluetooth_tray_icon->setContextMenu(tray_Menu);
    bluetooth_tray_icon->setToolTip(tr("Bluetooth"));
    bluetooth_tray_icon->setVisible(settings->get("tray-show").toBool());

    if(settings->get("switch").toString() == "false"){
        if(QIcon::hasThemeIcon("bluetooth-error"))
            bluetooth_tray_icon->setIcon(QIcon::fromTheme("bluetooth-error"));
        else
            bluetooth_tray_icon->setIcon(QIcon::fromTheme("bluetooth-active-symbolic"));
    }else{
        bluetooth_tray_icon->setIcon(QIcon::fromTheme("bluetooth-active-symbolic"));
    }

    connect(bluetooth_tray_icon,&QSystemTrayIcon::activated,
            [=](QSystemTrayIcon::ActivationReason reason){
                switch (reason)
                {
                    case QSystemTrayIcon::MiddleClick:
                    case QSystemTrayIcon::DoubleClick: /* 来自于双击激活。 */
                    case QSystemTrayIcon::Trigger: /* 来自于单击激活。 */
                        InitTrayWidget();
                        break;
                    case QSystemTrayIcon::Unknown:
                    case QSystemTrayIcon::Context:
                    break;
                }
            });
#ifdef KYSEC
    if (!kysec_is_disabled() && kysec_get_3adm_status() && (getuid() || geteuid())){
        secState = true;
        bluetooth_tray_icon->setVisible(false);
    }
#endif
}

TrayIcon::~TrayIcon()
{
    delete settings;
}

void TrayIcon::InitTrayWidget()
{
    emit showTayWidget();
}

void TrayIcon::SetTrayIcon(bool open)
{
    qDebug() << Q_FUNC_INFO << open;
    if(!open){
        if(QIcon::hasThemeIcon("bluetooth-error"))
            bluetooth_tray_icon->setIcon(QIcon::fromTheme("bluetooth-error"));
        else
            bluetooth_tray_icon->setIcon(QIcon::fromTheme("bluetooth-active-symbolic"));
    }else{
        bluetooth_tray_icon->setIcon(QIcon::fromTheme("bluetooth-active-symbolic"));
    }
}

void TrayIcon::GSettings_value_chanage(const QString &key)
{
    qDebug() << Q_FUNC_INFO << key;
    if(key != "trayShow" || secState)
        return;
    if (!hasAdapter)
        return;
    if(bluetooth_tray_icon->isVisible() != settings->get(key).toBool()){
        bluetooth_tray_icon->setVisible(settings->get(key).toBool());
    }
}

void TrayIcon::SetAdapterFlag(bool set) {
    qDebug() << Q_FUNC_INFO << __LINE__ ;
    hasAdapter = set;
    if (!settings->get("trayShow").toBool() && set)
        return;
    if (secState)
        return;
    bluetooth_tray_icon->setVisible(set);
    qDebug() << Q_FUNC_INFO << __LINE__ ;
}
