// 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 to save the results of the calculations
#ifndef RESULTSSAVER_H
#define RESULTSSAVER_H

#include <QObject>
#include <QLocale>
#include <QImage>
#include <QTimer>
#include <onelayerspectra.h>
#include "wavelength.h"
#include "hitranonemol.h"

class ResultsSaver : public QObject
{
    Q_OBJECT

public:

    explicit ResultsSaver(QObject *parent = 0);

    // Method will save the results to a specified path
    // The save file name will be date and time stamped
    void saveResults();
    void generateFilePathsAndTopString();

    // Methods to get and set the file save path
    QString getSavePath();
    void setSavePath(QString newSavePath);

    // Method to form the file name to save the data to
    QString generateFileName();

    // Method to get the generated filename
    QString getTextFilePath();
    QString getImageFilePath();
    QString getConfigFilePath();
    QString getGenearatedFileName();

    // Method to pass in the object with the calculation results
    void setData(OneLayerSpectra *slantLayer);
    void addPushedData(QVector<double> data, QString title);
    void clearPushedData();

    // Method to get the wavelengths parameters
    Wavelength *getWavelength();
    void setWavelength(Wavelength *newWavelength);

    // Methods to get the total pressure, temperature, pathlength and partial pressures (to be saved in the output file)
    double getTotalPress();
    void setTotalPress(double newTotalPress);
    double getTemperature();
    void setTemperature(double newTemp);
    double getPathlength();
    void setPathlength(double newPathlength);
    double getPartPressAt(int index);
    void setPartPress(QVector<double> *newPartPress);
    void setSelectedAndNames(QVector<HitranOneMol> *mol);

    // Method to get the top string
    QString getTopString();

    // Monitor file saved to disk complete
    void monitorFilesSavedToDisk();

    // Method to get the delimited wavenumber grid (as a String)
    QString getDelimitedWnGridString(QString delimiter, char format, int precision);

    // Method to get the delimited spectral data (as a String) based on the partial column title name
    QString getDelimitedSpectrumString(QString colSubStr, QString delimiter, char format, int precision);

signals:

    // Signal emitted when the file writing is complete
    void savingFilesToDiskComplete();

    // Method to save the configuration file for sharing
    void saveConfigToDocuments(QString path);

private slots:

    // Method is called every time the timer time reaches its maximum
    void dataTimerUpdate();
    void imageTimerUpdate();

private:

    // Method to create connections
    void createActions();

    // Method to form the data string to save to a text file
    void saveDataToFile(QString filePath);
    void savePushedDataToFile(QString filePath);

    // Method to save the image file to disl once saving of the data file has finished
    void saveTextFileToDisk();

    // Path to save the file to
    QString savePath;

    // Object to store the results of the calculations
    OneLayerSpectra *data;

    // Object with the wavelength and related parameters
    Wavelength *wavelength;

    // Reference to an array with the partial pressures
    double totalPress;  // Total pressure
    double temp;        // Temperature
    double pathlength;  // Pathlength
    QVector<double> partPress;
    QString topString;   // String with the Total pressure, Temperature, and the Pathlength

    // Array with the selected states to know which data is to be saved
    QVector<bool> selected;
    QVector<QString> names;

    // Path to the text file and the name of the generated file without extension
    QString textFilePath;
    QString imageFilePath;
    QString configFilePath;
    QString generatedFileName;

    // Object to monitor when the file writing to disk has finished
    QTimer *imageFileTimer;
    QTimer *dataFileTimer;

    // Array with the saved results
    QVector< QVector<double> > pushedData;
    QVector<QString> titles;

    // Locale specific string to number and inverse conversions
    QLocale loc;

};

#endif // RESULTSSAVER_H
