#include "templatewidget.h"

templatewidget::templatewidget(QString buttonName)
    : m_pButtonName(buttonName),
      m_pStyleNormal(new customstyle_switchNormalStatus("ukui-default")),
      m_pStyleOpen(new CustomStyle_SwitchOpenStatus("ukui-default")),
      m_pWidgetButton(new QWidget()),
      m_pInteriorButton(new QPushButton()),
      m_pInteriorLabel(new QLabel()),
      m_pVboxButtonLayout(new QVBoxLayout()),
      m_pVboxMainLayout(new QVBoxLayout())

{
    initInterfaceAttribute();
    setLabelText();
    setButtonStatus();
    initInterfaceLayout();
    initButtonConnect();
    initGsettingConnect();

    this->setFixedSize(QUICK_BUTTONWIDTH, QUICK_BUTTONHEIGHT);
    this->setContentsMargins(0, 0, 0, 0);
}

templatewidget::~templatewidget()
{

}

void templatewidget::initInterfaceAttribute()
{
    m_pWidgetButton->setFixedSize(56, 56);
    m_pWidgetButton->setContentsMargins(0, 0, 0, 0);

    m_pInteriorButton->setFixedSize(56, 56);
    m_pInteriorButton->setIconSize(QSize(24, 24));

    m_pInteriorLabel->setFixedWidth(60);
    m_pInteriorLabel->setAlignment(Qt::AlignHCenter);

    QFont ft;
    ft.setPixelSize(14);
    m_pInteriorLabel->setFont(ft);
    m_pInteriorLabel->setAlignment(Qt::AlignHCenter);
    return;
}

void templatewidget::initInterfaceLayout()
{
    m_pVboxButtonLayout->setContentsMargins(0, 0, 0, 0);
    m_pVboxButtonLayout->addWidget(m_pInteriorButton, 0, Qt::AlignCenter);
    m_pWidgetButton->setLayout(m_pVboxButtonLayout);

    m_pVboxMainLayout->setContentsMargins(0, 0, 0, 0);
    m_pVboxMainLayout->setSpacing(0);
    m_pVboxMainLayout->addWidget(m_pWidgetButton, 0, Qt::AlignCenter);
    m_pVboxMainLayout->addItem(new QSpacerItem(15, 10));
    m_pVboxMainLayout->addWidget(m_pInteriorLabel);
    this->setLayout(m_pVboxMainLayout);
    return;
}

// 获取按钮状态值
void templatewidget::setButtonStatus()
{
    if(m_pButtonName == SETTING_NAME) {
        //系统设置无状态值，设置默认未选中
        changeStyle(false);
    } else if (m_pButtonName == POWERSAVING_NAME) {
        //加载省电模式状态，初始化按钮状态
        changeStyle((PowerModeGsetting::getInstance()->getPowerPolicyAc() == 2) ||
                    (PowerModeGsetting::getInstance()->getPowerPolicyBattery() == 2));
    } else if (m_pButtonName == BLUETOOTH_NAME) {
        //加载蓝牙状态，初始化按钮状态
        changeStyle(BluetoothDbus::getInstance()->getDefaultAdapterPower());
    } else if (m_pButtonName == FLIGHTMODE_NAME) {
        //加载飞行模式，初始化按钮状态
        changeStyle(SettingDaemonDbus::getInstance()->getFlightState());
    } else if (m_pButtonName == PROJECTSCREEN_NAME) {
        //加载投屏，初始化按钮状态
        changeStyle(false);
    } else if (m_pButtonName == EYE_PROTECTION_NAME) {
        //加载护眼，初始化按钮状态 获取主题深浅色
        if (StyleGsetting::getInstance()->getStyleName() == DARK_STYLE) {
            //深色
            changeStyle(true);
        } else {
            //浅色
            changeStyle(false);
        }
    } else if (m_pButtonName == SCREENSHOT_NAME) {
        //加载截图，初始化按钮状态
        changeStyle(false);
    } else if (m_pButtonName == CLIPBOARD_NAME) {
        //加载剪贴板，初始化按钮状态
        changeStyle(false);
    } else if (m_pButtonName == NODISTURB_NAME) {
        //加载勿扰模式，初始化按钮状态
        changeStyle(QuickOperationGsetting::getInstance()->getModeStatus());
    } else if (m_pButtonName == ROTATION_NAME) {
        //加载自动旋转，初始化按钮状态
        automaticRotationButttonClickSlots();
        changeStyle(StatusManagerDbus::getInstance()->getAutoRotation());
    } else if (m_pButtonName == PAD_NAME) {
        //加载平板模式，初始化按钮状态
        changeStyle(StatusManagerDbus::getInstance()->getCurrentTabletMode());
    }
}

