#include "addbtn.h"
#include <QEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QVariant>
#include <QPainter>
#include <QGSettings/QGSettings>
#include <QApplication>

AddBtn::AddBtn(QWidget *parent):
    QPushButton(parent)
{
    this->setObjectName("this");
    this->setMinimumSize(QSize(580, 60));
    this->setMaximumSize(QSize(16777215, 60));
    this->setProperty("useButtonPalette", true);
//    this->setProperty("needTranslucent", true);
    this->setFlat(true);

    QHBoxLayout *addLyt = new QHBoxLayout;

    QLabel *iconLabel = new QLabel();
    QLabel *textLabel = new QLabel(tr("Add"));

    QIcon mAddIcon = QIcon::fromTheme("list-add-symbolic");
    iconLabel->setPixmap(mAddIcon.pixmap(mAddIcon.actualSize(QSize(16, 16))));
    iconLabel->setProperty("iconHighlightEffectMode", 1);

    const QByteArray idd(THEME_QT_SCHEMA);
    QGSettings *qtSettings  = new QGSettings(idd, QByteArray(), this);
    QString currentThemeMode = qtSettings->get(MODE_QT_KEY).toString();
    if ("ukui-dark" == currentThemeMode || "ukui-black" == currentThemeMode){
        iconLabel->setProperty("useIconHighlightEffect", true);
    }
    connect(qtSettings, &QGSettings::changed, this, [=](const QString &key) {
        if (key == "styleName") {
            QString currentThemeMode = qtSettings->get(key).toString();
            if ("ukui-black" == currentThemeMode || "ukui-dark" == currentThemeMode) {
                iconLabel->setProperty("useIconHighlightEffect", true);
            } else if("ukui-white" == currentThemeMode || "ukui-default" == currentThemeMode) {
                iconLabel->setProperty("useIconHighlightEffect", false);
            }
        }
    });

    addLyt->addStretch();
    addLyt->addWidget(iconLabel);
    addLyt->addWidget(textLabel);
    addLyt->addStretch();
    this->setLayout(addLyt);

}

AddBtn::~AddBtn()
{

}

void AddBtn::enterEvent(QEvent *event){
    Q_EMIT enterWidget();

    QPushButton::enterEvent(event);
}

void AddBtn::setBtnStyle(AddBtn::Shape type)
{
    switch (type) {
    case None:
        this->setStyleSheet("AddBtn:!checked:!pressed:!hover{background-color: palette(base);border-rdius: 0px}"
                            "AddBtn:!checked:!pressed:hover{background-color: palette(button);border-radius: 6px}");
        break;
    case Top:
        this->setStyleSheet("AddBtn:!checked:!pressed:!hover{background-color: palette(base); border-top-left-radius: 6px; border-top-right-radius: 6px;}"
                            "AddBtn:!checked:!pressed:hover{background-color: palette(button); border-radius: 6px;}");
        break;
    case Bottom:
        this->setStyleSheet("AddBtn:!checked:!pressed:!hover{background-color: palette(base); border-bottom-left-radius: 6px; border-bottom-right-radius: 6px;}"
                            "AddBtn:!checked:!pressed:hover{background-color: palette(button); border-radius: 6px;}");
        break;
    case Box:
        this->setStyleSheet("AddBtn:!checked:!pressed:!hover{background-color: palette(base); border-radius: 6px;}"
                            "AddBtn:!checked:!pressed:hover{background-color: palette(button); border-radius: 6px;}");
        break;
    }
}

void AddBtn::leaveEvent(QEvent *event){
    Q_EMIT leaveWidget();

    QPushButton::leaveEvent(event);
}

void AddBtn::paintEvent(QPaintEvent *event)

{

    QPainter painter(this);

    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿

    painter.setPen(Qt::NoPen);

    painter.setBrush(this->palette().base().color());


    QPalette pal = qApp->palette();

    QColor color = pal.color(QPalette::Button);

    color.setAlphaF(0.5);

    pal.setColor(QPalette::Button, color);

    this->setPalette(pal);


    QPushButton::paintEvent(event);

}
