#include "WindowButtonGroup.h"
#include <QToolButton>
#include <QResizeEvent>
#include <QStyle>
#include <QDebug>
#include <QPixmap>

//为了避免使用此框架的app设置了全局的qpushbutton 的 qss样式影响此按钮，定义了一个类


class WindowButtonGroupPrivate
{
public:

    WindowToolButton *buttonClose;
    WindowToolButton *buttonMinimize;
    WindowToolButton *buttonMaximize;
    WindowButtonGroupPrivate()
        : buttonClose(nullptr)
        , buttonMinimize(nullptr)
        , buttonMaximize(nullptr)
    {
    }


    void setupMinimizeButton(WindowButtonGroup *par, bool on, qreal iconscale = 0.5)
    {
        if (on) {
            if (!buttonMinimize) {
                buttonMinimize = new WindowToolButton(par);
                buttonMinimize->setObjectName(QStringLiteral("SAMinimizeWindowButton"));
                buttonMinimize->setFixedSize(30, 28);
                buttonMinimize->setFocusPolicy(Qt::NoFocus);//避免铺抓到
				buttonMinimize->setStyleSheet(QString(""
					"WindowToolButton"
					"{ "
					"    background-color: transparent; color:white;"
                    "    border:none;"
                    "}"
                    "WindowToolButton:hover {background-color:rgba(244,244,244,180);}"
                    "WindowToolButton:pressed {background-color:#A5AF9B;}"
                    "WindowToolButton:focus{outline: none}"));
				//QIcon icon = par->style()->standardIcon(QStyle::SP_TitleBarMinButton);
				QIcon icon(":/MainWindow/Resources/min.png");
                buttonMinimize->setIconSize(buttonMinimize->size()*iconscale);
                buttonMinimize->setIcon(icon);
                par->connect(buttonMinimize, &QAbstractButton::clicked
                    , par, &WindowButtonGroup::minimizeWindow);
            }
        }else {
            if (buttonMinimize) {
                delete buttonMinimize;
            }
        }
        updateSize(par);
    }


    void setupMaximizeButton(WindowButtonGroup *par, bool on, qreal iconscale = 0.5)
    {
        if (on) {
            if (!buttonMaximize) {
                buttonMaximize = new WindowToolButton(par);
                buttonMaximize->setObjectName(QStringLiteral("SAMaximizeWindowButton"));
                buttonMaximize->setFixedSize(30, 28);
                buttonMaximize->setFocusPolicy(Qt::NoFocus);//避免铺抓到
                buttonMaximize->setStyleSheet(QString(""
                    "WindowToolButton"
                    "{ "
                    "    background-color: transparent; "
                    "    border:none;"
                    "}\n"
                    "WindowToolButton:hover {background-color:rgba(244,244,244,180);}"
                    "WindowToolButton:pressed {background-color:#A5AF9B;}"
                    "WindowToolButton:focus{outline: none;}"));
                //QIcon icon = par->style()->standardIcon(QStyle::SP_TitleBarMaxButton);
				QIcon icon(":/MainWindow/Resources/max.png");
                buttonMaximize->setIconSize(buttonMaximize->size()*iconscale);
                buttonMaximize->setIcon(icon);
                par->connect(buttonMaximize, &QAbstractButton::clicked
                    , par, &WindowButtonGroup::maximizeWindow);
            }
        }else {
            if (buttonMaximize) {
                delete buttonMaximize;
            }
        }
        updateSize(par);
    }


    void setupCloseButton(WindowButtonGroup *par, bool on, qreal iconscale = 0.5)
    {
        if (on) {
            if (!buttonClose) {
                buttonClose = new WindowToolButton(par);
                buttonClose->setObjectName(QStringLiteral("SACloseWindowButton"));
                buttonClose->setFixedSize(40, 28);
                buttonClose->setFocusPolicy(Qt::NoFocus);//避免铺抓到
                //buttonClose->setFlat(true);
                par->connect(buttonClose, &QAbstractButton::clicked
                    , par, &WindowButtonGroup::closeWindow);
                buttonClose->setStyleSheet(QString(""
                    "WindowToolButton"
                    "{ "
                    "    background-color: transparent; "
                    "    border:none;"
                    "}"
                    "WindowToolButton:hover {background-color:#F0604D;}"
                    "WindowToolButton:pressed {background-color:#F0604D;}"
                    "WindowToolButton:focus{outline: none;}"));
                //QIcon icon = par->style()->standardIcon(QStyle::SP_TitleBarCloseButton);
                QIcon icon(":/Resources/close.png");
                buttonClose->setIconSize(buttonClose->size()*iconscale);
                buttonClose->setIcon(icon);
            }
        }else {
            if (buttonClose) {
                delete buttonClose;
            }
        }
        updateSize(par);
    }


