//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Setup/Data2DToolbar.cpp
//! @brief     Implements class Data2DToolbar.
//!
//! @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/Setup/Data2DToolbar.h"
#include "GUI/View/Base/mainwindow_constants.h"
#include "GUI/View/Setup/FrameActions.h"
#include <QLabel>
#include <QToolButton>

Data2DToolbar::Data2DToolbar()
    : m_activity_button_group(new QButtonGroup)
{
    setOrientation(Qt::Vertical);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
    setStyleSheet("QToolBar{border-left:1px solid rgb(180,180,180);} "
                  "::separator{background:lightgray;height:0px;"
                  "margin-top:25px;margin-bottom:25px;};");
    setIconSize(QSize(GUI::Style::toolbar_icon_size, GUI::Style::toolbar_icon_size));

    //... Zoom / selection mode

    auto* panButton = new QToolButton;
    panButton->setIcon(QIcon(":/images/hand-right.svg"));
    panButton->setToolTip("Pan/zoom mode (space)\n"
                          "Drag axes with the mouse, use mouse wheel to zoom in/out");
    panButton->setCheckable(true);
    panButton->setChecked(true);
    addWidget(panButton);

    auto* resetViewButton = new QToolButton;
    resetViewButton->setDefaultAction(gActions->reset_view);
    addWidget(resetViewButton);

    addSeparator();

    auto* selectionButton = new QToolButton;
    selectionButton->setIcon(QIcon(":/images/maskeditor_arrow.svg"));
    selectionButton->setToolTip("Select/modify mask");
    selectionButton->setCheckable(true);
    addWidget(selectionButton);

    m_activity_button_group->addButton(panButton, Canvas2DMode::PAN_ZOOM);
    m_activity_button_group->addButton(selectionButton, Canvas2DMode::SELECTION);

    addSeparator();

    //... Projection tools

    auto* horizontalPrjnButton = new QToolButton;
    horizontalPrjnButton->setIcon(QIcon(":/images/maskeditor_horizontalline.svg"));
    horizontalPrjnButton->setToolTip("Create horizontal projection");
    horizontalPrjnButton->setCheckable(true);
    m_activity_button_group->addButton(horizontalPrjnButton, Canvas2DMode::HORIZONTAL_PRJN);
    addWidget(horizontalPrjnButton);

    auto* verticalPrjnButton = new QToolButton;
    verticalPrjnButton->setIcon(QIcon(":/images/maskeditor_verticalline.svg"));
    verticalPrjnButton->setToolTip("Create vertical projection");
    verticalPrjnButton->setCheckable(true);
    m_activity_button_group->addButton(verticalPrjnButton, Canvas2DMode::VERTICAL_PRJN);
    addWidget(verticalPrjnButton);

    addSeparator();

    //... Shape creation tools

    auto* roiButton = new QToolButton;
    roiButton->setIcon(QIcon(":/images/maskeditor_roi.svg"));
    roiButton->setToolTip("Create region of interest");
    roiButton->setCheckable(true);
    m_activity_button_group->addButton(roiButton, Canvas2DMode::ROI);
    addWidget(roiButton);

    //    auto* maskAllButton = new QToolButton;
    //    maskAllButton->setIcon(QIcon(":/images/maskeditor_maskall.svg"));
    //    maskAllButton->setToolTip("Create masked area covering whole detector plane\n"
    //                              "Will be placed beneath all masks. Only one instance is
    //                              allowed.");
    //    maskAllButton->setCheckable(true);
    //    m_activity_button_group->addButton(maskAllButton, Canvas2DMode::MASKALL);
    //    addWidget(maskAllButton);

    auto* rectangleButton = new QToolButton;
    rectangleButton->setIcon(QIcon(":/images/maskeditor_rectangle.svg"));
    rectangleButton->setToolTip("Create rectangle mask");
    rectangleButton->setCheckable(true);
    m_activity_button_group->addButton(rectangleButton, Canvas2DMode::RECTANGLE);
    addWidget(rectangleButton);

    auto* polygonButton = new QToolButton;
    polygonButton->setIcon(QIcon(":/images/maskeditor_polygon.svg"));
    polygonButton->setToolTip("Create polygon mask");
    m_activity_button_group->addButton(polygonButton, Canvas2DMode::POLYGON);
    polygonButton->setCheckable(true);
    addWidget(polygonButton);

    auto* ellipseButton = new QToolButton;
    ellipseButton->setIcon(QIcon(":/images/maskeditor_ellipse.svg"));
    ellipseButton->setToolTip("Create ellipse mask");
    ellipseButton->setCheckable(true);
    m_activity_button_group->addButton(ellipseButton, Canvas2DMode::ELLIPSE);
    addWidget(ellipseButton);

    auto* horizontalMaskButton = new QToolButton;
    horizontalMaskButton->setIcon(QIcon(":/images/maskeditor_horizontalline.svg"));
    horizontalMaskButton->setToolTip("Create horizontal line mask");
    horizontalMaskButton->setCheckable(true);
    m_activity_button_group->addButton(horizontalMaskButton, Canvas2DMode::HORIZONTAL_MASK);
    addWidget(horizontalMaskButton);

    auto* verticalMaskButton = new QToolButton;
    verticalMaskButton->setIcon(QIcon(":/images/maskeditor_verticalline.svg"));
    verticalMaskButton->setToolTip("Create vertical line mask");
    verticalMaskButton->setCheckable(true);
    m_activity_button_group->addButton(verticalMaskButton, Canvas2DMode::VERTICAL_MASK);
    addWidget(verticalMaskButton);

    connect(m_activity_button_group, &QButtonGroup::idClicked,
            [this] { emit activityChanged(currentActivity()); });

    addSeparator();

    //... Modifier and inspector tools

    auto* raiseButton = new QToolButton;
    addWidget(raiseButton);
    raiseButton->setDefaultAction(gActions->raise_mask);

    auto* lowerButton = new QToolButton;
    addWidget(lowerButton);
    lowerButton->setDefaultAction(gActions->lower_mask);

    addSeparator();

    auto* presentationButton = new QToolButton;
    presentationButton->setIcon(QIcon(":/images/maskeditor_lightbulb.svg"));
    presentationButton->setToolTip("Press and hold to see mask results.");
    addWidget(presentationButton);

    connect(presentationButton, &QToolButton::pressed, [this] { emit presentationChange(true); });
    connect(presentationButton, &QToolButton::released, [this] { emit presentationChange(false); });

    auto* propertyPanelButton = new QToolButton;
    propertyPanelButton->setDefaultAction(gActions->toggle_properties_panel);
    addWidget(propertyPanelButton);

    addSeparator();

    auto* saveButton = new QToolButton;
    saveButton->setDefaultAction(gActions->save_projections);
    addWidget(saveButton);
}

void Data2DToolbar::onChangeActivityRequest(Canvas2DMode::Flag mode)
{
    setCurrentActivity(mode);
    emit activityChanged(currentActivity());
}

//! Change activity only if current activity is one of drawing mode (horizontal, vertical
//! projections drawing).
void Data2DToolbar::onProjectionTabChange(Canvas2DMode::Flag mode)
{
    if (currentActivity() == Canvas2DMode::HORIZONTAL_PRJN
        || currentActivity() == Canvas2DMode::VERTICAL_PRJN)
        onChangeActivityRequest(mode);
}

Canvas2DMode::Flag Data2DToolbar::currentActivity() const
{
    return Canvas2DMode::Flag(m_activity_button_group->checkedId());
}

void Data2DToolbar::setCurrentActivity(Canvas2DMode::Flag mode)
{
    m_activity_button_group->button(mode)->setChecked(true);
}