// 初始化gsetting 信号连接
void templatewidget::initGsettingConnect()
{
    //主题改变更新,需要修改，连接的应该还有图标更改
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,this,&templatewidget::setButtonStatus);

    if(m_pButtonName == POWERSAVING_NAME) {
        //节能模式
        connect(PowerModeGsetting::getInstance(),&PowerModeGsetting::Sig_powerModeChanged,[=](QString key) {
            if((key == POWER_MODE_POWERPOLICYBATTERY) || (key == POWER_MODE_POWERPOLICYAC)) {
                setButtonStatus();
            }
        });

    } else if (m_pButtonName == BLUETOOTH_NAME) {
        //蓝牙
        QDBusConnection::sessionBus().connect(QString(KYLIN_BLUETOOTH_SERVER_NAME),
                                              QString(KYLIN_BLUETOOTH_SERVER_PATH),
                                              QString(KYLIN_BLUETOOTH_SERVER_INTERFACE),
                                              QString("defaultAdapterPowerChanged"), this, SLOT(setButtonStatus()));

    } else if (m_pButtonName == ROTATION_NAME) {
        //自动旋转
        QDBusConnection::sessionBus().connect(QString("com.kylin.statusmanager.interface"),
                                             QString("/"),
                                             QString("com.kylin.statusmanager.interface"),
                                             QString("mode_change_signal"), this, SLOT(setButtonStatus()));

        QDBusConnection::sessionBus().connect(QString("com.kylin.statusmanager.interface"),
                                             QString("/"),
                                             QString("com.kylin.statusmanager.interface"),
                                             QString("auto_rotation_change_signal"), this, SLOT(setButtonStatus()));

    } else if (m_pButtonName == PAD_NAME) {
        //平板&pc
        QDBusConnection::sessionBus().connect(QString("com.kylin.statusmanager.interface"),
                                             QString("/"),
                                             QString("com.kylin.statusmanager.interface"),
                                             QString("mode_change_signal"), this, SLOT(setButtonStatus()));

    } else if (m_pButtonName == EYE_PROTECTION_NAME) {
        //护眼
        connect(SettingsDaemonGsetting::getInstance(),&SettingsDaemonGsetting::Sig_settingsDaemonChanged, [=](QString key){
            if ((key == SETTINGSDAEMON_KEY) || (key == SETTINGSDAEMON_THEME_KEY)) {
                setButtonStatus();
            }
        });

        connect(EyeprotectionGsetting::getInstance(),&EyeprotectionGsetting::Sig_eyeprotectionChanged, [=](QString key){
            if ((key == EYEPROTECTION_TIME_KEY) || (key == EYEPROTECTION_DISTANCE_KEY)) {
                setButtonStatus();
            }
        });

    } else if (m_pButtonName == FLIGHTMODE_NAME) {
        //护眼 主线版本监听主题变化！
        connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key) {
            if (key == UKUI_STYLE_NAME_KEY) {
                setButtonStatus();
            }
        });
    }
}

bool templatewidget::checkButtonValid()
{
    //因为不同组件机制不同，一些功能暂时无法确定是否为可用状态，此部分代码持续跟进
    if(m_pButtonName == SETTING_NAME) {
        //系统设置
        return true;
    } else if (m_pButtonName == POWERSAVING_NAME) {
        //省电模式
        if (!getBacklitFile()) {
            m_pInteriorButton->setEnabled(false);
            return false;
        } else {
            return true;
        }
    } else if (m_pButtonName == BLUETOOTH_NAME) {
        //蓝牙
        return true;
    } else if (m_pButtonName == FLIGHTMODE_NAME) {
        //飞行模式
        return true;
    } else if (m_pButtonName == PROJECTSCREEN_NAME) {
        //投屏
        return true;
    } else if (m_pButtonName == EYE_PROTECTION_NAME) {
        //护眼模式
        return true;
    } else if (m_pButtonName == SCREENSHOT_NAME) {
        //截图
        return true;
    } else if (m_pButtonName == CLIPBOARD_NAME) {
        //剪贴板
        return true;
    } else if (m_pButtonName == NODISTURB_NAME) {
        //勿扰模式
        return true;
    } else if (m_pButtonName == ROTATION_NAME) {
        //自动旋转
        return true;
    } else if (m_pButtonName == PAD_NAME) {
        //平板模式
        return true;
    }
}

