#include "util/creatingSelector.h"
#include "graphicsItem/graphicsRectItem.h"
#include "graphicsItem/graphicsPolygonItem.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>

CreatingSelector::CreatingSelector(QObject *parent)
    : BaseSelector(parent)
{
    m_type = ST_cerating;
    m_creatingMethod = CM_drag;
    m_pCreatingItem = nullptr;
    //m_scalBasePoint = QPointF();
}
CreatingSelector::~CreatingSelector()
{

}

void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    if (event->button() != Qt::LeftButton)
        return;

    ms_ptMouseDown = event->scenePos();
    ms_ptMouseLast = event->scenePos();

    if(m_pCreatingItem == nullptr)
    {
        scene->clearSelection();

        switch (m_creatingItemType)
        {
        case GIT_rect:
        {
            m_creatingMethod = CM_drag;
            m_pCreatingItem = new GraphicsRectItem(QRect(-2, -2, 4, 4));
        }
        break;
        case GIT_roundRect:
        {
            m_creatingMethod = CM_drag;
            m_pCreatingItem = new GraphicsRectItem(QRect(1, 1, 1, 1), true);
        }
        break;
        case GIT_polygon:
        {
            m_creatingMethod = CM_click;
            m_pCreatingItem = new GraphicPolygonItem();
        }
        break;
        default:
            break;
        }

        if(m_pCreatingItem)
        {
            m_pCreatingItem->setPos(event->scenePos());
            m_pCreatingItem->setSelected(true);
            scene->addItem(m_pCreatingItem);

            if(m_creatingMethod == CM_drag)
            {
                ms_ptMouseDown += QPoint(2, 2);
                ms_nDragHandle = H_rightBottom;
            }
            else if(m_creatingMethod == CM_click)
                m_pCreatingItem->addPoint(ms_ptMouseDown);
        }
    }

    if(m_pCreatingItem && m_creatingMethod == CM_click)
    {
        //创建时添加了第一个点，紧接着再次添加第二点，然后从第二个点开始进行移动绘制
        m_pCreatingItem->addPoint(ms_ptMouseDown);
        ms_nDragHandle = m_pCreatingItem->handleCount();
    }

}

void CreatingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    setCursor(scene, Qt::CrossCursor);
    ms_ptMouseLast = event->scenePos();

    if (m_pCreatingItem && m_creatingMethod == CM_drag)
    {

        /*if(m_scalBasePoint.isNull()) //基准点不能采用临时变量，因为handle的坐标也在不断变化，计算会出现问题
        {
            m_scalBasePoint = m_pCreatingItem->getSymmetricPointPos(ms_nDragHandle); qDebug() << m_scalBasePoint;
            if(m_scalBasePoint.x() == 0)
                m_scalBasePoint.setX(1);
            if(m_scalBasePoint.y() == 0)
                m_scalBasePoint.setY(1);
        }*/

        QPointF scaleBasePoint =  m_pCreatingItem->boundingRect().topLeft();
        //计算缩放倍数
        QPointF iniDelta = m_pCreatingItem->mapFromScene(ms_ptMouseDown) - scaleBasePoint;
        QPointF lastDelta = m_pCreatingItem->mapFromScene(ms_ptMouseLast) - scaleBasePoint;
        double sx = lastDelta.x() / iniDelta.x();
        double sy = lastDelta.y() / iniDelta.y();

        m_pCreatingItem->resize(ms_nDragHandle, sx, sy, scaleBasePoint);
    }
    else if (m_pCreatingItem && m_creatingMethod == CM_click)
    {
        if(ms_nDragHandle > H_left)
        {
            m_pCreatingItem->editShape(ms_nDragHandle, ms_ptMouseLast);
        }
    }
}

void CreatingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    if (m_pCreatingItem && m_creatingMethod == CM_drag)
    {
        if (event->scenePos() == (ms_ptMouseDown - QPoint(2, 2))) //最小拖动范围
        {
            m_pCreatingItem->setSelected(false);
            scene->removeItem(m_pCreatingItem);
            delete m_pCreatingItem;
        }
        else if (ms_ptMouseLast != ms_ptMouseDown)
        {
            m_pCreatingItem->updateCoordinate();
            emit scene->signalAddItem(m_pCreatingItem);
        }

        ms_nDragHandle = H_none;
        m_pCreatingItem = nullptr;
        //m_scalBasePoint = QPointF();
        setCursor(scene, Qt::ArrowCursor);
        emit setWorkingSelector(ST_base);
    }
    else if (m_pCreatingItem && m_creatingMethod == CM_click && event->button() == Qt::RightButton) //右键结束绘制
    {
        if(m_pCreatingItem->endDrawing())
            m_pCreatingItem->updateCoordinate();
        else
        {
            m_pCreatingItem->setSelected(false);
            scene->removeItem(m_pCreatingItem);
            delete m_pCreatingItem;
        }

        ms_nDragHandle = H_none;
        m_pCreatingItem = nullptr;
        setCursor(scene, Qt::ArrowCursor);
        emit setWorkingSelector(ST_base);
    }
}
