#include "customwindowbutton.h"
#include <QVariant>

CustomWindowButton::CustomWindowButton(QWidget *parent)
    : QPushButton(parent)
{
    setFocusPolicy(Qt::NoFocus);
    setFixedSize(Parmscontroller::parm(Parmscontroller::Parm::PM_WindowButtonBarSize), Parmscontroller::parm(Parmscontroller::Parm::PM_WindowButtonBarSize));
    setProperty("isWindowButton", 0x1);
    setProperty("useIconHighlightEffect", 0x2);
    setFlat(true);
    installEventFilter(this);

    changeTheme();
    connect(m_gsetting, &QGSettings::changed, this, [=]() {
        changeTheme();
    });
    connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() {
        setFixedSize(Parmscontroller::parm(Parmscontroller::Parm::PM_WindowButtonBarSize), Parmscontroller::parm(Parmscontroller::Parm::PM_WindowButtonBarSize));
        updateGeometry();
        update();
    });
    return;
}

CustomWindowButton::~CustomWindowButton()
{
}

void CustomWindowButton::setCustomIcon(QIcon customIcon)
{
    icon = customIcon;
    setIcon(customIcon);
    return;
}

bool CustomWindowButton::eventFilter(QObject *watched, QEvent *event)
{
    QIcon menuIcon = icon;

    if (QEvent::WindowDeactivate == event->type()) {
        QColor color = QColor(179, 179, 179);
        menuIcon = ThemeController::drawColoredPixmap(menuIcon.pixmap(QSize(Parmscontroller::parm(Parmscontroller::Parm::PM_WindowButtonBarSize), Parmscontroller::parm(Parmscontroller::Parm::PM_WindowButtonBarSize))), color);
        setIcon(menuIcon);
    }
    if (QEvent::WindowActivate == event->type() || QEvent::MouseButtonPress == event->type()) {
        setIcon(icon);
    }
    return QObject::eventFilter(watched, event);
}

void CustomWindowButton::changeTheme()
{
    initThemeStyle();
}
