//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/ToCore/SimulationToCore.cpp
//! @brief     Implements class DomainSimulationBuilder
//!
//! @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/ToCore/SimulationToCore.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Device/InstrumentItems.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/ToCore/SampleToCore.h"
#include "GUI/Support/Data/SimulationOptionsItem.h"
#include "Resample/Options/SimulationOptions.h"
#include "Sample/Multilayer/MultiLayer.h"
#include "Sim/Simulation/ISimulation.h"

namespace {

void setSimulationOptions(ISimulation* simulation, const SimulationOptionsItem& item)
{
    simulation->options().setNumberOfThreads(item.numberOfThreads());
    if (item.useMonteCarloIntegration())
        simulation->options().setMonteCarloIntegration(true, item.numberOfMonteCarloPoints());
    simulation->options().setUseAvgMaterials(item.useAverageMaterials());
    simulation->options().setIncludeSpecular(item.includeSpecularPeak());
}

} // namespace


std::unique_ptr<ISimulation>
GUI::ToCore::itemsToSimulation(const SampleItem& sampleItem, const InstrumentItem& instrumentItem,
                               const SimulationOptionsItem& optionsItem)
{
    std::unique_ptr<MultiLayer> sample = GUI::ToCore::itemToSample(sampleItem);
    std::unique_ptr<ISimulation> result(instrumentItem.createSimulation(*sample));

    setSimulationOptions(result.get(), optionsItem);

    return result;
}
