//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Device/ScanEditor.cpp
//! @brief     Defines class ScanEditor.
//!
//! @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/Device/ScanEditor.h"
#include "Base/Axis/Frame.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Axis/PointwiseAxisItem.h"
#include "GUI/Model/Beam/BeamWavelengthItem.h"
#include "GUI/Model/Beam/SourceItems.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sim/InstrumentsSet.h"
#include "GUI/View/Device/AlphaScanEditor.h"
#include "GUI/View/Device/DistributionEditor.h"
#include "GUI/View/Device/FootprintForm.h"
#include "GUI/View/Numeric/DoubleSpinBox.h"
#include <QFormLayout>
#include <QLineEdit>

ScanEditor::ScanEditor(QWidget* parent, InstrumentItem* instr_item, ScanItem* item,
                       bool allow_footprint, bool allow_distr)
    : CollapsibleGroupBox("Beam and scan parameters", parent, item->expandBeamParameters)
{
    auto* layout = new QVBoxLayout;
    body()->setLayout(layout);

    //... Intensity

    auto* form = new QFormLayout;
    layout->addLayout(form);
    form->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
    auto* intensity_spinbox = new DoubleSpinBox(&item->intensity());
    form->addRow("Intensity:", intensity_spinbox);
    intensity_spinbox->setToolTip("Correction factor for normalized intensity");

    //... Wavelength and angles of incidence

    auto* hLayout = new QHBoxLayout;
    hLayout->setAlignment(Qt::AlignLeft);
    layout->addLayout(hLayout);

    auto* wavelengthEditor = new DistributionEditor("Wavelength (nm)", MeanConfig{true},
                                                    GUI::DistributionsCategory::Symmetric, this,
                                                    item->wavelengthItem(), allow_distr);
    hLayout->addWidget(wavelengthEditor);

    auto* inclinationEditor = new AlphaScanEditor(this, item->grazingScanItem(), allow_distr);
    hLayout->addWidget(inclinationEditor);

    // In the following, signal order is important! Indicators have to be recalculated first,
    // then updated (recalculation is done in EditController)
    connect(wavelengthEditor, &DistributionEditor::distributionChanged,
            [instr_item, inclinationEditor] {
                // Update values in pointwise axis. This is necessary after a change of the mean
                // wavelength.
                if (auto* iiI = dynamic_cast<SpecularInstrumentItem*>(instr_item)) {
                    if (auto* pw = dynamic_cast<PointwiseAxisItem*>(
                            iiI->scanItem()->inclinationAxisItem())) {
                        const Frame frame = instr_item->makeFrame();
                        pw->updateAxIndicators(frame);
                    }
                }
                emit gDoc->instruments()->currentModified();
                emit inclinationEditor->updateIndicators();
            });

    connect(inclinationEditor, &AlphaScanEditor::dataChanged, [wavelengthEditor] {
        emit gDoc->instruments()->currentModified();
        emit wavelengthEditor->updateData();
    });

    //... Footprint

    if (allow_footprint) {
        auto* footprintEditor = new FootprintForm(this, item);
        hLayout->addWidget(footprintEditor);
        connect(footprintEditor, &FootprintForm::dataChanged, this, &ScanEditor::dataChanged);
    }
}
