#include "sui.h"
sui::sui(QWidget *parent)
    : QWidget(parent),
      m_layout(new QVBoxLayout),
      m_widget(new QWidget),
      m_press(false),
      m_border_width(2),
      ori(NONE)
{
    m_title = new sui_title(this);
    setObjectName(tr("m_widget"));
    m_layout->addWidget(m_title);
    m_title->setMouseTracking(true);
    m_layout->setContentsMargins(5, 5, 5, 5);
    m_layout->setSpacing(0);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setMouseTracking(true);
    setBaseSize(1260, 800);
    resize(1260, 800);
    setLayout(m_layout);
    connect(m_title, SIGNAL(signals_close()), this, SLOT(close()));
}
sui::~sui()
{
}

void sui::showEvent(QShowEvent *event)
{
    this->setAttribute(Qt::WA_Mapped);
    QWidget::showEvent(event);
    QSize oldSize = this->size();
    resize(oldSize + QSize(10, 10));
    resize(oldSize);
}

void sui::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawRoundedRect(this->rect(), 10, 10);
    QWidget::paintEvent(event);
}

void sui::region(const QPoint &currentGlobalPoint)
{
    QRect rect = this->rect();
    QPoint topLeft = this->mapToGlobal(rect.topLeft());
    QPoint bottomRight = this->mapToGlobal(rect.bottomRight());
    
    int x = currentGlobalPoint.x();
    int y = currentGlobalPoint.y();
    
    // 使用更清晰的边界检查
    bool inLeftBorder = (x >= topLeft.x() && x <= topLeft.x() + m_border_width);
    bool inTopBorder = (y >= topLeft.y() && y <= topLeft.y() + m_border_width);
    bool inRightBorder = (x >= bottomRight.x() - m_border_width && x <= bottomRight.x());
    bool inBottomBorder = (y >= bottomRight.y() - m_border_width && y <= bottomRight.y());

    if (inLeftBorder && inTopBorder) {
        ori = LEFTTOP;
        this->setCursor(QCursor(Qt::SizeFDiagCursor));
    }
    else if (inRightBorder && inBottomBorder) {
        ori = RIGHTBOTTOM;
        this->setCursor(QCursor(Qt::SizeFDiagCursor));
    }
    else if (inLeftBorder && inBottomBorder) {
        ori = LEFTBOTTOM;
        this->setCursor(QCursor(Qt::SizeBDiagCursor));
    }
    else if (inRightBorder && inTopBorder) {
        ori = RIGHTTOP;
        this->setCursor(QCursor(Qt::SizeBDiagCursor));
    }
    else if (inLeftBorder) {
        ori = LEFT;
        this->setCursor(QCursor(Qt::SizeHorCursor));
    }
    else if (inRightBorder) {
        ori = RIGHT;
        this->setCursor(QCursor(Qt::SizeHorCursor));
    }
    else if (inTopBorder) {
        ori = UP;
        this->setCursor(QCursor(Qt::SizeVerCursor));
    }
    else if (inBottomBorder) {
        ori = DOWN;
        this->setCursor(QCursor(Qt::SizeVerCursor));
    }
    else {
        ori = NONE;
        this->setCursor(QCursor(Qt::ArrowCursor));
    }
}

