//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Data/Data2DItem.cpp
//! @brief     Implements class Data2DItem.
//!
//! @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/Data/Data2DItem.h"
#include "Base/Axis/Frame.h"
#include "Base/Axis/Scale.h"
#include "Base/Util/Assert.h"
#include "Device/Data/Datafield.h"
#include "GUI/Model/Axis/AmplitudeAxisItem.h"
#include "GUI/Model/Axis/BasicAxisItem.h"
#include "GUI/Model/Mask/MasksSet.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {

namespace Tag {

const QString BaseData("BaseData");
const QString Interpolation("Interpolation");
const QString ZAxis("ZAxis");

} // namespace Tag

} // namespace

Data2DItem::Data2DItem()
    : DataItem(M_TYPE)
    , m_is_interpolated(true)
    , m_z_axis(std::make_unique<AmplitudeAxisItem>())
    , m_masks(std::make_unique<MasksSet>())
    , m_prjns(std::make_unique<MasksSet>())
{
}

Data2DItem::~Data2DItem() = default;

void Data2DItem::setDatafield(const Datafield& data)
{
    ASSERT(data.rank() == 2);
    DataItem::setDatafield(data);
    updateAxesZoomLevel();
    updateDataRange();
}

double Data2DItem::xMin() const
{
    const double defaultXmin(0.0);
    return m_datafield ? m_datafield->axis(0).min() : defaultXmin;
}

double Data2DItem::xMax() const
{
    const double defaultXmax(1.0);
    return m_datafield ? m_datafield->axis(0).max() : defaultXmax;
}

double Data2DItem::yMin() const
{
    const double defaultYmin(0.0);
    return m_datafield ? m_datafield->axis(1).min() : defaultYmin;
}

double Data2DItem::yMax() const
{
    const double defaultYmax(1.0);
    return m_datafield ? m_datafield->axis(1).max() : defaultYmax;
}

double Data2DItem::lowerZ() const
{
    return zAxisItem()->min();
}

double Data2DItem::upperZ() const
{
    return zAxisItem()->max();
}

void Data2DItem::setZrange(double zmin, double zmax)
{
    if (lowerZ() == zmin && upperZ() == zmax)
        return;
    zAxisItem()->setMin(zmin);
    zAxisItem()->setMax(zmax);
    emit itemAxesRangeChanged();
}

void Data2DItem::copyZRangeFromItem(DataItem* sourceItem)
{
    const auto* source = dynamic_cast<Data2DItem*>(sourceItem);
    if (!source || source == this)
        return;
    setZrange(source->lowerZ(), source->upperZ());
}

bool Data2DItem::isZoomed() const
{
    return lowerX() > xMin() || upperX() < xMax() || lowerY() > yMin() || upperY() < yMax();
}

bool Data2DItem::isLog() const
{
    return zAxisItem()->isLogScale();
}

void Data2DItem::setLog(bool islog)
{
    zAxisItem()->setLogScale(islog);
}

void Data2DItem::setInterpolated(bool interp)
{
    m_is_interpolated = interp;
    emit interpolationChanged(interp);
}

bool Data2DItem::isZaxisLocked() const
{
    return m_z_axis->isLocked();
}

void Data2DItem::setZaxisLocked(bool state)
{
    return m_z_axis->setLocked(state);
}

std::vector<int> Data2DItem::axdims() const
{
    return {xSize(), ySize()};
}

//! Sets zoom range of X,Y axes, if it was not yet defined.

void Data2DItem::updateAxesZoomLevel()
{
    // set zoom range of x-axis to min, max values if it was not set already
    if (upperX() < lowerX())
        setXrange(xMin(), xMax());

    // set zoom range of y-axis to min, max values if it was not set already
    if (upperY() < lowerY())
        setYrange(yMin(), yMax());

    const int nx = static_cast<int>(m_datafield->axis(0).size());
    axItemX()->resize(nx);
    const int ny = static_cast<int>(m_datafield->axis(1).size());
    axItemY()->resize(ny);
}

//! Sets min,max values for z-axis, if axes is not locked, and ranges are not yet set.

void Data2DItem::updateDataRange()
{
    if (isZaxisLocked())
        return;

    computeDataRange();
    emit alignRanges();
}

void Data2DItem::computeDataRange()
{
    setZrange(dataRange().first, dataRange().second);
}

//! Init zmin, zmax to match the intensity values range.
std::pair<double, double> Data2DItem::dataRange() const
{
    const Datafield* data = c_field();

    const auto vec = data->flatVector();
    double min(*std::min_element(vec.cbegin(), vec.cend()));
    double max(*std::max_element(vec.cbegin(), vec.cend()));
    double logRange = pow(10, zAxisItem()->logRangeOrders());
    if (isLog()) {
        min = std::max(min, max / logRange);
        max *= 1.1;
    } else
        max *= 1.1;

    return {min, max};
}

void Data2DItem::resetView()
{
    if (!m_datafield)
        return;

    setAxesRangeToData();
    if (!isZaxisLocked())
        computeDataRange();
}

void Data2DItem::setAxesRangeToData()
{
    setXrange(xMin(), xMax());
    setYrange(yMin(), yMax());
}

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

    // parameters from base class
    w->writeStartElement(Tag::BaseData);
    DataItem::writeBaseTo(w);
    w->writeEndElement();

    // interpolation
    w->writeStartElement(Tag::Interpolation);
    XML::writeAttribute(w, XML::Attrib::value, m_is_interpolated);
    w->writeEndElement();

    // z axis
    w->writeStartElement(Tag::ZAxis);
    m_z_axis->writeTo(w);
    w->writeEndElement();
}

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

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

        // parameters from base class
        if (tag == Tag::BaseData) {
            DataItem::readBaseFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // interpolation
        } else if (tag == Tag::Interpolation) {
            XML::readAttribute(r, XML::Attrib::value, &m_is_interpolated);
            XML::gotoEndElementOfTag(r, tag);

            // z axis
        } else if (tag == Tag::ZAxis) {
            m_z_axis->readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}
