//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Sample/SampleItem.cpp
//! @brief     Implements class SampleItem.
//!
//! @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/Model/Sample/SampleItem.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Type/PredefinedColors.h"
#include "GUI/Model/Util/Backup.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {
namespace Tag {

const QString Name("Name");
const QString Description("Description");
const QString CrossCorrelationLength("CrossCorrelationLength");
const QString MaterialsSet("MaterialsSet");
const QString Layer("Layer");
const QString ExternalField("ExternalField");
const QString ExpandInfoGroupbox("ExpandInfoGroupbox");

} // namespace Tag
} // namespace

SampleItem::SampleItem()
    : NamedItem("Sample")
{
    m_cross_correlation_length.init("Cross-correlation length (nm)",
                                    "Cross correlation length of roughnesses between interfaces",
                                    0.0, 5, RealLimits::nonnegative(), "cross");
    m_external_field.init("External field", "External field (A/m)", "extField");
}

SampleItem::~SampleItem() = default;

SampleItem* SampleItem::clone() const
{
    auto* result = new SampleItem;
    GUI::Util::copyContents(this, result);
    return result;
}

std::vector<ItemWithMaterial*> SampleItem::itemsWithMaterial() const
{
    std::vector<ItemWithMaterial*> result;
    for (LayerItem* layer : m_layers)
        Vec::concat(result, layer->itemsWithMaterial());
    return result;
}

void SampleItem::addStandardMaterials()
{
    // add only non-existing materials
    QString name = materialMap.key(DefaultMaterials::Default);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 1e-3, 1e-5);

    name = materialMap.key(DefaultMaterials::Vacuum);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 0.0, 0.0);

    name = materialMap.key(DefaultMaterials::Particle);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 6e-4, 2e-8);

    name = materialMap.key(DefaultMaterials::Core);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 2e-4, 1e-8);

    name = materialMap.key(DefaultMaterials::Substrate);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 6e-6, 2e-8);
}

LayerItem* SampleItem::createLayerItemAt(int index)
{
    if (index < 0)
        index = m_layers.size();

    auto* layer = new LayerItem(&m_materials);
    m_layers.insert_at(index, layer);
    updateTopBottom();
    return layer;
}

void SampleItem::updateTopBottom()
{
    for (LayerItem* l : m_layers) {
        l->setIsTopLayer(l == m_layers.front());
        l->setIsBottomLayer(l == m_layers.back());
    }
}

void SampleItem::removeLayer(LayerItem* layer)
{
    m_layers.delete_element(layer);
    updateTopBottom();
}

void SampleItem::moveLayer(LayerItem* layer, LayerItem* aboveThisLayer)
{
    if (layer == aboveThisLayer)
        return;

    int currentIndex = Vec::indexOfPtr(layer, layerItems());
    m_layers.release_at(currentIndex);

    int destIndex = m_layers.size();
    if (aboveThisLayer != nullptr)
        destIndex = Vec::indexOfPtr(aboveThisLayer, layerItems());
    m_layers.insert_at(destIndex, layer);

    updateTopBottom();
}

void SampleItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeAttribute(w, XML::Attrib::version, uint(1));

    // name
    w->writeStartElement(Tag::Name);
    XML::writeAttribute(w, XML::Attrib::value, name());
    w->writeEndElement();

    // description
    w->writeStartElement(Tag::Description);
    XML::writeAttribute(w, XML::Attrib::value, description());
    w->writeEndElement();

    // cross-correlation length
    w->writeStartElement(Tag::CrossCorrelationLength);
    m_cross_correlation_length.writeTo(w);
    w->writeEndElement();

    // exernal field
    w->writeStartElement(Tag::ExternalField);
    m_external_field.writeTo(w);
    w->writeEndElement();

    // materials
    w->writeStartElement(Tag::MaterialsSet);
    m_materials.writeTo(w);
    w->writeEndElement();

    // layers
    for (const auto* layer : m_layers) {
        w->writeStartElement(Tag::Layer);
        layer->writeTo(w);
        w->writeEndElement();
    }

    // info groupbox: is expanded?
    w->writeStartElement(Tag::ExpandInfoGroupbox);
    XML::writeAttribute(w, XML::Attrib::value, expandInfo);
    w->writeEndElement();
}

void SampleItem::readFrom(QXmlStreamReader* r)
{
    const uint version = XML::readUIntAttribute(r, XML::Attrib::version);
    Q_UNUSED(version)

    m_layers.clear();

    while (r->readNextStartElement()) {
        QString tag = r->name().toString();

        // sample name
        if (tag == Tag::Name) {
            setName(XML::readAttributeString(r, XML::Attrib::value));
            XML::gotoEndElementOfTag(r, tag);

            // description
        } else if (tag == Tag::Description) {
            setDescription(XML::readAttributeString(r, XML::Attrib::value));
            XML::gotoEndElementOfTag(r, tag);

            // cross-correlation length
        } else if (tag == Tag::CrossCorrelationLength) {
            m_cross_correlation_length.readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // exernal field
        } else if (tag == Tag::ExternalField) {
            m_external_field.readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // materials
        } else if (tag == Tag::MaterialsSet || tag == "MaterialModel" /*v<=21*/) {
            m_materials.readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // layers
        } else if (tag == Tag::Layer) {
            createLayerItemAt()->readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // info groupbox: is expanded?
        } else if (tag == Tag::ExpandInfoGroupbox) {
            XML::readAttribute(r, XML::Attrib::value, &expandInfo);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}

void SampleItem::updateDefaultLayerColors()
{
    const auto& colors = GUI::Colors::layerDefaults();

    int col = 0;
    for (auto* l : layerItems()) {
        if (!l->color().isValid())
            l->setColor(colors[col]);
        col = (col + 1) % colors.size();
    }
}
