//  ************************************************************************************************
//
//  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::readUInt(r, XML::Attrib::version);
    Q_UNUSED(version)

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

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

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

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

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

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

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

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

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

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

        } else
            r->skipCurrentElement();
    }
}
