// 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 determine the spectral regions to be requested from the remote server
// The difference between this object and the standard download regions finder is that here equidistant sub-regions are used and files are not merged
#ifndef NORMALIZEDDOWNLOADREGIONSFINDER_H
#define NORMALIZEDDOWNLOADREGIONSFINDER_H

#include <QVector>
#include <QString>
#include <QObject>

class NormalizedDownloadRegionsFinder : public QObject
{
    Q_OBJECT

public:

    NormalizedDownloadRegionsFinder(QObject *parent = 0);

    // Name of the file containing the history of downloads
    QString getSegmentedHitranPath();
    void setSegmentedHitranPath(QString newSegmentedHitranPath);

    // Methods to get and set the user specified and determined for the new download spectral region
    double getStartWn();
    void setStartWn(double newStartWn);
    double getEndWn();
    void setEndWn(double newEndWn);

    // Getting and setting the download regions wavelength arrays (start and end)
    QVector<double> *getDownloadStartWn();
    void setDownloadStartWn(QVector<double> *newDownloadStartWn);
    QVector<double> *getDownloadEndWn();
    void setDownloadEndWn(QVector<double> *newDownloadEndWn);

    // Methods to get and set the download regions at a specified index
    double getDownloadStartWnValueAtIndex(int index);
    void setDownloadStartWnValueAtIndex(int index, double value);
    double getDownloadEndWnValueAtIndex(int index);
    void setDownloadEndWnValueAtIndex(int index, double value);

    // Very beginning wavenumber for the spectral regions to download
    double getDownloadVeryStartWn();
    // Very end wavenumber for the spectral regions to download
    double getDownloadVeryEndWn();

    // Method to determine the new download region based on the requested region and the data about the spectral ranges already downloaded
    void findDownloadRegions();
    void findDownloadEveryTmeRegions();

    // Method to get the file name for a given spectral region
    QString getFileNameAtIndex(int index);

signals:

    // Method to notify the donwloader that the list of regions to download has been formed
    void downloadRegionsFormed();

private:

    // Method to get the start and end wavelengths from the region number
    void getWnFromRegionNum(int regionNum, double *startWn, double *endWn);

    // Name of the file containing the history of downloads
    QString segmentedHitranPath;

    // Variables containing the user set download region boundaries as well as those determined after adjusting the download region based on the download history file information
    double startWn;
    double endWn;

    // Very start and very end download regions wavelengths (determined by rounding down and up the spectral download regions specified by the user)
    double downloadVeryStartWn;  // Very beginning wavenumber for the spectral regions to download
    double downloadVeryEndWn;    // Very end wavenumber for the spectral regions to download

    // Variable containing the download region step
    QVector<double> downloadtSteps;

    // Segment numbers which need to be downloaded
    QVector<int> segmentsToDownload;

    // Names of the files to save the downloaded data to
    QVector<QString> downloadFileNames;

    // Array with the wavenumbers for the new download region
    QVector<double> downloadStartWn;
    QVector<double> downloadEndWn;

};

#endif // NORMALIZEDDOWNLOADREGIONSFINDER_H