//! 此处图标有未反色的问题！
//! 目前反色机制为：根据点击按钮的状态，设置不同高亮图标来实现
//! 目前图标与主题的交互为： 深色主题用浅色图标， 浅色主题用深色图标
//! 所以目前图标颜色机制行为如下：
//! 在浅色主题中，按钮为未点击态，图标为深色；按钮为点击态，图标为浅色
//! 在深色主题中，按钮为未点击态，图标为浅色；按钮为点击态，图标为浅色（不变）
//!  !!! 目前出现的问题是： 浅色主题的按钮点击状态的深浅色状态改变的问题 !!!
//!
//! 注意： intel 版本目前主题图标库没有相应图片，为引用资源文件的方式设置
//!       3.1 已应用主题图标库
//!
// 设置按钮图标  state true 点击状态 false 非点击状态
void templatewidget::setButtonIcon(bool state)
{
    if(state){
        //点击态
        if (m_pButtonName == POWERSAVING_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_POWER_NORMAL_NAME, QIcon(KYLIN_POWER_PRESS_PATH)));
        } else if (m_pButtonName == BLUETOOTH_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_BLUETOOTH_NORMAL_PATH, QIcon(KYLIN_BLUETOOTH_NORMAL_PATH)));
        }  else if (m_pButtonName == FLIGHTMODE_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SETTING_WHITE_PATH, QIcon(KYLIN_SETTING_WHITE_PATH)));
        } else if (m_pButtonName == PROJECTSCREEN_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SETTING_SCREEN_WHITE_PATH, QIcon(KYLIN_SETTING_SCREEN_WHITE_PATH)));
        } else if (m_pButtonName == EYE_PROTECTION_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_EYE_MODE_NORMAL_3_1_NAME, QIcon(KYLIN_EYE_MODE_NORMAL_3_1_NAME)));
        } else if (m_pButtonName == SCREENSHOT_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SCREENSHOT_NORMAL_PATH_3_1_WHITE, QIcon(KYLIN_SCREENSHOT_NORMAL_PATH_3_1_WHITE)));
        } else if(m_pButtonName == SETTING_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SETTING_PRESS_3_1_NAME, QIcon(KYLIN_SETTING_PRESS_3_1_NAME)));
        } else if (m_pButtonName == CLIPBOARD_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_CLIPBOARD_WHITE_PATH, QIcon(KYLIN_CLIPBOARD_WHITE_PATH)));
        } else if (m_pButtonName == NODISTURB_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_NODISTURB_PRESS_NAME, QIcon(KYLIN_NODISTURB_PRESS_PATH)));
        } else if (m_pButtonName == ROTATION_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_AUTOMATICROTATION_PRESS_NAME, QIcon(KYLIN_AUTOMATICROTATION_PRESS_PATH)));
        } else if (m_pButtonName == PAD_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_PAD_PRESS_NAME, QIcon(KYLIN_PAD_PRESS_PATH)));
        }

    } else {
        //非点击态
        if (m_pButtonName == POWERSAVING_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_POWER_NORMAL_NAME, QIcon(KYLIN_POWER_NORMAL_PATH)));
        } else if (m_pButtonName == BLUETOOTH_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_BLUETOOTH_NORMAL_PATH, QIcon(KYLIN_BLUETOOTH_NORMAL_PATH)));
        } else if (m_pButtonName == FLIGHTMODE_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SETTING_WHITE_PATH, QIcon(KYLIN_SETTING_WHITE_PATH)));
        } else if (m_pButtonName == PROJECTSCREEN_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SETTING_SCREEN_WHITE_PATH, QIcon(KYLIN_SETTING_SCREEN_WHITE_PATH)));
        } else if (m_pButtonName == EYE_PROTECTION_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_EYE_MODE_NORMAL_3_1_NAME, QIcon(KYLIN_EYE_MODE_NORMAL_3_1_NAME)));
        } else if (m_pButtonName == SCREENSHOT_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SCREENSHOT_NORMAL_PATH_3_1_WHITE, QIcon(KYLIN_SCREENSHOT_NORMAL_PATH_3_1_WHITE)));
        } else if(m_pButtonName == SETTING_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_SETTING_PRESS_3_1_NAME, QIcon(KYLIN_SETTING_PRESS_3_1_NAME)));
        } else if (m_pButtonName == CLIPBOARD_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_CLIPBOARD_WHITE_PATH, QIcon(KYLIN_CLIPBOARD_WHITE_PATH)));
        } else if (m_pButtonName == NODISTURB_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_NODISTURB_NORMAL_NAME, QIcon(KYLIN_NODISTURB_NORMAL_PATH)));
        } else if (m_pButtonName == ROTATION_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_AUTOMATICROTATION_NORMAL_NAME, QIcon(KYLIN_AUTOMATICROTATION_NORMAL_PATH)));
        } else if (m_pButtonName == PAD_NAME) {
            m_pInteriorButton->setIcon(QIcon::fromTheme(KYLIN_PAD_NORMAL_NAME, QIcon(KYLIN_PAD_NORMAL_PATH)));
        }

    }
    m_pInteriorButton->update();
}

