//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Beam/SourceItems.h
//! @brief     Defines BeamItem hierarchy.
//!
//! @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_MODEL_BEAM_SOURCEITEMS_H
#define BORNAGAIN_GUI_MODEL_BEAM_SOURCEITEMS_H

#include "GUI/Model/Beam/FootprintItemCatalog.h"
#include "GUI/Model/Descriptor/DoubleProperty.h"
#include "GUI/Model/Descriptor/PolyItem.h"
#include <functional>
#include <heinz/Vectors3D.h>

class BasicAxisItem;
class Beam;
class BeamAzimuthalAngleItem;
class BeamDistributionItem;
class BeamScan;
class BeamWavelengthItem;
class Frame;
class GrazingScanItem;
class IFootprint;
class Scale;

//! Base class for BeamItem and ScanItem. Name refers to radiation source.
class SourceItem {
public:
    DoubleProperty& intensity() { return m_intensity; }
    const DoubleProperty& intensity() const { return m_intensity; }
    void setIntensity(double v) { m_intensity.setValue(v); }

    double wavelength() const;
    void setWavelength(double value);
    BeamWavelengthItem* wavelengthItem() const;

    double azimuthalAngle() const;
    void setAzimuthalAngle(double value);
    BeamAzimuthalAngleItem* azimuthalAngleItem() const;

    bool expandBeamParameters = true;

    PolyItem<FootprintItemCatalog>& footprintSelection() { return m_footprint; }

    void setGaussianFootprint(double value);
    void setSquareFootprint(double value);

    void setFootprint(const IFootprint* footprint);

    bool expandFootprint = true;

protected:
    SourceItem();
    void writeTo(QXmlStreamWriter* w) const;
    void readFrom(QXmlStreamReader* r);

    std::unique_ptr<BeamWavelengthItem> m_wavelength_item;
    std::unique_ptr<BeamAzimuthalAngleItem> m_azimuthal_angle_item;

    DoubleProperty m_intensity;

    PolyItem<FootprintItemCatalog> m_footprint;
};

class BeamItem : public SourceItem {
public:
    BeamItem();

    void writeTo(QXmlStreamWriter* w) const;
    void readFrom(QXmlStreamReader* r);

    BeamDistributionItem* beamDistributionItem() const;
    void setInclinationAngle(double value);
    double getInclinationAngle() const;

    std::unique_ptr<Beam> createBeam() const;

private:
    std::unique_ptr<BeamDistributionItem> m_inclination_angle_item;
};

class ScanItem : public SourceItem {
public:
    ScanItem();

    void setScan(const BeamScan* scan);

    void writeTo(QXmlStreamWriter* w) const;
    void readFrom(QXmlStreamReader* r);

    GrazingScanItem* grazingScanItem() const;
    BasicAxisItem* inclinationAxisItem() const;

    void updateToData(const Scale& axis);

private:
    std::unique_ptr<GrazingScanItem> m_grazing_scan_item;
};

#endif // BORNAGAIN_GUI_MODEL_BEAM_SOURCEITEMS_H
