#include "util/baseSelector.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <graphicsItem/graphicsBaseItem.h>
#include <graphicsItem/graphicsTextItem.h>
#include <QDebug>

QPointF BaseSelector::s_ptMouseDown(0.0, 0.0);
QPointF BaseSelector::s_ptMouseLast(0.0, 0.0);
double  BaseSelector::s_dAngleMouseDownToItem = 0.0;
int     BaseSelector::s_nDragHandle = 0;

BaseSelector::BaseSelector(QObject *parent)
    : QObject(parent)
{
    m_type = ST_base;
    m_opMode = OM_none;
    m_bHoverOnHandel = false;
}
BaseSelector::~BaseSelector()
{

}

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

    s_ptMouseDown = event->scenePos();
    s_ptMouseLast = event->scenePos();

    if(!m_bHoverOnHandel)
        scene->callParentEvent(event); //此处是通过触发QGraphicsScene的事件函数来取消所有被选中item的选中状态

    m_opMode = OM_none;

    QList<QGraphicsItem *> items = scene->selectedItems();
    if (items.count() == 1) //只有一个选中
    {
        QGraphicsItem *gitem = items.first();
        IShape *item = dynamic_cast<IShape *>(gitem);
        if(item)
        {
            //需要增加当前是否点击在控制点的判断函数
            s_nDragHandle = item->collidesWithHandle(event->scenePos());
            if(s_nDragHandle != H_none && s_nDragHandle <= H_left) //在缩放控制点上
            {
                m_opMode = OM_scale;
                emit setWorkingSelector(ST_scaling);
            }
            else if(s_nDragHandle >= H_rotate_leftTop && s_nDragHandle <= H_rotate_leftBottom) //在旋转控制点上
            {
                m_opMode = OM_rotate;
                //计算夹角
                QPointF originPoint = gitem->mapToScene(gitem->boundingRect().center());
                double dLengthY = s_ptMouseLast.y() - originPoint.y();
                double dLengthX = s_ptMouseLast.x() - originPoint.x();
                s_dAngleMouseDownToItem = atan2(dLengthY, dLengthX) * 180 / M_PI;
                // if(atan2(dLengthY, dLengthX) < 0)
                //     s_dAngleMouseDownToItem += 360.0;
                //创建副本
                item->createOperationCopy();
                emit setWorkingSelector(ST_rotation);
            }
            else if(s_nDragHandle > H_rotate_leftBottom) //编辑控制点上
            {
                m_opMode = OM_edit;
                setCursor(scene, Qt::ClosedHandCursor);
                emit setWorkingSelector(ST_editing);
            }
            else
            {
                m_opMode = OM_move;
                setCursor(scene, Qt::ClosedHandCursor);
                emit setWorkingSelector(ST_moving);
            }
        }
    }
    else if (items.count() > 1) //选中多个
    {
        m_opMode = OM_move;
        emit setWorkingSelector(ST_moving);
        setCursor(scene, Qt::ClosedHandCursor);
    }

    if(m_opMode == OM_move) //可以多个选中同时移动
    {
        for(int n = 0; n < items.size(); n++)
        {
            //创建副本
            IShape *item = dynamic_cast<IShape *>(items.at(n));
            if (item)
                item->createOperationCopy();
        }
    }
    else if(m_opMode == OM_none)
    {
        QGraphicsView *view = scene->getView();
        if(view)
            view->setDragMode(QGraphicsView::RubberBandDrag);
    }

}

void BaseSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    s_ptMouseLast = event->scenePos();

    QList<QGraphicsItem *> items = scene->selectedItems();

    if (items.count() == 1)
    {
        QGraphicsItem *gitem = items.first();
        IShape *item = dynamic_cast<IShape *>(gitem);
        if(item)
        {
            if(s_nDragHandle == H_none)
            {
                //设置光标样式
                int nHandle = item->collidesWithHandle(event->scenePos());
                if(nHandle == H_none)
                {
                    setCursor(scene, Qt::ArrowCursor);
                    m_bHoverOnHandel = false;
                }
                else if(nHandle >= H_edit)
                {
                    setCursor(scene, Qt::OpenHandCursor);
                    m_bHoverOnHandel = true;
                }
                else
                {
                    //划分为四组区间范围，分别水平组、垂直组、一三象限倾斜组、二四象限倾斜组，每组由两个对称区间构成
                    double dRotation = gitem->rotation();
                    dRotation += item->getSyncRotationDataFromParent();
                    //让角度保持在正负180的区间，也就是上下两个半圈，这样易于象限判断
                    if (dRotation > 180)
                        dRotation -= 360;
                    if (dRotation < -180)
                        dRotation += 360;
                    //qDebug() << "selfRotation:" << item->rotation() << " syncRotation:" << item->getSyncRotationDataFromParent() << " fininalRotatio:" << dRotation;
                    double dTileAngle = 15.0;

                    switch (nHandle)
                    {
                    case H_leftTop:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeBDiagCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeHorCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeVerCursor);
                        else //水平区间
                            setCursor(scene, Qt::SizeFDiagCursor);
                        break;
                    }
                    case H_top:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeHorCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeFDiagCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeBDiagCursor);
                        else
                            setCursor(scene, Qt::SizeVerCursor);
                        break;
                    }
                    case H_rightTop:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeFDiagCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeVerCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeHorCursor);
                        else
                            setCursor(scene, Qt::SizeBDiagCursor);
                        break;
                    }
                    case H_right:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeVerCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeBDiagCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeFDiagCursor);
                        else
                            setCursor(scene, Qt::SizeHorCursor);
                        break;
                    }
                    case H_rightBottom:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeBDiagCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeHorCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeVerCursor);
                        else //水平区间
                            setCursor(scene, Qt::SizeFDiagCursor);
                        break;
                    }
                    case H_bottom:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeHorCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeFDiagCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeBDiagCursor);
                        else
                            setCursor(scene, Qt::SizeVerCursor);
                        break;
                    }
                    case H_leftBottom:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeFDiagCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeVerCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeHorCursor);
                        else //水平区间
                            setCursor(scene, Qt::SizeBDiagCursor);
                        break;
                    }
                    case H_left:
                    {
                        if((dRotation > -145+dTileAngle && dRotation < -45-dTileAngle) || (dRotation > 45+dTileAngle && dRotation < 145-dTileAngle)) //垂直区间
                            setCursor(scene, Qt::SizeVerCursor);
                        else if((dRotation >= -45-dTileAngle && dRotation <= -45+dTileAngle) || (dRotation >= 145-dTileAngle && dRotation <= 145+dTileAngle)) //一三象限倾斜
                            setCursor(scene, Qt::SizeBDiagCursor);
                        else if((dRotation >= -145-dTileAngle && dRotation <= -145+dTileAngle) || (dRotation >= 45-dTileAngle && dRotation <= 45+dTileAngle)) //二四象限倾斜
                            setCursor(scene, Qt::SizeFDiagCursor);
                        else
                            setCursor(scene, Qt::SizeHorCursor);
                        break;
                    }
                    case H_rotate_leftTop:
                    {
                        int nSize = 24;
                        QCursor rotateCursor = QCursor(QPixmap(":/images/icon_rotate.png")/*.scaled(nSize, nSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)*/);
                        setCursor(scene, rotateCursor);
                        break;
                    }
                    case H_rotate_rightTop:
                    {
                        int nSize = 24;
                        QCursor rotateCursor = QCursor(QPixmap(":/images/icon_rotate.png")/*.scaled(nSize, nSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)*/);
                        setCursor(scene, rotateCursor);
                        break;
                    }
                    case H_rotate_rightBottom:
                    {
                        int nSize = 24;
                        QCursor rotateCursor = QCursor(QPixmap(":/images/icon_rotate.png")/*.scaled(nSize, nSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)*/);
                        setCursor(scene, rotateCursor);
                        break;
                    }
                    case H_rotate_leftBottom:
                    {
                        int nSize = 24;
                        QCursor rotateCursor = QCursor(QPixmap(":/images/icon_rotate.png")/*.scaled(nSize, nSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)*/);
                        setCursor(scene, rotateCursor);
                        break;
                    }
                    default:
                        break;
                    }

                    m_bHoverOnHandel = true;
                }
            }
        }
    }
}

void BaseSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    setCursor(scene, Qt::ArrowCursor);

    if(m_opMode == OM_none)
    {
        QGraphicsView *view = scene->getView();
        if(view)
            view->setDragMode(QGraphicsView::NoDrag);
    }

    m_opMode = OM_none;
    m_bHoverOnHandel = false;
    s_nDragHandle = H_none;
    scene->callParentEvent(event);
}

void BaseSelector::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    Q_UNUSED(event)
    QList<QGraphicsItem *> items = scene->selectedItems();
    if (items.count() == 1) {
        GraphicsTextItem *textItem = dynamic_cast<GraphicsTextItem *>(items.first());
        if (textItem && !textItem->isEditing())
            textItem->startEditing();
    }
}

void BaseSelector::setCursor(DesignerScene *scene, const QCursor &cursor)
{
    QGraphicsView *view = scene->getView();
    if (view)
        view->setCursor(cursor);
}
