// 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 following resources where most helpful in implementing this permission request code insert
// https://doc.qt.io/qt-5/qtandroid.html
// http://falsinsoft.blogspot.com.by/2015/03/qt-android-interface-c-and-java-through.html
// http://www.kdab.com/qt-android-episode-5/
// http://www.kdab.com/qt-android-episode-6/
// https://www.kdab.com/qt-android-episode-7/
// http://stackoverflow.com/questions/35027043/implementing-android-6-0-permissions-in-unity3d
// http://stackoverflow.com/questions/32347532/android-m-permissions-confused-on-the-usage-of-shouldshowrequestpermissionrati
// https://developer.android.com/training/permissions/requesting.html
// https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
// https://gist.github.com/patrickhammond/47cff299bc34e976e18b
// https://github.com/codepath/android_guides/wiki/Understanding-App-Permissions
// http://doc.qt.io/qt-5/qandroidjniobject.html
//
// 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 request permissions from the Android system
#include "permissions.h"
#include <QMessageBox>
#include <QApplication>

Permissions::Permissions(QObject *parent) : QObject(parent)
{
}

// Method to request permissions
void Permissions::requestExternalStoragePermission()
{
    QStringList permissionsToRequest;   // String list with the permissions to request

    #if defined(Q_OS_ANDROID)

        // Forming the string list with the permissions to request
        permissionsToRequest << "android.permission.WRITE_EXTERNAL_STORAGE";
        permissionsToRequest << "android.permission.READ_EXTERNAL_STORAGE";

        // Assuming that no permission has been granted
        this->permissionResult = false;

        // Result of the request for WRITE_EXTERNAL_STORAGE
        QtAndroid::PermissionResult requestResultWrite = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
        // Result of the request for READ_EXTERNAL_STORAGE
        QtAndroid::PermissionResult requestResultRead = QtAndroid::checkPermission("android.permission.READ_EXTERNAL_STORAGE");

        if ((requestResultWrite == QtAndroid::PermissionResult::Denied) || (requestResultRead == QtAndroid::PermissionResult::Denied))
        {
            QtAndroid::requestPermissionsSync(permissionsToRequest);
            requestResultWrite = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
            requestResultRead = QtAndroid::checkPermission("android.permission.READ_EXTERNAL_STORAGE");

            if ((requestResultWrite == QtAndroid::PermissionResult::Denied) || (requestResultRead == QtAndroid::PermissionResult::Denied))
            {
                this->permissionResult = false;
                if (QtAndroid::shouldShowRequestPermissionRationale("android.permission.WRITE_EXTERNAL_STORAGE") || QtAndroid::shouldShowRequestPermissionRationale("android.permission.READ_EXTERNAL_STORAGE"))
                {
                    ShowPermissionRationale = QAndroidJniObject("org/bytran/bytran/ShowPermissionRationale",
                                                                "(Landroid/app/Activity;)V",
                                                                 QtAndroid::androidActivity().object<jobject>()
                                                               );

                    // Checking for errors in the JNI
                    QAndroidJniEnvironment env;
                    if (env->ExceptionCheck()) {
                        // Handle exception here.
                        env->ExceptionClear();
                    }
                }
            }
            else { this->permissionResult = true; }
        }
        else { this->permissionResult = true; }

    #else

        this->permissionResult = false;

    #endif
}

// Method to get the permission granted state
bool Permissions::getPermissionResult() { return permissionResult; }
