//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/SampleDesigner/SampleEditorCommands.cpp
//! @brief     Implements command classes for LayerOrientedSampleEditor
//!
//! @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/SampleEditorCommands.h"

#include "GUI/Model/Item/LayerItem.h"
#include "GUI/Model/Item/MultiLayerItem.h"
#include "GUI/Model/XML/Serializer.h"
#include "GUI/View/SampleDesigner/MultiLayerForm.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
#include <utility>

namespace {

constexpr int COMMAND_ID_CHANGE_VALUE = 11;
constexpr auto TagBackup = "backup";

template <typename ItemType> QByteArray serialize(ItemType& t)
{
    QByteArray a;
    QXmlStreamWriter w(&a);
    w.writeStartElement(TagBackup);
    Serializer s(&w);
    t.serialize(s);
    w.writeEndElement();
    return a;
}

template <typename ItemType> void deserialize(ItemType* t, const QByteArray& a)
{
    QXmlStreamReader r(a);
    Serializer sr(&r);
    r.readNextStartElement();
    ASSERT(r.name().toString() == TagBackup);
    t->serialize(sr);
}


} // namespace


CommandRemoveLayer::CommandRemoveLayer(SampleEditorController* ec, LayerItem* layerItem,
                                       QUndoCommand* parent /*= nullptr*/)
    : QUndoCommand(parent)
    , m_ec(ec)
{
    m_indexOfLayer = ec->multiLayerItem()->layers().indexOf(layerItem);
    m_layerItemBackup = serialize(*layerItem);
}

void CommandRemoveLayer::redo()
{
    auto* layer = m_ec->multiLayerItem()->layers()[m_indexOfLayer];
    emit m_ec->aboutToRemoveItem(layer);
    m_ec->multiLayerForm()->onAboutToRemoveLayer(layer);
    m_ec->multiLayerItem()->removeLayer(layer);
    m_ec->multiLayerForm()->updateRowVisibilities();
    emit m_ec->modified();
}

void CommandRemoveLayer::undo()
{
    LayerItem* restoredLayer = m_ec->multiLayerItem()->addLayer(m_indexOfLayer);
    deserialize(restoredLayer, m_layerItemBackup);
    m_ec->multiLayerForm()->onLayerAdded(restoredLayer);
    emit m_ec->modified();
}


CommandChangeValue::CommandChangeValue(const QString& label, SampleEditorController* ec,
                                       double oldValue, double newValue, QString path,
                                       QUndoCommand* parent /*= nullptr*/)
    : QUndoCommand(parent)
    , m_ec(ec)
    , m_oldValue(oldValue)
    , m_newValue(newValue)
    , m_path(std::move(path))
{
    setText("change " + label + "\n");
}

int CommandChangeValue::id() const
{
    return COMMAND_ID_CHANGE_VALUE;
}

bool CommandChangeValue::mergeWith(const QUndoCommand* command)
{
    if (command->id() != id()) // make sure other is also an AppendText command
        return false;

    const auto* const other = dynamic_cast<const CommandChangeValue*>(command);

    if (m_path != other->m_path)
        return false;

    m_newValue = other->m_newValue;
    return true;
}

void CommandChangeValue::redo()
{
    if (m_isFirst)
        m_isFirst = false;
    else
        m_ec->setDoubleFromUndo(m_newValue, m_path);
}

void CommandChangeValue::undo()
{
    m_ec->setDoubleFromUndo(m_oldValue, m_path);
}