// 设置按钮状态  state true 点击状态 false 非点击状态
void templatewidget::setButtonStyle(bool state)
{
    //判断按钮是否有此功能
    if (!checkButtonValid()) {
        m_pInteriorButton->setStyle(m_pStyleNormal);
    } else if (state){
        m_pInteriorButton->setStyle(m_pStyleOpen);
    } else {
        m_pInteriorButton->setStyle(m_pStyleNormal);
    }
}

// 根据不同按钮标志，连接不同信号
void templatewidget::initButtonConnect()
{
    if(m_pButtonName == SETTING_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::settingButtonClick);
    } else if (m_pButtonName == POWERSAVING_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::powerSavingButtonClickSlots);
    } else if (m_pButtonName == BLUETOOTH_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::bluetoothButtonClickSlots);
    } else if (m_pButtonName == FLIGHTMODE_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::flightmodeClickSlots);
    } else if (m_pButtonName == PROJECTSCREEN_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::projectionscreenClickSlots);
    } else if (m_pButtonName == EYE_PROTECTION_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::eyeProtectionClickSlots);
    } else if (m_pButtonName == SCREENSHOT_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::screenshotClickSlots);
    } else if (m_pButtonName == CLIPBOARD_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::clipboardClickSlots);
    } else if (m_pButtonName == NODISTURB_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::nodisturbClickSlots);
    } else if (m_pButtonName == ROTATION_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::automaticRotationClickSlots);
    } else if (m_pButtonName == PAD_NAME) {
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::padClickSlots);
    }
}

// 根据不同按钮标志，设置不同文案
void templatewidget::setLabelText()
{
    LabelFotmatText *m_pLabelFotmatText = new LabelFotmatText;
    if(m_pButtonName == SETTING_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("Setting"), m_pInteriorLabel));
    }  else if (m_pButtonName == POWERSAVING_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("Energy"), m_pInteriorLabel));
    } else if (m_pButtonName == BLUETOOTH_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("Bluetooth"), m_pInteriorLabel));
    } else if (m_pButtonName == FLIGHTMODE_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("flight"), m_pInteriorLabel));
    } else if (m_pButtonName == PROJECTSCREEN_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("projectscreen"), m_pInteriorLabel));
    } else if (m_pButtonName == EYE_PROTECTION_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("Night mode"), m_pInteriorLabel));
    } else if (m_pButtonName == SCREENSHOT_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("screenshot"), m_pInteriorLabel));
    } else if (m_pButtonName == CLIPBOARD_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("clipboard"), m_pInteriorLabel));
    } else if (m_pButtonName == NODISTURB_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("NotiToggle"), m_pInteriorLabel));
    } else if (m_pButtonName == ROTATION_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("Autorotate"), m_pInteriorLabel));
    } else if (m_pButtonName == PAD_NAME) {
        m_pInteriorLabel->setText(m_pLabelFotmatText->SetFormatBody(QObject::tr("Pad"), m_pInteriorLabel));
    }
}

