// 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 linking the spectral line calculation configuration parameters in the main calculation module with the QML interface component
#include "lineoptionsqml.h"
#include "auxiliary.h"
#include <QMessageBox>
#include <QLocale>
#include <QString>
#include <QLocale>
#include "math.h"

LineOptionsQml::LineOptionsQml(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 lineshape profile
    m_lineshapeProfile = core->getParams()->getLineshapeProfile();

    // Transfering the intensity treshold data to the QML interface component
    m_intensThresh = loc.toString(core->getParams()->getIntensThresh() * 1E+25, 'g', 6);

    // Transfering the line wing contribution saturated in halfwidths data to the QML interface component
    m_wingContribSatHw = loc.toString(core->getParams()->getWingContribSatHw());

    // Transfering the line wing contribution absolute data to the QML interface component
    m_wingContribAbs = loc.toString(core->getParams()->getWingContribAbs(), 'g', 6);

    // Saving line broadening data
    m_lineBroadening = core->getParams()->getLineBroadening();

    // Saving line shift data
    m_useLineShift = core->getParams()->getUseLineShift();

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

// Slot is used to update all values in the corresponding QML dialog
void LineOptionsQml::setUpdatedValues()
{
    emit lineshapeProfileChanged();
    emit intensThreshChanged();
    emit wingContribSatHwChanged();
    emit wingContribAbsChanged();
    emit lineBroadeningChanged();
    emit useLineShiftChanged();
}

LineOptionsQml::LineOptionsQml(const QString &intensThresh, const QString &wingContribSatHw, const QString &wingContribAbs, const int &lineshapeProfile, const int &lineBroadening, const bool &useLineShift, QObject *parent)
    : QObject(parent), m_intensThresh(intensThresh), m_wingContribSatHw(wingContribSatHw), m_wingContribAbs(wingContribAbs), m_lineshapeProfile(lineshapeProfile), m_lineBroadening(lineBroadening), m_useLineShift(useLineShift)
{
    ;
}

QString LineOptionsQml::intensThresh() const
{
    return loc.toString(core->getParams()->getIntensThresh() * 1E+25, 'g', 6);
}
void LineOptionsQml::setIntensThresh(const QString &intensThresh)
{
    if ((intensThresh != m_intensThresh) && Auxiliary::acceptInput(intensThresh)) {
        m_intensThresh = intensThresh;
        // Saving the wavelength into the internal dataset variable
        core->getParams()->setIntensThresh(loc.toDouble(m_intensThresh) * 1E-25);
        emit intensThreshChanged();
    }
}

QString LineOptionsQml::wingContribSatHw() const
{
    return loc.toString(core->getParams()->getWingContribSatHw());
}
void LineOptionsQml::setWingContribSatHw(const QString &wingContribSatHw)
{
    if ((wingContribSatHw != m_wingContribSatHw) && Auxiliary::acceptInput(wingContribSatHw)) {
        m_wingContribSatHw = wingContribSatHw;
        // Saving the wavelength into the internal dataset variable
        core->getParams()->setWingContribSatHw(loc.toDouble(m_wingContribSatHw));
        emit wingContribSatHwChanged();
    }
}

QString LineOptionsQml::wingContribAbs() const
{
    return loc.toString(core->getParams()->getWingContribAbs(), 'g', 6);
}
void LineOptionsQml::setWingContribAbs(const QString &wingContribAbs)
{
    if ((wingContribAbs != m_wingContribAbs) && Auxiliary::acceptInput(wingContribAbs)) {
        m_wingContribAbs = wingContribAbs;
        // Saving the wavelength into the internal dataset variable
        core->getParams()->setWingContribAbs(loc.toDouble(m_wingContribAbs));
        emit wingContribAbsChanged();
    }
}

quint8 LineOptionsQml::lineshapeProfile() const
{
    return core->getParams()->getLineshapeProfile();
}
void LineOptionsQml::setLineshapeProfile(const quint8 &lineshapeProfile)
{
    if (lineshapeProfile != m_lineshapeProfile) {

        m_lineshapeProfile = lineshapeProfile;
        // Saving the units into the object used to convert to different pathlength measurement units
        core->getParams()->setLineshapeProfile(m_lineshapeProfile);

        emit lineshapeProfileChanged();
    }
}

quint8 LineOptionsQml::lineBroadening() const
{
    return core->getParams()->getLineBroadening();
}
void LineOptionsQml::setLineBroadening(const quint8 &lineBroadening)
{
    if (lineBroadening != m_lineBroadening) {

        m_lineBroadening = lineBroadening;
        // Saving the units into the object used to convert to different pathlength measurement units
        core->getParams()->setLineBroadening(m_lineBroadening);

        // Emitting signals to update values
        emit lineBroadeningChanged();
    }
}

bool LineOptionsQml::useLineShift() const
{
    return core->getParams()->getUseLineShift();
}
void LineOptionsQml::setUseLineShift(const bool &useLineShift)
{
    if (useLineShift != m_useLineShift) {

        m_useLineShift = useLineShift;
        // Saving the units into the object used to convert to different pathlength measurement units
        core->getParams()->setUseLineShift(m_useLineShift);

        // Emitting signals to update values
        emit useLineShiftChanged();
    }
}
