//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Particle/Compound.h
//! @brief     Defines class Compound.
//!
//! @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_SAMPLE_PARTICLE_COMPOUND_H
#define BORNAGAIN_SAMPLE_PARTICLE_COMPOUND_H

#include "Sample/Particle/IParticle.h"
#include <memory>
#include <optional>
#include <vector>

//! A composition of particles at fixed positions

class Compound : public IParticle {
public:
    Compound();
    ~Compound() override;

    void addComponent(const IParticle& particle);
    void addComponent(const IParticle& particle, const R3& position);
    void addComponents(const IParticle& particle, std::vector<R3> positions);

#ifndef SWIG
    //! Takes ownership of particle without cloning (preserves calculation context).
    void addComponent(std::unique_ptr<IParticle> particle);
#endif

    void setRadius(double radius) { m_radius = radius; }

#ifndef SWIG
    Compound* clone() const override;

    //! Flattens the tree till other types: Particle, CoreAndShell, Mixture, Mesocrystal
    std::vector<std::unique_ptr<IParticle>> decompose() const override;

    //! Returns number of component particles
    size_t nbrComponents() const { return m_particles.size(); }

    //! Returns component particles (for GUI enumeration; NOT for scattering - use
    //! IParticle::particles())
    std::vector<const IParticle*> components() const;

    void setSliceNumber(size_t n) override;

    double bottomZ() const override;
    double topZ() const override;
    double volumeToAdmix() const override;
    std::unique_ptr<const Material> materialToAdmix() const override;
    bool hasRadius() const override { return m_radius.has_value(); }
    double radius() const override;
    double radialExtension() const override;
    bool containsSoftParticle() const override;

    complex_t theFF(const WavevectorInfo& wavevectors) const override;
    SpinMatrix thePolFF(const WavevectorInfo& wavevectors) const override;

    ScatteringMoments scatteringMoments(const DiffuseElement& ele) const override;
    PolScatteringMoments polScatteringMoments(const DiffuseElement& ele) const override;
    ChannelMoments channelMoments(const DwbaChannels& channels) const override;
    PolChannelMoments polChannelMoments(const PolDwbaChannels& channels) const override;

private:
    std::vector<std::unique_ptr<IParticle>> m_particles;
    std::optional<double> m_radius;
#endif // SWIG
};

#endif // BORNAGAIN_SAMPLE_PARTICLE_COMPOUND_H