void templatewidget::changeStyle(bool state)
{
    setButtonIcon(state);
    setButtonStyle(state);
    setLabelTextColor(state);
}

bool templatewidget::getBacklitFile()
{
    QProcess process;
    process.start("ukui-power-backlight-helper --get-max-brightness");
    process.waitForFinished();
    QByteArray output = process.readAllStandardOutput();
    QString str_output = output;
    if (str_output == "没有找到您系统上的背光\n") {
        return false;
    } else {
        return true;
    }
}

void templatewidget::getInitialData()
{
    //获取亮度信息
    m_brightness = PowerModeGsetting::getInstance()->getBrightnessAc();

    //获取电池节能状态
    m_BatteryPowerStatus = PowerModeGsetting::getInstance()->getPowerPolicyBattery();

    //获取电源节能状态
    m_AcPowerStatus = PowerModeGsetting::getInstance()->getPowerPolicyAc();

}

QColor templatewidget::getSystemPaleteColor(bool state)
{
    QStyleOption opt;
    opt.init(m_pInteriorButton);

    if (state) {
        return opt.palette.color(QPalette::Highlight);
    } else {
        return opt.palette.color(QPalette::Text);
    }
}

void templatewidget::setLabelTextColor(bool state)
{
    QPalette palette;
    QColor color = getSystemPaleteColor(state);
    color.setAlphaF(0.75);
    palette.setBrush(QPalette::WindowText, color);
    m_pInteriorLabel->setPalette(palette);
    this->update();
}

// 控制面板槽函数
void templatewidget::settingButtonClick()
{
    if(!AppManagerDbus::getInstance()->LaunchApp("ukui-control-center.desktop")) {
        qWarning()<<"AppManagerDbus 接口无效，采用QProcess启动";
        QProcess p1;
        p1.startDetached("ukui-control-center");
    }
    sendPointData(SETTING_NAME);
    return;
}

// 省电模式槽函数
void templatewidget::powerSavingButtonClickSlots()
{
    if ((PowerModeGsetting::getInstance()->getPowerPolicyAc() == 2) ||
          (PowerModeGsetting::getInstance()->getPowerPolicyBattery() == 2)){
        //设置不节电
        if((PowerModeGsetting::getInstance()->getBrightnessAc()) < m_brightness){
            PowerModeGsetting::getInstance()->setBrightnessAc(m_brightness);
        }

        PowerModeGsetting::getInstance()->setPowerPolicyBattery(m_BatteryPowerStatus);
        PowerModeGsetting::getInstance()->setPowerPolicyAc(m_AcPowerStatus);

    } else {
        getInitialData();
        //设置节电模式
        PowerModeGsetting::getInstance()->setBrightnessAc(BRIGHTNESSDATA);
        PowerModeGsetting::getInstance()->setPowerPolicyBattery(POWERSAVINGNUM);
        PowerModeGsetting::getInstance()->setPowerPolicyAc(POWERSAVINGNUM);
    }
    setButtonStatus();
    sendPointData(POWERSAVING_NAME);
    return;
}

// 蓝牙槽函数
void templatewidget::bluetoothButtonClickSlots()
{
    if(BluetoothDbus::getInstance()->getDefaultAdapterPower()) {
        BluetoothDbus::getInstance()->setDefaultAdapterPower(false);
    } else {
        BluetoothDbus::getInstance()->setDefaultAdapterPower(true);
    }

    setButtonStatus();
    sendPointData(BLUETOOTH_NAME);
    return;
}

// 飞行模式槽函数
void templatewidget::flightmodeClickSlots()
{
    if (SettingDaemonDbus::getInstance()->getFlightState()) {
        SettingDaemonDbus::getInstance()->setFlightState(false);
    } else {
        SettingDaemonDbus::getInstance()->setFlightState(true);
    }
    setButtonStatus();
    sendPointData(FLIGHTMODE_NAME);
    return;
}

// 投屏模式槽函数
void templatewidget::projectionscreenClickSlots()
{
    QProcess p(0);
    p.startDetached("ukydisplayswitch");
    p.waitForStarted();
    sendPointData(PROJECTSCREEN_NAME);
    return;
}

