// 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.



// Object is used to relay the wavelength information betweeen the C++ data objects and the QML interface
// All the data conversion code may better be moved into the Core module so the values retured are in the right
// units, otherwise this will have to be reimplemented for every component where units conversion is taking place
#include "wavelengthqml.h"
#include "auxiliary.h"
#include <QMessageBox>
#include <QLocale>
#include <QString>
#include <QLocale>
#include "math.h"

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

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

    // Saving the wavelength units
    m_units = core->getWavelengths()->getUnits();

    // Transfering the start wavelength data to the QML interface component
    m_startWavelength = loc.toString(core->getWavelengths()->getStartWlCurrUnits(), 'g', 6);

    // Transfering the end wavelength data to the QML interface component
    m_endWavelength = loc.toString(core->getWavelengths()->getEndWlCurrUnits(), 'g', 6);

    // Transfering the number of data points value into the QML interfacing component
    m_numPoints = core->getWavelengths()->getNumPoints();

    // Transfering the wavelength resolution data to the QML interface component
    m_resolWavelength = loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);

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

// Slot is used to update all values in the corresponding QML dialog
void WavelengthQml::setUpdatedValues()
{
    emit startWavelengthChanged();
    emit endWavelengthChanged();
    emit resolWavelengthChanged();
    emit unitsChanged();
    emit numPointsChanged();
}

WavelengthQml::WavelengthQml(const QString &startWavelength, const QString &endWavelength, const QString &resolWavelength, const int &units, QObject *parent)
    : QObject(parent), m_startWavelength(startWavelength), m_endWavelength(endWavelength), m_resolWavelength(resolWavelength), m_units(units)
{
    ;
}

QString WavelengthQml::startWavelength() const
{
    return loc.toString(core->getWavelengths()->getStartWlCurrUnits(), 'g', 6);
}
void WavelengthQml::setStartWavelength(const QString &startWavelength)
{
    if ((startWavelength != m_startWavelength) && Auxiliary::acceptInput(startWavelength)) {
        m_startWavelength = startWavelength;
        // Saving the wavelength into the internal dataset variable        
        core->getWavelengths()->setStartWlCurrUnits(loc.toDouble(m_startWavelength));
        core->getWavelengths()->setNumPoints(m_numPoints);
        m_resolWavelength = loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);
        emit startWavelengthChanged();
        emit resolWavelengthChanged();
    }
}

QString WavelengthQml::endWavelength() const
{
    return loc.toString(core->getWavelengths()->getEndWlCurrUnits(), 'g', 6);
}
void WavelengthQml::setEndWavelength(const QString &endWavelength)
{
    if ((endWavelength != m_endWavelength) && Auxiliary::acceptInput(endWavelength)) {
        m_endWavelength = endWavelength;
        // Saving the wavelength into the internal dataset variable
        core->getWavelengths()->setEndWlCurrUnits(loc.toDouble(m_endWavelength));
        core->getWavelengths()->setNumPoints(m_numPoints);
        m_resolWavelength = loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);
        emit endWavelengthChanged();
        emit resolWavelengthChanged();
    }
}

QString WavelengthQml::resolWavelength() const
{
    return loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);
}

quint8 WavelengthQml::units() const
{
    return core->getWavelengths()->getUnits();
}
void WavelengthQml::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->getWavelengths()->setUnits(m_units);

        // Updating the displayed wavelength and units values
        // Transfering the start wavelength data to the QML interface component
        m_startWavelength = loc.toString(core->getWavelengths()->getStartWlCurrUnits(), 'g', 6);
        // Transfering the end wavelength data to the QML interface component
        m_endWavelength = loc.toString(core->getWavelengths()->getEndWlCurrUnits(), 'g', 6);
        // Transfering the wavelength resolution data to the QML interface component
        m_resolWavelength = loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);

        // Emitting signals to update values
        emit startWavelengthChanged();
        emit endWavelengthChanged();
        emit resolWavelengthChanged();
        emit unitsChanged();
    }
}

// Number of points
int WavelengthQml::numPoints() const { return core->getWavelengths()->getNumPoints(); }
void WavelengthQml::setNumPoints(const int &numPoints)
{
    if (numPoints != m_numPoints)
    {
        // Saving the number of data points
        m_numPoints = numPoints;
        core->getWavelengths()->setNumPoints(m_numPoints);

        // Transfering the wavelength resolution data to the QML interface component
        m_resolWavelength = loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);

        emit numPointsChanged();
        emit resolWavelengthChanged();
    }
}
