// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Data object linking the vertical axis window with the core module performing the calculations
#include "yaxisqml.h"
#include "auxiliary.h"
#include <QMessageBox>
#include <QLocale>
#include <QString>
#include <QLocale>
#include "math.h"

YAxisQml::YAxisQml(QObject *parent, BytranCore *inCore) : QObject(parent)
{
    // Passing in the core object
    core = inCore;

    // Setting the currently displayed value
    m_displayedValue = this->core->getYAxis()->getDisplayedValue();

    // So that commas are not inserted into numbers
    loc.setNumberOptions(QLocale::OmitGroupSeparator);

    // Transfering the minimum to the QML interface component
    m_minimum = loc.toString(core->getYAxis()->getMinYPlot(), 'g', 6);

    // Transfering the maximum to the QML interface component
    m_maximum = loc.toString(core->getYAxis()->getMaxYPlot(), 'g', 6);

    // Saving units
    m_units = core->getYAxis()->getYPlotUnits();

    // Auto recalculation of the minimum and maximum scale values for the cros-secions and radiance
    m_crossSectionAutoRecalc = this->core->getYAxis()->getCrossSectionAutoRecalc();
    m_radianceAutoRecalc = this->core->getYAxis()->getRadianceAutoRecalc();

    // Setting the enable rulers variable state
    m_enableRulers = core->getYAxis()->getEnableRulers();

    // Connections
    connect(this->core, SIGNAL(valuesUpdated()), this, SLOT(setUpdatedValues()));
}

YAxisQml::YAxisQml(const QString &minimum, const QString &maximum, const int &units, QObject *parent)
    : QObject(parent), m_units(units), m_minimum(minimum), m_maximum(maximum)
{
    ;
}

// This method is used to update the maximum scale value
void YAxisQml::updateScaleValues()
{
    emit minimumChanged();
    emit maximumChanged();
}

// Slot is used to update all values in the corresponding QML dialog
void YAxisQml::setUpdatedValues()
{
    emit unitsChanged();
    emit displayedValueChanged();
    emit minimumChanged();
    emit maximumChanged();
}

// Displayed units
quint8 YAxisQml::displayedValue() const { return this->core->getYAxis()->getDisplayedValue(); }
void YAxisQml::setDisplayedValue(const quint8 &displayedValue)
{
    if (displayedValue != m_displayedValue)
    {
        m_displayedValue = displayedValue;
        core->getYAxis()->setDisplayedValue(displayedValue);
        emit displayedValueChanged();
        emit minimumChanged();
        emit maximumChanged();
    }
}

QString YAxisQml::minimum() const { return loc.toString(core->getYAxis()->getMinYPlot(), 'g', 6); }
void YAxisQml::setMinimum(const QString &minimum)
{
    if ((minimum != m_minimum) && Auxiliary::acceptInput(minimum)) {
        m_minimum = minimum;
        // Saving the wavelength into the internal dataset variable
        core->getYAxis()->setMinYPlot(loc.toDouble(m_minimum));
        emit minimumChanged();
    }
}

QString YAxisQml::maximum() const { return loc.toString(core->getYAxis()->getMaxYPlot(), 'g', 6); }
void YAxisQml::setMaximum(const QString &maximum)
{
    if ((maximum != m_maximum)&& Auxiliary::acceptInput(maximum)) {
        m_maximum = maximum;
        // Saving the wavelength into the internal dataset variable
        core->getYAxis()->setMaxYPlot(loc.toDouble(m_maximum));
        emit maximumChanged();
    }
}

// Vertical scale units
quint8 YAxisQml::units() const { return core->getYAxis()->getYPlotUnits(); }
void YAxisQml::setUnits(const quint8 &units)
{
    if (units != m_units) {
        m_units = units;
        // Saving the units into the object used to convert to different pathlength measurement units
        core->getYAxis()->setYPlotUnits(m_units);
        emit minimumChanged();
        emit maximumChanged();
        emit unitsChanged();
    }
}

// Auto recalculation of the cross-sectional minimum and maximum values
bool YAxisQml::crossSectionAutoRecalc() const { return this->core->getYAxis()->getCrossSectionAutoRecalc(); }
void YAxisQml::setCrossSectionAutoRecalc(const bool &crossSectionAutoRecalc)
{
    if (crossSectionAutoRecalc != m_crossSectionAutoRecalc)
    {
        m_crossSectionAutoRecalc = crossSectionAutoRecalc;
        this->core->getYAxis()->setCrossSectionAutoRecalc(crossSectionAutoRecalc);
        emit crossSectionAutoRecalcChanged();
    }
}

// Auto recalculation of the radiance minimum and maximum values
bool YAxisQml::radianceAutoRecalc() const { return this->core->getYAxis()->getRadianceAutoRecalc(); }
void YAxisQml::setRadianceAutoRecalc(const bool &radianceAutoRecalc)
{
    if (radianceAutoRecalc != m_radianceAutoRecalc)
    {
        m_radianceAutoRecalc = radianceAutoRecalc;
        this->core->getYAxis()->setRadianceAutoRecalc(radianceAutoRecalc);
        emit radianceAutoRecalcChanged();
    }
}

// Methods to enable or disable the tracking rulers
bool YAxisQml::enableRulers() const { return core->getYAxis()->getEnableRulers(); }
void YAxisQml::setEnableRulers(const bool &enableRulers)
{
    if (enableRulers != m_enableRulers)
    {
        m_enableRulers = enableRulers;
        core->getYAxis()->setEnableRulers(m_enableRulers);
        emit enableRulersChanged();
    }
}