void sui::mousePressEvent(QMouseEvent *event)
{
    switch (event->button())
    {
    case Qt::LeftButton:
        m_press = true;
        if (ori != NONE)
        {
            // 返回当前抓取鼠标输入的窗口
            this->mouseGrabber();
        }
        else
        {
            m_point = event->globalPosition().toPoint() - this->frameGeometry().topLeft();
            // globalPos()鼠标位置，topLeft()窗口左上角的位置
        }
        break;
    case Qt::RightButton:
        //this->setWindowState(Qt::WindowMinimized);
        break;
    default:
        QWidget::mousePressEvent(event);
    }
}
void sui::mouseMoveEvent(QMouseEvent *event)
{
    // 添加最大化状态判断
    if (this->windowState() & Qt::WindowMaximized) {
        this->setCursor(QCursor(Qt::ArrowCursor));
        return;
    }

    // 添加鼠标按钮判断
    if (!(event->buttons() & Qt::LeftButton)) {
        m_press = false;
    }
    
    QPoint globalPoint = event->globalPosition().toPoint();
    QRect rect = this->rect();
    QPoint topLeft = mapToGlobal(rect.topLeft());
    QPoint bottomRight = mapToGlobal(rect.bottomRight());

    if (this->windowState() != Qt::WindowMaximized)
    {
        // 没有按下左键时才检测鼠标位置改变光标形状
        if (!m_press)
        {
            this->region(globalPoint);
        }
        else
        {
            if (ori != NONE)
            {
                QRect newRect(topLeft, bottomRight);

                switch (ori)
                {
                case LEFT:

                    if (bottomRight.x() - globalPoint.x() <= this->minimumWidth())
                    {
                        // 小于界面的最小宽度时，设置为左上角横坐标为窗口x
                        newRect.setLeft(topLeft.x());
                        // 只改变左边界
                    }
                    else
                    {
                        newRect.setLeft(globalPoint.x());
                    }
                    break;
                case RIGHT:
                    // 只能改变右边界
                    newRect.setWidth(globalPoint.x() - topLeft.x());
                    break;
                case UP:
                    if (bottomRight.y() - globalPoint.y() <= this->minimumHeight())
                    {
                        newRect.setY(topLeft.y());
                    }
                    else
                    {
                        newRect.setY(globalPoint.y());
                    }
                    break;
                case DOWN:
                    newRect.setHeight(globalPoint.y() - topLeft.y());
                    break;
                case LEFTTOP:
                    if (bottomRight.x() - globalPoint.x() <= this->minimumWidth())
                    {
                        newRect.setX(topLeft.x());
                    }
                    else
                    {
                        newRect.setX(globalPoint.x());
                    }

                    if (bottomRight.y() - globalPoint.y() <= this->minimumHeight())
                    {
                        newRect.setY(topLeft.y());
                    }
                    else
                    {
                        newRect.setY(globalPoint.y());
                    }
                    break;
                case RIGHTTOP:
                    if (globalPoint.x() - topLeft.x() >= this->minimumWidth())
                    {
                        newRect.setWidth(globalPoint.x() - topLeft.x());
                    }
                    else
                    {
                        newRect.setWidth(bottomRight.x() - topLeft.x());
                    }
                    if (bottomRight.y() - globalPoint.y() >= this->minimumHeight())
                    {
                        newRect.setY(globalPoint.y());
                    }
                    else
                    {
                        newRect.setY(topLeft.y());
                    }
                    break;
                case LEFTBOTTOM:
                    if (bottomRight.x() - globalPoint.x() >= this->minimumWidth())
                    {
                        newRect.setX(globalPoint.x());
                    }
                    else
                    {
                        newRect.setX(topLeft.x());
                    }
                    if (globalPoint.y() - topLeft.y() >= this->minimumHeight())
                    {
                        newRect.setHeight(globalPoint.y() - topLeft.y());
                    }
                    else
                    {
                        newRect.setHeight(bottomRight.y() - topLeft.y());
                    }
                    break;
                case RIGHTBOTTOM:
                    newRect.setWidth(globalPoint.x() - topLeft.x());
                    newRect.setHeight(globalPoint.y() - topLeft.y());
                    break;
                default:
                    break;
                }
                this->setGeometry(newRect);
            }
            else
            {
                // 移动窗口时保持箭头光标
                this->setCursor(QCursor(Qt::ArrowCursor));
                move(event->globalPosition().toPoint() - m_point);
                event->accept();
            }
        }
    }
}

// 鼠标释放事件
void sui::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_press = false;
        ori = NONE;  // 直接重置方向
        this->setCursor(QCursor(Qt::ArrowCursor));
        this->releaseMouse();
    }
}