// 夜间模式(主线版本)
void templatewidget::eyeProtectionClickSlots()
{
    //主线版本 改主题颜色 org.mate.interface & org.ukui.style
    if(SettingsDaemonGsetting::getInstance()->getnightLightEnabled()) {
        //默认主题
        StyleGsetting::getInstance()->setStyleName(DEFAULT_STYLE);
        GtkStyleGsetting::getInstance()->setGtkTheme(WHITE_STYLE);
        KwinDbus::getInstance()->setNightMode(false);
        SettingsDaemonGsetting::getInstance()->setnightLightEnabled(false);
    } else {
        //深色主题
        StyleGsetting::getInstance()->setStyleName(DARK_STYLE);
        GtkStyleGsetting::getInstance()->setGtkTheme(BLACK_STYLE);
        KwinDbus::getInstance()->setNightMode(true);
        SettingsDaemonGsetting::getInstance()->setnightLightEnabled(true);
    }
    setButtonStatus();
    sendPointData(EYE_PROTECTION_NAME);
    return;
}

// 截图槽函数
void templatewidget::screenshotClickSlots()
{
    QProcess p(0);
    p.startDetached("ukui-sidebar -state");
    p.waitForStarted();
    QTimer::singleShot(500,this,[=](){
        QStringList args;
        args.append("gui");
        if(!AppManagerDbus::getInstance()->LaunchAppWithArguments("kylin-screenshot.desktop",args)) {
            qWarning()<<"AppManagerDbus 接口无效，采用QProcess启动";
            QProcess p(0);
            p.startDetached("kylin-screenshot gui");
            p.waitForStarted();
        }
    });
    sendPointData(SCREENSHOT_NAME);
    return;
}

// 剪贴板槽函数
void templatewidget::clipboardClickSlots()
{
    QProcess p(0);
    p.startDetached("ukui-paste");
    p.waitForStarted();
    sendPointData(CLIPBOARD_NAME);
    return;
}

// 勿扰模式
void templatewidget::nodisturbClickSlots()
{
    //取自身值再取反设置
    QuickOperationGsetting::getInstance()->setModeStatus(!(QuickOperationGsetting::getInstance()->getModeStatus()));

    //设置自身状态值
    QuickOperationGsetting::getInstance()->setModeSoundState(QuickOperationGsetting::getInstance()->getModeStatus());
    QuickOperationGsetting::getInstance()->setModeNotifyState(QuickOperationGsetting::getInstance()->getModeStatus());

    setButtonStatus();
    sendPointData(NODISTURB_NAME);
    return;
}

//自动旋转
void templatewidget::automaticRotationClickSlots()
{
    StatusManagerDbus::getInstance()->setAutoRotation(!(StatusManagerDbus::getInstance()->getAutoRotation()));

    disconnect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::automaticRotationClickSlots);
    QTimer::singleShot(400,this,[=](){
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::automaticRotationClickSlots);
    });

    setButtonStatus();
    sendPointData(ROTATION_NAME);
    return;
}

//自动旋转 - 按钮状态
void templatewidget::automaticRotationButttonClickSlots()
{
    //首次启动设置可自动旋转状态
    if (oneShotBool) {
        StatusManagerDbus::getInstance()->setAutoRotation(true);
        oneShotBool= false;
    }

    //判断是否为平板模式，非平板模式禁用自动旋转按钮
    if(StatusManagerDbus::getInstance()->getCurrentTabletMode()) {
        m_pInteriorButton->setEnabled(true);
    } else {
        m_pInteriorButton->setEnabled(false);
    }
    return;
}

//平板模式
void templatewidget::padClickSlots()
{
    StatusManagerDbus::getInstance()->setCurrentTabletMode(!(StatusManagerDbus::getInstance()->getCurrentTabletMode()));
    disconnect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::padClickSlots);
    QTimer::singleShot(500,this,[=](){
        connect(m_pInteriorButton, &QPushButton::clicked, this, &templatewidget::padClickSlots);
    });

    QProcess p1(0);
    p1.startDetached("ukui-sidebar -hide");
    p1.waitForStarted();
    sendPointData(PAD_NAME);
    return;

}

void templatewidget::sendPointData(QString btnName)
{
    pointDataStruct pointData;
    pointData.module = btnName;
    pointData.function = CLICK;
    DataProcess::getInstance()->sendData(pointData);
}

