//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Material/Material.h
//! @brief     Defines and implements class Material.
//!
//! @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_MATERIAL_MATERIAL_H
#define BORNAGAIN_SAMPLE_MATERIAL_MATERIAL_H

#include "Base/Type/Cloneable.h"
#include "Base/Vector/RotMatrix.h"
#include <heinz/Complex.h>
#include <heinz/Vectors3D.h>
#include <memory>
#include <string>
#include <vector>

class SpinMatrix;
class WavevectorInfo;

//! Display color with components in [0, 1].
struct MaterialColor {
    MaterialColor(double red, double green, double blue)
        : r(red)
        , g(green)
        , b(blue)
    {
    }

    double r;
    double g;
    double b;
};

//! Abstract base class for materials, characterized by scattering properties and magnetization.

class Material : public Cloneable {
public:
    //! Creates material with particular material implementation
    Material(std::string name, const MaterialColor& color, const R3& magnetization = {});

    virtual ~Material() = default;

#ifndef SWIG
    //! Sets the magnetization in A/m
    void setMagnetization(const R3& magnetization) { m_magnetization = magnetization; }

    virtual Material* clone() const = 0;
    std::unique_ptr<Material> cloned_unique() const { return std::unique_ptr<Material>(clone()); }

    std::string materialName() const { return m_name; }

    //! Returns the display color.
    MaterialColor color() const { return m_color; }

    //! Returns the magnetization in A/m
    R3 magnetization() const { return m_magnetization; }

    bool magnetized() const { return m_magnetization != R3(); }

    virtual bool hasNativeRefractiveIndex() const = 0;
    virtual bool hasNativeSLD() const = 0;
    virtual bool isVacuum() const = 0;

    //! Returns refractive index at given wavelength
    virtual complex_t refractiveIndex(double wavelength) const = 0;

    //! Returns squared refractive index at given wavelength
    virtual complex_t squaredRefractiveIndex(double wavelength) const = 0;

    //! Returns constant characteristic data
    virtual complex_t rawData() const = 0;
    virtual complex_t averageableData() const { return rawData(); }

    //! Returns SLD in Å⁻² at given wavelength (in nm). For SLDMaterial this is wavelength-
    //! independent; for RefractiveMaterial it's computed from the refractive index.
    virtual complex_t sld(double wavelength) const = 0;

    //! Returns (π/λ² − SLD(in nm⁻²))
    complex_t scalarSubtrSLD(const WavevectorInfo& wavevectors) const;

    //! Returns (π/λ² − SLD(in nm⁻²)) matrix with magnetization corrections
    SpinMatrix polarizedSubtrSLD(const WavevectorInfo& wavevectors) const;

    std::unique_ptr<Material> rotatedMaterial(const RotMatrix& transform) const;

private:
    //! Returns (π/λ² − SLD(in nm⁻²)) for given vacuum wavelength
    virtual complex_t plainSubtrSLD(double lambda0) const = 0;

    const std::string m_name;
    MaterialColor m_color;
    R3 m_magnetization;
#endif // SWIG
};

bool operator==(const Material& left, const Material& right);
bool operator!=(const Material& left, const Material& right);

#endif // BORNAGAIN_SAMPLE_MATERIAL_MATERIAL_H
