//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Tune/FitSuiteItem.cpp
//! @brief     Implements class FitSuiteItem.
//!
//! @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/Tune/FitSuiteItem.h"
#include "GUI/Model/Mini/MinimizerItems.h"
#include "GUI/Model/Tune/FitParameterContainerItem.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {
namespace Tag {

const QString UpdateInterval("UpdateInterval");
const QString IterationsCount("IterationsCount");
const QString Chi2("Chi2");

const QString MinimizerContainer("MinimizerContainer");
const QString FitParameterContainer("FitParameterContainer");

} // namespace Tag
} // namespace

FitSuiteItem::FitSuiteItem()
    : m_update_interval(10)
    , m_iter_count(0)
    , m_chi2(0.0)
    , m_fit_container(std::make_unique<FitParameterContainerItem>())
    , m_minimizer_container(std::make_unique<MinimizerContainerItem>())
{
    setObjectName("FitSuite");
}

FitSuiteItem::~FitSuiteItem() = default;

void FitSuiteItem::setUpdateInterval(int interval)
{
    m_update_interval = interval;
    emit updateIntervalChanged(interval);
}

void FitSuiteItem::setIterationCount(int count)
{
    m_iter_count = count;
    emit iterationCountChanged(count);
}

void FitSuiteItem::setChi2(const double chi2)
{
    m_chi2 = chi2;
}

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

    // update interval
    w->writeStartElement(Tag::UpdateInterval);
    XML::writeAttribute(w, XML::Attrib::value, m_update_interval);
    w->writeEndElement();

    // iterations count
    w->writeStartElement(Tag::IterationsCount);
    XML::writeAttribute(w, XML::Attrib::value, m_iter_count);
    w->writeEndElement();

    // chi2
    w->writeStartElement(Tag::Chi2);
    XML::writeAttribute(w, XML::Attrib::value, m_chi2);
    w->writeEndElement();

    // minimizer container
    if (m_minimizer_container) {
        w->writeStartElement(Tag::MinimizerContainer);
        m_minimizer_container->writeTo(w);
        w->writeEndElement();
    }

    // fit parameter container
    if (m_fit_container) {
        w->writeStartElement(Tag::FitParameterContainer);
        m_fit_container->writeTo(w);
        w->writeEndElement();
    }
}

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

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

        // update interval
        if (tag == Tag::UpdateInterval) {
            XML::readAttribute(r, XML::Attrib::value, &m_update_interval);
            XML::gotoEndElementOfTag(r, tag);

            // iterations count
        } else if (tag == Tag::IterationsCount) {
            XML::readAttribute(r, XML::Attrib::value, &m_iter_count);
            XML::gotoEndElementOfTag(r, tag);

            // chi2
        } else if (tag == Tag::Chi2) {
            XML::readAttribute(r, XML::Attrib::value, &m_chi2);
            XML::gotoEndElementOfTag(r, tag);

            // minimizer container
        } else if (tag == Tag::MinimizerContainer) {
            m_minimizer_container->readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // fit parameter container
        } else if (tag == Tag::FitParameterContainer) {
            m_fit_container->readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}