    void updateSize(WindowButtonGroup *par)
    {
        par->setFixedSize(sizeHint());
        int span = 0;

        if (buttonClose) {
            buttonClose->move(par->width()-buttonClose->width()
                , par->y());
            span = buttonClose->width();
        }
        if (buttonMaximize) {
            buttonMaximize->move(par->width()-buttonMaximize->width()-span
                , par->y());
            span += buttonMaximize->width();
        }
        if (buttonMinimize) {
            buttonMinimize->move(par->width()-buttonMinimize->width()-span
                , par->y());
        }
    }


    QSize sizeHint() const
    {
        int width = 0;
        int height = 0;

        if (buttonClose) {
            width += buttonClose->width();
            height = qMax(height, buttonClose->height());
        }
        if (buttonMaximize) {
            width += buttonMaximize->width();
            height = qMax(height, buttonMaximize->height());
        }
        if (buttonMinimize) {
            width += buttonMinimize->width();
            height = qMax(height, buttonMinimize->height());
        }
        return (QSize(width, height));
    }
};

WindowToolButton::WindowToolButton(QWidget *p):QToolButton(p)
{
    setAutoRaise(true);
}

WindowButtonGroup::WindowButtonGroup(QWidget *parent, qreal iconscale)
	: QWidget(parent)
    , m_d(new WindowButtonGroupPrivate())
	, rootWidget(parent)
{
    updateWindowFlag(iconscale);
    if (parent) {
        parent->installEventFilter(this);
    }
}


WindowButtonGroup::~WindowButtonGroup()
{
    delete m_d;
}


void WindowButtonGroup::setupMinimizeButton(bool on, qreal iconscale)
{
    m_d->setupMinimizeButton(this, on, iconscale);
}


void WindowButtonGroup::setupMaximizeButton(bool on, qreal iconscale)
{
    m_d->setupMaximizeButton(this, on, iconscale);
    updateMaximizeButtonIcon();
}


void WindowButtonGroup::setupCloseButton(bool on, qreal iconscale)
{
    m_d->setupCloseButton(this, on, iconscale);
}


void WindowButtonGroup::updateWindowFlag(qreal iconscale)
{
    Qt::WindowFlags flags = parentWidget()->windowFlags();

    setupMinimizeButton(flags & Qt::WindowMinimizeButtonHint, iconscale);

    setupMaximizeButton(flags & Qt::WindowMaximizeButtonHint, iconscale);

    setupCloseButton(flags & Qt::WindowCloseButtonHint, iconscale);
}


QSize WindowButtonGroup::sizeHint() const
{
    return (m_d->sizeHint());
}


bool WindowButtonGroup::eventFilter(QObject *watched, QEvent *e)
{
    if (watched == parentWidget()) {
        switch (e->type())
        {
        case QEvent::Resize:
            parentResize();
            break;

        default:
            break;
        }
    }
    return (false);//不截断任何事件
}


void WindowButtonGroup::parentResize()
{
    //QWidget *par = parentWidget();

	if (rootWidget) {
		QSize parSize = rootWidget->size();
        move(parSize.width() - width()-1, 1);
    }
}


void WindowButtonGroup::updateMaximizeButtonIcon()
{
    //QWidget *par = parentWidget();

	if (rootWidget) {
		if (rootWidget->isMaximized()) {
            if (m_d->buttonMaximize) {
                //m_d->buttonMaximize->setIcon(style()->standardIcon(QStyle::SP_TitleBarNormalButton));
				m_d->buttonMaximize->setIcon(QIcon(":/MainWindow/Resources/restore.png"));
            }
        }else {
            if (m_d->buttonMaximize) {
                //m_d->buttonMaximize->setIcon(style()->standardIcon(QStyle::SP_TitleBarMaxButton));
				m_d->buttonMaximize->setIcon(QIcon(":/MainWindow/Resources/max.png"));
            }
        }
    }
}


void WindowButtonGroup::closeWindow()
{
	if (rootWidget) {
		rootWidget->close();
    }
}


void WindowButtonGroup::minimizeWindow()
{
	if (rootWidget) {
		rootWidget->showMinimized();
    }
}


void WindowButtonGroup::maximizeWindow()
{
	QWidget *par = rootWidget;

    if (par) {
        if (par->isMaximized()) {
            par->showNormal();
        }else {
            par->showMaximized();
        }
        updateMaximizeButtonIcon();
    }
}




