//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Base/Axis/Scale.h
//! @brief     Defines interface Scale.
//!
//! @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_BASE_AXIS_SCALE_H
#define BORNAGAIN_BASE_AXIS_SCALE_H

#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>

using trafo_t = std::function<double(double)>;

class Bin1D;
class Coordinate;

//! An axis, with coordinate and bins.

class Scale {
public:
    Scale(const Coordinate& coord, const std::vector<Bin1D>& bins);
    Scale(const Scale& other);
    ~Scale();

    double binCenter(size_t i) const;
    std::vector<double> binCenters() const;

    size_t size() const; //!< Number of bins

    double min() const; //!< minimum value in first bin
    double max() const; //!< maximum value in last bin

    std::pair<double, double> bounds() const { return {min(), max()}; }
    bool rangeComprises(double value) const { return value >= min() && value < max(); }
    double span() const { return max() - min(); }

    std::string axisLabel() const;

#ifndef SWIG
    Scale* clone() const;

    const Bin1D& bin(size_t i) const;
    const std::vector<Bin1D>& bins() const { return m_bins; }

    //! find bin index which is best match for given value
    size_t closestIndex(double value) const;

    bool isEquiDivision() const;
    bool isEquiScan() const;
    bool isScan() const;

    Scale clipped(double lower, double upper) const;
    Scale clipped(std::pair<double, double> bounds) const;

    bool operator==(const Scale& other) const;

    friend std::ostream& operator<<(std::ostream& ostr, const Scale& ax);

    std::string coordName() const;
    std::string unit() const;

    std::unique_ptr<Scale> plottableScale() const;
    std::unique_ptr<Scale> transformedScale(const Coordinate& coord, const trafo_t& axTrafo) const;
    std::unique_ptr<Scale> reversedScale() const;

protected:
    std::vector<Bin1D> m_bins;
    std::unique_ptr<Coordinate> m_coord;
#endif // SWIG
};

#endif // BORNAGAIN_BASE_AXIS_SCALE_H
