//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Overlay/RectangleOverlay.cpp
//! @brief     Implements class RectangleOverlay.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Overlay/RectangleOverlay.h"
#include "GUI/Model/Mask/MaskItems.h"
#include "GUI/View/Plotter/ColorMap.h"
#include <QGraphicsSceneMouseEvent>

RectangleOverlay::RectangleOverlay(RectangleItem* item, ColorMap* plot)
    : IRectangularOverlay(plot)
    , m_item(item)
{
    m_item->maskVisibilityChanged();
}

QPainterPath RectangleOverlay::shape() const
{
    QPainterPath path;
    path.addRect(m_mask_rect);
    return path;
}

OverlayItem* RectangleOverlay::parameterizedItem() const
{
    return m_item;
}

void RectangleOverlay::onChangedX()
{
    setBlockOnProperty(true);
    m_item->setXLow(m_plot->fromSceneX(this->x()));
    m_item->setXUp(m_plot->fromSceneX(this->x() + m_mask_rect.width()));
    setBlockOnProperty(false);
}

void RectangleOverlay::onChangedY()
{
    setBlockOnProperty(true);
    m_item->setYLow(m_plot->fromSceneY(this->y() + m_mask_rect.height()));
    m_item->setYUp(m_plot->fromSceneY(this->y()));
    setBlockOnProperty(false);
}

void RectangleOverlay::onPropertyChange()
{
    update_view();
}

void RectangleOverlay::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
    if (m_active_handle_element) {
        qreal xmin = std::min(event->scenePos().x(), m_resize_opposite_origin.x());
        qreal xmax = std::max(event->scenePos().x(), m_resize_opposite_origin.x());
        qreal ymin = std::min(event->scenePos().y(), m_resize_opposite_origin.y());
        qreal ymax = std::max(event->scenePos().y(), m_resize_opposite_origin.y());

        if (m_active_handle_element->getHandleType() == SizeHandleElement::RESIZE) {
            m_item->setXLow(m_plot->fromSceneX(xmin));
            m_item->setYLow(m_plot->fromSceneY(ymax));
            m_item->setXUp(m_plot->fromSceneX(xmax));
            m_item->setYUp(m_plot->fromSceneY(ymin));
        } else if (m_active_handle_element->getHandleType() == SizeHandleElement::RESIZE_HEIGHT) {
            m_item->setYLow(m_plot->fromSceneY(ymax));
            m_item->setYUp(m_plot->fromSceneY(ymin));
        } else if (m_active_handle_element->getHandleType() == SizeHandleElement::RESIZE_WIDTH) {
            m_item->setXLow(m_plot->fromSceneX(xmin));
            m_item->setXUp(m_plot->fromSceneX(xmax));
        }
        update_view();
    } else {
        QGraphicsObject::mouseMoveEvent(event);
    }
}

//! updates position of view using item properties
void RectangleOverlay::update_position()
{
    setX(m_plot->toSceneX(m_item->xLow()));
    setY(m_plot->toSceneY(m_item->yUp()));
}

QRectF RectangleOverlay::mask_rectangle()
{
    return {0, 0, width(), height()};
}

qreal RectangleOverlay::width() const
{
    return m_plot->toSceneX(m_item->xUp()) - m_plot->toSceneX(m_item->xLow());
}

qreal RectangleOverlay::height() const
{
    return m_plot->toSceneY(m_item->yLow()) - m_plot->toSceneY(m_item->yUp());
}
