//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/SampleDesigner/SampleEditor.cpp
//! @brief     Implements class SampleEditor
//!
//! @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)
//
//  ************************************************************************************************

#include "GUI/View/SampleDesigner/SampleEditor.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/View/SampleDesigner/LayerEditorUtil.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
#include "GUI/View/SampleDesigner/SampleForm.h"
#include "GUI/View/Widget/ApplicationSettings.h"
#include "GUI/View/Widget/StyledToolbar.h"
#include <QActionGroup>
#include <QBoxLayout>
#include <QPushButton>
#include <QScrollArea>

SampleEditor::SampleEditor(QWidget* parent, ProjectDocument* document)
    : QWidget(parent)
    , m_currentSampleWidget(nullptr)
    , m_currentSample(nullptr)
    , m_document(document)
{
    m_scrollArea = new QScrollArea(this);
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setWidget(createEmptyWidget());

    auto* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(m_scrollArea);
}

SampleEditor::~SampleEditor()
{
    qDeleteAll(m_editControllers.values());
}

void SampleEditor::setCurrentSample(SampleItem* sampleItem)
{
    if (m_currentSample != nullptr)
        m_editControllers[m_currentSample]->setSampleForm(nullptr);

    m_currentSampleWidget = nullptr;
    delete m_scrollArea->takeWidget();

    m_currentSample = sampleItem;
    if (m_currentSample == nullptr) {
        m_scrollArea->setWidget(createEmptyWidget());
        return;
    }

    if (!m_editControllers.contains(m_currentSample))
        m_editControllers.insert(m_currentSample,
                                 new SampleEditorController(m_document, m_currentSample));
    auto* ec = m_editControllers[m_currentSample];
    connect(ec, &SampleEditorController::requestViewInRealspace, this,
            &SampleEditor::requestViewInRealspace);
    connect(ec, &SampleEditorController::aboutToRemoveItem, this, &SampleEditor::aboutToRemoveItem);
    connect(ec, &SampleEditorController::modified, this, &SampleEditor::modified);

    createLayerColors();

    m_currentSampleWidget = new SampleForm(this, m_currentSample, ec);
    ec->setSampleForm(m_currentSampleWidget);
    m_currentSampleWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    m_scrollArea->setWidget(m_currentSampleWidget);
}

void SampleEditor::createLayerColors() // #baLayerEditor move to better place
{
    if (!m_currentSample)
        return;

    int col = 0;
    for (auto* l : m_currentSample->layerItems()) {
        if (l->color().isValid())
            continue;

        l->setColor(LayerEditorUtil::predefinedLayerColors()[col]);
        col++;
        if (col == LayerEditorUtil::predefinedLayerColors().size())
            col = 0;
    }
}

QWidget* SampleEditor::createEmptyWidget()
{
    auto* emptyWidget = new QWidget(this);

    auto* btn = new QPushButton("Create sample", emptyWidget);
    connect(btn, &QPushButton::clicked, this, &SampleEditor::requestCreateNewSample);

    auto* layout = new QHBoxLayout(emptyWidget);
    layout->setContentsMargins(10, 20, 10, 20);
    layout->addStretch();
    layout->addWidget(btn);
    layout->addStretch();
    layout->setAlignment(Qt::AlignTop);

    return emptyWidget;
}