//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/SampleView/RealspaceBuilder.h
//! @brief     Defines class RealspaceBuilder
//!
//! @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)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_GUI_VIEW_SAMPLEVIEW_REALSPACEBUILDER_H
#define BORNAGAIN_GUI_VIEW_SAMPLEVIEW_REALSPACEBUILDER_H

#include "Img3D/View/Camera.h"

class Item3D;
class ItemWithParticles;
class LayerItem;
class SampleItem;
class ParticleLayoutItem;
struct SceneGeometry;

namespace Img3D {
class BuilderUtils;
class Model;
class Particle3DContainer;
} // namespace Img3D

class RealspaceBuilder {

public:
    RealspaceBuilder(std::function<QColor(const QString&)> fnColorFromMaterialName);
    ~RealspaceBuilder();

    void populate(Img3D::Model* model, Item3D* item, const SceneGeometry& sceneGeometry,
                  unsigned int& numParticles) const;

private:
    std::unique_ptr<Img3D::BuilderUtils> m_builderUtils;

    std::vector<Img3D::Particle3DContainer>
    particle3DContainerVector(const ParticleLayoutItem& layoutItem,
                              const Img3D::F3& origin = {}) const;

    void populateSample(Img3D::Model* model, const SampleItem& mlayerItem,
                        const SceneGeometry& sceneGeometry, unsigned int& numParticles,
                        const Img3D::F3& origin = {}) const;

    void populateLayer(Img3D::Model* model, const LayerItem& layerItem,
                       const SceneGeometry& sceneGeometry, unsigned int& numParticles,
                       const Img3D::F3& origin = {}, bool isTopLayer = false) const;

    void populateLayout(Img3D::Model* model, const ParticleLayoutItem& layoutItem,
                        const SceneGeometry& sceneGeometry, unsigned int& numParticles,
                        const Img3D::F3& origin = {}) const;

    Img3D::Particle3DContainer particlesFromItem(const ItemWithParticles& particleItem,
                                                 double total_abundance = 1,
                                                 const Img3D::F3& origin = {}) const;

    void translateContainer(Img3D::Model* model,
                            const Img3D::Particle3DContainer& particle3DContainer,
                            unsigned& numParticles, const Img3D::F3& lattice_position = {}) const;

    void populateParticlesInLattice(
        const std::vector<std::vector<double>>& lattice_positions,
        const std::vector<Img3D::Particle3DContainer>& particle3DContainer_vector,
        Img3D::Model* model, const SceneGeometry& sceneGeometry, unsigned& numParticles) const;
};

#endif // BORNAGAIN_GUI_VIEW_SAMPLEVIEW_REALSPACEBUILDER_H
