// 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
//
// Share results methods are based on the tutorial and code Copyrighted (c) 2014 Nicolas Froment
// http://blog.lasconic.com/share-on-ios-and-android-using-qml/
//
// 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 Data save QML component with the core calculation components
#ifndef SHARERESULTSQML_H
#define SHARERESULTSQML_H

#include <QQuickItem>
#include <QObject>
#include "bytrancore.h"
#include "ios/iosshare.h"
#include "winphone/sharewindows.h"
#include "winphone/winrtsharetest.h"

class ShareResultsQml : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString saveImage READ saveImage WRITE setSaveImage NOTIFY saveImageChanged)
    Q_PROPERTY(QString topString READ topString WRITE setTopString NOTIFY topStringChanged)
    Q_PROPERTY(bool fileWritingComplete READ fileWritingComplete WRITE setFileWritingComplete NOTIFY fileWritingCompleteChanged)
    Q_PROPERTY(bool attachImage READ attachImage WRITE setAttachImage NOTIFY attachImageChanged)
    Q_PROPERTY(bool attachDataFile READ attachDataFile WRITE setAttachDataFile NOTIFY attachDataFileChanged)
    Q_PROPERTY(bool shareResults READ shareResults WRITE setShareResults NOTIFY shareResultsChanged)
    Q_PROPERTY(bool dataAvailable READ dataAvailable)

public:

    Q_INVOKABLE void saveToDocuments();
    // Share results methods are based on the tutorial and code Copyrighted (c) 2014 Nicolas Froment - http://blog.lasconic.com/share-on-ios-and-android-using-qml/
    // Other resources were used as well but were less helpful:
    // http://stackoverflow.com/questions/27094100/how-to-use-qandroidjniobject-to-call-java-code-from-inside-qt
    // https://www.calligra.org/blogs/sharing-with-qt-on-android/
    // http://www.kdab.com/qt-android-episode-5/
    // http://stackoverflow.com/questions/2264622/android-multiple-email-attachment-using-intent-question
    // https://amin-ahmadi.com/2015/12/07/how-to-mix-c-and-java-code-in-qt-for-android/
    // Method is called after saving of the data to a file has been completed (if the user has chosen to share data)
    Q_INVOKABLE void shareImageAndText();

    // Method to let the user know that no data is available for saving
    Q_INVOKABLE void showNoDataAlert();

    Q_INVOKABLE void testCall();

    explicit ShareResultsQml(QObject *parent = 0, BytranCore *inCore = nullptr);

    // Methods to get and set the plot image
    QString saveImage() const;
    void setSaveImage(const QString &saveImage);

    // Methods to get and set values indicating if an image file is to be attached to the share message
    bool attachImage() const;
    void setAttachImage(const bool attachImage);

    // Methods to get and set the values indicating if a data file is to be attached to the share message
    bool attachDataFile() const;
    void setAttachDataFile(const bool attachDataFile);

    // Methods to get and set the top string with the information about the total pressure, pathlegth and temperature
    QString topString() const;
    void setTopString(const QString &topString);

    // Methods to get and set the state when file writing is complete (this could be implemented in a different was as aprameter passing is not needed)
    bool fileWritingComplete() const;
    void setFileWritingComplete(const bool &fileWritingComplete);

    // Method to set a variable indicating if the results need to be shared after they have been saved to the local storage
    bool shareResults() const;
    void setShareResults(const bool &shareResults);

    // Method to expose the state of the data available valriable
    bool dataAvailable() const;

signals:

    // Signal emited when the plot image is modigied
    void saveImageChanged();

    // Signal emited when the attach image boolean is modified
    void attachImageChanged();

    // signal emited when the attach data file boolean is modified
    void attachDataFileChanged();

    // Signal is emited when the top string is modified
    void topStringChanged();

    // Signal is emitted when saving of a file is complete
    void fileWritingCompleteChanged();

    // Signal emitted when a variable indicating if the results are to be shared is modified
    void shareResultsChanged();

private slots:

    // Informting the QML components that file writing is complete
    void fileSavedToDisk();

    // Slot to save the configuration file for sharing
    void saveConfigForSharing(QString path);

public slots:

private:

    // Method to create connections
    void createActions();

    // Pointer to the core object
    BytranCore *core;

    // Variable containing the path to save the plot image to
    QString m_saveImage;

    // Variable indicates if an image file is to be attached to the shared message
    bool m_attachImage;

    // Variable indicates if a data file is to be attached to the shared message
    bool m_attachDataFile;

    // Variable contains the top string with the additional information to be displayed in the saved plot
    QString m_topString;

    // Variable corresponding to the completion of file writing to disk
    bool m_fileWritingComplete;

    // Variable indicates that the save operation is being performed
    bool saveInProgress;

    // Variable indicating if the results are to be shared after saving
    bool m_shareResults;

    // Object to share results of the calculations on the iOS platform
    iosShare *iosShareDataAndText;

    // Variable indicating if the data is available
    bool m_dataAvailable;

    // Object to share the calculation results under the Windows Phone operating system
    //ShareWindows ^shareWinPhone;

    #ifdef Q_OS_WINPHONE
        WinRtShareTest ^winPhoneShare;
    #endif
};

#endif // SHARERESULTSQML_H
