package net.heartshield.ui;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import net.heartshield.prevent.R;
import net.heartshield.sensors.Camera2Detector;

/**
 * For apps above API 21, Android requires that we flash the user with a runtime permission request
 * the first time before we use a permission.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2016-03-21
 */
public class PermissionHelper {
    private static final String TAG = "PermissionHelper";

    private Activity mActivity;
    private String[] mPermissions;

    public PermissionHelper(Activity activity, String[] permissions) {
        mActivity = activity;
        mPermissions = permissions;
    }

    /** show a permissions request if necessary */
    public void request() {
        if(!hasPermissionsGranted()) {
            requestPermissions();
        }
    }

    public boolean hasPermissionsGranted() {
        for (String permission : mPermissions) {
            if (ActivityCompat.checkSelfPermission(mActivity, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public static class ConfirmationDialog extends DialogFragment {
        private Activity mActivity;
        private String[] mPermissions;

        public ConfirmationDialog() {}

        public void setActivity(Activity activity) { mActivity = activity; }

        public void setPermissions(String[] permissions) {
            this.mPermissions = permissions;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Activity activity = mActivity;

            return new AlertDialog.Builder(getActivity())
                    .setMessage(R.string.permission_request)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(activity, mPermissions, 1);
                        }
                    })
                    .setNegativeButton(android.R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    activity.moveTaskToBack(true);
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                    System.exit(1);
                                }
                            })
                    .create();
        }
    }

    private void requestPermissions() {
        if (shouldShowRequestPermissionRationale(mPermissions)) {
            Log.i(TAG, "shouldShowRequestPermissionRationale()=true");

            ConfirmationDialog cd = new ConfirmationDialog();
            cd.setActivity(mActivity);
            cd.setPermissions(mPermissions);
            cd.show(mActivity.getFragmentManager(), "dialog");
        } else {
            Log.i(TAG, "shouldShowRequestPermissionRationale()=false");
            ActivityCompat.requestPermissions(mActivity, mPermissions, 1);
        }
    }

    private boolean shouldShowRequestPermissionRationale(String[] permissions) {
        for (String permission : permissions) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permission)) {
                return true;
            }
        }
        return false;
    }

}
