#include "functionNavigationBar.h"
#include "./ui_functionNavigationBar.h"

#include <QPropertyAnimation>
#include <QTimer>

#define WIDTH 59

FunctionNavigationBar::FunctionNavigationBar(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::functionNavigationBar)
    , m_pLastFunButton(nullptr)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_TranslucentBackground);

    initialize();
}

FunctionNavigationBar::~FunctionNavigationBar()
{
    delete ui;
}


void FunctionNavigationBar::initialize()
{
    ui->funBtn_Dvie->setProperty("skin", "icon_dashboard");
    ui->funBtn_Dvie->setProperty("funName", "DVIE");
    connect(ui->funBtn_Dvie, SIGNAL(clicked()), this, SLOT(onBtnClicked_funBtn()));
    ui->funBtn_TCC->setProperty("skin", "icon_chart");
    ui->funBtn_TCC->setProperty("funName", "TCC");
    connect(ui->funBtn_TCC, SIGNAL(clicked()), this, SLOT(onBtnClicked_funBtn()));
    m_pLastFunButton = ui->funBtn_Dvie;

    m_strCurState = "collapse";
    m_bInAnimation = false;
    m_pAni_geo = new QPropertyAnimation(this, "geometry");
    m_pAni_geo->setDuration(100);
    connect(m_pAni_geo, &QPropertyAnimation::finished, this, [this]{m_bInAnimation = false;});

    int nMargin_left = 0, nMargin_top = 0, nMargin_right = 0, nMargin_bottom = 0;
    ui->mainLayout->getContentsMargins(&nMargin_left, &nMargin_top, &nMargin_right, &nMargin_bottom);
    resize(ui->dockWidget->width()+nMargin_left,height());
}

void FunctionNavigationBar::enterEvent(QEnterEvent* e)
{
    Q_UNUSED(e);
    //防止非意图操作，加一个定时器进行延时执行
    QTimer::singleShot(300,[this]{
        if(rect().contains(mapFromGlobal(cursor().pos())))
            expand();
    });
}

void FunctionNavigationBar::leaveEvent(QEvent* e)
{
    Q_UNUSED(e);
    collapse();
}

void FunctionNavigationBar::expand()
{
    if(m_bInAnimation || m_strCurState == "expand" )
        return;

    QWidget* pParent = parentWidget();
    if(pParent)
    {
        int nY = (pParent->height() - this->height()) * 0.5;
        int nMargin_left = 0, nMargin_top = 0, nMargin_right = 0, nMargin_bottom = 0;
        ui->mainLayout->getContentsMargins(&nMargin_left, &nMargin_top, &nMargin_right, &nMargin_bottom);
        QRect startRect = QRect(0, nY, ui->dockWidget->width() + nMargin_left, this->height());
        QRect endRect = QRect(0, nY, WIDTH, this->height());
        m_pAni_geo->setStartValue(startRect);
        m_pAni_geo->setEndValue(endRect);
        m_pAni_geo->start();
        m_strCurState = "expand";
    }
}
void FunctionNavigationBar::collapse()
{
    if(m_bInAnimation || m_strCurState == "collapse" )
        return;

    QWidget* pParent = parentWidget();
    if(pParent)
    {
        int nY = (pParent->height() - this->height()) * 0.5;
        QRect startRect = QRect(0, nY, WIDTH, this->height());
        int nMargin_left = 0, nMargin_top = 0, nMargin_right = 0, nMargin_bottom = 0;
        ui->mainLayout->getContentsMargins(&nMargin_left, &nMargin_top, &nMargin_right, &nMargin_bottom);
        QRect endRect = QRect(0, nY, ui->dockWidget->width() + nMargin_left, this->height());
        m_pAni_geo->setStartValue(startRect);
        m_pAni_geo->setEndValue(endRect);
        m_pAni_geo->start();
        m_strCurState = "collapse";
    }
}

void FunctionNavigationBar::onBtnClicked_funBtn()
{
    QPushButton* pBtn = qobject_cast<QPushButton*>(sender());
    if(pBtn == m_pLastFunButton)
        return;
    //先将上一个被选中的按钮复原
    if(m_pLastFunButton)
    {
        QString strIcon = m_pLastFunButton->property("skin").toString();
        m_pLastFunButton->setStyleSheet("QPushButton\n"
                                        "{\n"
                                        "   padding:2px;\n"
                                        "   image: url(:/images/" + strIcon + "_notSelected);\n"
                                                    "   background-color:transparent;\n"
                                                    "}\n"
                                                    "QPushButton::hover\n"
                                                    "{\n"
                                                    "	background-color: rgba(58, 63, 75, 200);\n"
                                                    "}");
    }
    //将当前按钮置为选中状态
    QString strIcon = pBtn->property("skin").toString();
    pBtn->setStyleSheet("QPushButton\n"
                        "{\n"
                        "   padding:2px;\n"
                        "   image: url(:/images/" + strIcon + "_selected);\n"
                                    "   background-color:transparent;\n"
                                    "}\n");
    m_pLastFunButton = pBtn;

    QString strFunName = pBtn->property("funName").toString();
    emit sgl_funBtnClicke(strFunName);
}
