//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Axis/BasicAxisItem.cpp
//! @brief     Implements various axis items.
//!
//! @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)
//
//  ************************************************************************************************

#include "GUI/Model/Axis/BasicAxisItem.h"
#include "Base/Axis/MakeScale.h"
#include "Base/Axis/Scale.h"
#include "Base/Const/Units.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {
namespace Tag {

const QString IsVisible("IsVisible");
const QString Nbins("Nbins");
const QString MinDeg("MinDeg");
const QString MaxDeg("MaxDeg");
const QString BaseData("BaseData");
const QString LockMinMax("LockMinMax");
const QString LogScale("LogScale");

} // namespace Tag
} // namespace

BasicAxisItem::BasicAxisItem(QObject* parent)
    : QObject(parent)
    , m_visible(true)
    , m_nbins(100)
    , m_min(0)
    , m_max(-1)
{
}

BasicAxisItem::~BasicAxisItem() = default;

void BasicAxisItem::resize(size_t value)
{
    m_nbins = int(value);
}

void BasicAxisItem::setMin(double value)
{
    m_min = value;
    emit axisRangeChanged();
}

void BasicAxisItem::setMax(double value)
{
    m_max = value;
    emit axisRangeChanged();
}

Scale BasicAxisItem::makeScale(std::string name) const
{
    return EquiScan(name, size(), Units::deg2rad(min()), Units::deg2rad(max()));
}

void BasicAxisItem::setVisible(bool b)
{
    m_visible = b;
    emit axisVisibilityChanged();
}

void BasicAxisItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeAttribute(w, XML::Attrib::version, uint(1));

    // visibility
    w->writeStartElement(Tag::IsVisible);
    XML::writeAttribute(w, XML::Attrib::value, m_visible);
    w->writeEndElement();

    // nbins
    w->writeStartElement(Tag::Nbins);
    XML::writeAttribute(w, XML::Attrib::value, m_nbins);
    w->writeEndElement();

    // min
    w->writeStartElement(Tag::MinDeg);
    XML::writeAttribute(w, XML::Attrib::value, m_min);
    w->writeEndElement();

    // max
    w->writeStartElement(Tag::MaxDeg);
    XML::writeAttribute(w, XML::Attrib::value, m_max);
    w->writeEndElement();
}

void BasicAxisItem::readFrom(QXmlStreamReader* r)
{
    const uint version = XML::readUIntAttribute(r, XML::Attrib::version);
    Q_UNUSED(version)

    while (r->readNextStartElement()) {
        QString tag = r->name().toString();

        // visibility?
        if (tag == Tag::IsVisible) {
            XML::readAttribute(r, XML::Attrib::value, &m_visible);
            XML::gotoEndElementOfTag(r, tag);

            // nbins
        } else if (tag == Tag::Nbins) {
            XML::readAttribute(r, XML::Attrib::value, &m_nbins);
            XML::gotoEndElementOfTag(r, tag);

            // min
        } else if (tag == Tag::MinDeg) {
            XML::readAttribute(r, XML::Attrib::value, &m_min);
            XML::gotoEndElementOfTag(r, tag);

            // max
        } else if (tag == Tag::MaxDeg) {
            XML::readAttribute(r, XML::Attrib::value, &m_max);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}
