// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | contact@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 "wavelengthdataobject.h"
#include <QMessageBox>
#include <QLocale>
#include <QString>
#include <QLocale>
#include "math.h"

WavelengthDataObject::WavelengthDataObject(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 = QString::number(core->getWavelengths()->getNumPoints());

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

WavelengthDataObject::WavelengthDataObject(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 WavelengthDataObject::startWavelength() const
{
    return m_startWavelength;
}

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

QString WavelengthDataObject::endWavelength() const
{
    return m_endWavelength;
}

void WavelengthDataObject::setEndWavelength(const QString &endWavelength)
{
    if (endWavelength != m_endWavelength) {
        m_endWavelength = endWavelength;

        // Saving the wavelength into the internal dataset variable
        core->getWavelengths()->setEndWlCurrUnits(loc.toDouble(m_endWavelength));
        core->getWavelengths()->setNumPoints(loc.toInt(m_numPoints));
        m_resolWavelength = loc.toString(core->getWavelengths()->getWlResolCurrUnits(), 'g', 6);
        emit endWavelengthChanged();
        emit resolWavelengthChanged();
    }
}

QString WavelengthDataObject::resolWavelength() const
{
    return m_resolWavelength;
}

int WavelengthDataObject::units() const
{
    return m_units;
}

void WavelengthDataObject::setUnits(const int &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
QString WavelengthDataObject::numPoints() const { return this->m_numPoints; }
void WavelengthDataObject::setNumPoints(const QString &numPoints)
{
    if (numPoints != m_numPoints)
    {
        // Saving the number of data points
        m_numPoints = numPoints;
        core->getWavelengths()->setNumPoints(loc.toInt(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();
    }
}
