//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Sim/SimulationOptionsItem.cpp
//! @brief     Defines class SimulationOptionsItem.
//!
//! @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/Sim/SimulationOptionsItem.h"
#include "GUI/Model/Util/UtilXML.h"
#include <thread>

namespace {
namespace Tag {

const QString RunImmediately("RunImmediately");
const QString NumberOfThreads("NumberOfThreads");
const QString Analytical("Analytical");
const QString NumberOfMonteCarloPoints("NumberOfMonteCarloPoints");
const QString UseAverageMaterials("UseAverageMaterials");
const QString IncludeSpecularPeak("IncludeSpecularPeak");
const QString UseFastMesoAlgorithm("UseFastMesoAlgorithm");
const QString MesoCutoffRadius("MesoCutoffRadius");
const QString ExpandAdvancedOptionsGroupbox("ExpandAdvancedOptionsGroupbox");

} // namespace Tag
} // namespace


SimulationOptionsItem::SimulationOptionsItem()
    : m_number_of_threads(std::thread::hardware_concurrency())
{
}

bool SimulationOptionsItem::useMonteCarloIntegration() const
{
    return !useAnalytical();
}

void SimulationOptionsItem::setUseMonteCarloIntegration(unsigned numberOfPoints)
{
    m_computation_method_analytical = false;
    m_number_of_monte_carlo_points = numberOfPoints;
}

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

    // run immediately?
    w->writeStartElement(Tag::RunImmediately);
    XML::writeAttribute(w, XML::Attrib::value, m_run_immediately);
    w->writeEndElement();

    // number of threads
    w->writeStartElement(Tag::NumberOfThreads);
    XML::writeAttribute(w, XML::Attrib::value, m_number_of_threads);
    w->writeEndElement();

    // computation method
    w->writeStartElement(Tag::Analytical);
    XML::writeAttribute(w, XML::Attrib::value, m_computation_method_analytical);
    w->writeEndElement();

    // number of Monte Carlo points
    w->writeStartElement(Tag::NumberOfMonteCarloPoints);
    XML::writeAttribute(w, XML::Attrib::value, m_number_of_monte_carlo_points);
    w->writeEndElement();

    // use average materials
    w->writeStartElement(Tag::UseAverageMaterials);
    XML::writeAttribute(w, XML::Attrib::value, m_use_average_materials);
    w->writeEndElement();

    // include specular peak
    w->writeStartElement(Tag::IncludeSpecularPeak);
    XML::writeAttribute(w, XML::Attrib::value, m_include_specular_peak);
    w->writeEndElement();

    // fast mesocrystal computation
    w->writeStartElement(Tag::UseFastMesoAlgorithm);
    XML::writeAttribute(w, XML::Attrib::value, m_use_meso_reciprocal_sum);
    w->writeEndElement();

    // mesocrystal cutoff radius
    w->writeStartElement(Tag::MesoCutoffRadius);
    XML::writeAttribute(w, XML::Attrib::value, m_meso_radius_factor);
    w->writeEndElement();

    // advanced options groupbox: is expanded?
    w->writeStartElement(Tag::ExpandAdvancedOptionsGroupbox);
    XML::writeAttribute(w, XML::Attrib::value, expandAdvancedOptions);
    w->writeEndElement();
}

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

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

        // run immediately?
        if (tag == Tag::RunImmediately) {
            XML::readAttribute(r, XML::Attrib::value, &m_run_immediately);
            XML::gotoEndElementOfTag(r, tag);

            // number of threads
        } else if (tag == Tag::NumberOfThreads) {
            XML::readAttribute(r, XML::Attrib::value, &m_number_of_threads);
            XML::gotoEndElementOfTag(r, tag);

            // computation method
        } else if (tag == Tag::Analytical) {
            XML::readAttribute(r, XML::Attrib::value, &m_computation_method_analytical);
            XML::gotoEndElementOfTag(r, tag);

            // number of Monte Carlo points
        } else if (tag == Tag::NumberOfMonteCarloPoints) {
            XML::readAttribute(r, XML::Attrib::value, &m_number_of_monte_carlo_points);
            XML::gotoEndElementOfTag(r, tag);

            // use average materials
        } else if (tag == Tag::UseAverageMaterials) {
            XML::readAttribute(r, XML::Attrib::value, &m_use_average_materials);
            XML::gotoEndElementOfTag(r, tag);

            // include specular peak
        } else if (tag == Tag::IncludeSpecularPeak) {
            XML::readAttribute(r, XML::Attrib::value, &m_include_specular_peak);
            XML::gotoEndElementOfTag(r, tag);

            // fast mesocrystal computation
        } else if (tag == Tag::UseFastMesoAlgorithm) {
            XML::readAttribute(r, XML::Attrib::value, &m_use_meso_reciprocal_sum);
            XML::gotoEndElementOfTag(r, tag);

            // mesocrystal cutoff radius
        } else if (tag == Tag::MesoCutoffRadius) {
            XML::readAttribute(r, XML::Attrib::value, &m_meso_radius_factor);
            XML::gotoEndElementOfTag(r, tag);

            // advanced options groupbox: is expanded?
        } else if (tag == Tag::ExpandAdvancedOptionsGroupbox) {
            XML::readAttribute(r, XML::Attrib::value, &expandAdvancedOptions);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}
