//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Sample/SelectionContainerForm.h
//! @brief     Defines class SelectionContainerForm, and implements templated functions.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_GUI_VIEW_SAMPLE_SELECTIONCONTAINERFORM_H
#define BORNAGAIN_GUI_VIEW_SAMPLE_SELECTIONCONTAINERFORM_H

#include "GUI/Model/Descriptor/PolyItem.h"
#include "GUI/View/Base/CustomEventFilters.h"
#include "GUI/View/Sample/LayerEditorUtil.h"
#include "GUI/View/Sample/SampleEditorController.h"
#include <QComboBox>
#include <QGridLayout>

//! Abstract widget base class to contain a selection, defined by a SelectionDescriptor.
//!
//! This abstract base class contains only the selection combo box and the infrastructure to
//! call the SampleEditorController to change the current selection. A derived class has to
//! implement the showing of the contents (method createContent).
class ISelectionContainerForm : public QWidget {
public:
    virtual void createContent() = 0;

protected:
    ISelectionContainerForm(QWidget* parent, SampleEditorController* ec);

    template <typename T> void initUI(PolyItem<T>& d)
    {
        m_grid_layout = new QGridLayout(this);
        m_grid_layout->setContentsMargins(0, 0, 0, 0);
        m_grid_layout->setSpacing(6);

        m_combo = new QComboBox;
        WheelEventEater::install(m_combo);
        m_combo->addItems(d.options());
        m_combo->setCurrentIndex(d.currentIndex());
        m_combo->setMaxVisibleItems(m_combo->count());

        QObject::connect(m_combo, &QComboBox::currentIndexChanged, [this, &d](int current) {
            clear();
            d.setCurrentIndex(current);
            createContent();
            emit m_ec->modified();
        });

        m_grid_layout->addWidget(m_combo, 1, 0);
        createContent();
    }

private:
    //! Remove all properties from the layout
    void clear();

protected:
    QGridLayout* m_grid_layout;
    QComboBox* m_combo;
    SampleEditorController* m_ec;
};

//! A widget to contain a selection, defined by a PolyItem.
//!
//! This SelectionContainerForm is limited to contain the selection combo box and a list of double
//! values represented by DoubleProperties. The list of DoubleProperties is queried by calling
//! GUI::Util::Layer::doublePropertiesOfItem(). To have the correct DoubleProperties on this form,
//! you may have to overload this method according to your class and your needs. The overload will
//! expect the template type you defined for PolyItem.
//!
//! Example:
//! PolyItem<RotationItemCatalog>  =>
//! GUI::Util::Layer::doublePropertiesOfItem(RotationItem*)
//!
//! The connection from selection combo -> PolyItem is made via
//! SampleEditorController::setCurrentIndex(), where a command can be used for undo purposes.
//!
//! For each DoubleProperty, a unit-aware DoubleSpinBox is created. The connection from each
//! spinbox to the DoubleProperty is made via SampleEditorController::setDouble(), where a command
//! can be used for undo purposes.
//!
//! If a more complex selection shall be realized (e.g. with
//! sub-selections or different value types), this class is not sufficient.
class SelectionContainerForm : public ISelectionContainerForm {
public:
    template <typename T>
    SelectionContainerForm(QWidget* parent, PolyItem<T>& d, SampleEditorController* ec)
        : ISelectionContainerForm(parent, ec)
    {
        currentValues = [&d] { return doublePropertiesOfItem(d.certainItem()); };
        initUI(d);
    }

    virtual void createContent();

private:
    static DoubleProperties doublePropertiesOfItem(RoughnessItem* item);
    static DoubleProperties doublePropertiesOfItem(FormFactorItem* item);
    static DoubleProperties doublePropertiesOfItem(Profile1DItem* item);
    static DoubleProperties doublePropertiesOfItem(Profile2DItem* item);
    static DoubleProperties doublePropertiesOfItem(RotationItem* item);

    std::function<DoubleProperties()> currentValues = nullptr;
};

#endif // BORNAGAIN_GUI_VIEW_SAMPLE_SELECTIONCONTAINERFORM_H
