#include "movelabel.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>
#include "ukuiwindowhelper/ukuiwindowhelper.h"
#include <QPainterPath>
MoveLabel::MoveLabel(QWidget *parent) : QLabel(parent)
{
    setAttribute(Qt::WA_TranslucentBackground);
    UkuiWindowHelper *windowHelper = new UkuiWindowHelper(this);
    windowHelper->setBlurEffect(QRegion(), 0, true);
    QPalette p = palette();
    m_backColor = p.color(QPalette::Window);
    m_backColor.setAlpha(125);
}

bool MoveLabel::getFirstMove()
{
    return isFirstMove;
}

void MoveLabel::setFirstMove(bool firstMove)
{
    isFirstMove = firstMove;
}

void MoveLabel::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    QPainterPath path;
    path.addRoundedRect(rect(), 8, 8);
    painter.fillPath(path, m_backColor);

    QWidget::paintEvent(event);
}

void MoveLabel::mousePressEvent(QMouseEvent *e)
{
    m_dragStart = e->globalPos();
    m_offsetPos = m_dragStart-this->mapToGlobal(QPoint(0,0));
}

void MoveLabel::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton){
        QDesktopWidget* desktop = QApplication::desktop();
        QRect desktopRect = desktop->availableGeometry();
        //临时方案，四周不可拖动区域大小尚且不清楚
        QRect windowRect(desktopRect.x()+20,desktopRect.y()+40,desktopRect.width()-40,desktopRect.height()-40);
        //不可拖动区域拖动趋势判定，如果拖向不可拖动区域会被阻止不作任何处理
        QPoint point(e->globalPos() - m_dragStart);
        bool flag = false;
        //靠左左拖
        if(point.x()<=0&&this->x()<=windowRect.x()){
            flag = true;
        }
        //靠上上拖
        if(point.y()<=0&&this->y()<=windowRect.y()){
            flag = true;
        }
        //靠右右拖
        if(point.x()>=0&&this->x()+width()>=windowRect.x()+windowRect.width()){
            flag = true;
        }
        //靠下下拖
        if(point.y()>=0&&this->y()+height()>=windowRect.y()+windowRect.height()){
            flag = true;
        }
        if(!flag){
            isFirstMove = true;
            QPoint posMove = e->globalPos()-m_offsetPos;
            int xx = posMove.x()-20;
            int yy = posMove.y()-60;
            if(m_buttonHandler){
//                m_buttonHandler->setIsMoved(isFirstMove);
                m_buttonHandler->updatePosition(QRect(xx,yy,this->width(),this->height()));
                vectorButtons = m_buttonHandler->getButtons();
                this->move(vectorButtons.first()->pos().x() - 15,
                           vectorButtons.first()->pos().y() - 3);
            }
        }
    }
}

void MoveLabel::mouseReleaseEvent(QMouseEvent *e)
{
    Q_UNUSED(e);
}

