package com.yyrh.utils;

import android.Manifest;
import android.app.Activity;

import android.content.pm.PackageManager;
import android.os.Build;

import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
public class PermissionUtils {
    private static final String TAG = PermissionUtils.class.getSimpleName();
    public static final int CODE_RECORD_AUDIO = 0;
    public static final int CODE_GET_ACCOUNTS = 1;
    public static final int CODE_READ_PHONE_STATE = 2;
    public static final int CODE_CALL_PHONE = 3;
    public static final int CODE_CAMERA = 4;
    public static final int CODE_ACCESS_FINE_LOCATION = 5;
    public static final int CODE_ACCESS_COARSE_LOCATION = 6;
    public static final int CODE_READ_EXTERNAL_STORAGE = 7;
    public static final int CODE_WRITE_EXTERNAL_STORAGE = 8;
    public static final int CODE_MULTI_PERMISSION = 100;

    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    private static final String[] requestPermissions = {
            PERMISSION_RECORD_AUDIO,
            PERMISSION_GET_ACCOUNTS,
            PERMISSION_READ_PHONE_STATE,
            PERMISSION_CALL_PHONE,
            PERMISSION_CAMERA,
            PERMISSION_ACCESS_FINE_LOCATION,
            PERMISSION_ACCESS_COARSE_LOCATION,
            PERMISSION_READ_EXTERNAL_STORAGE,
            PERMISSION_WRITE_EXTERNAL_STORAGE
    };

    public interface PermissionGrant {
        void onPermissionGranted(int requestCode);

    }

    public static int checkSelfPermission(final Activity activity, final String requestPermission, String met) {

        Object result = null;
        /*
        try {
            Class<?> cls = Class.forName("android.support.v4.app.ActivityCompat");
            Object object = cls.newInstance();
            Method method = cls.getMethod(met, Context.class, String.class);
            result = method.invoke(object, activity, requestPermission);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        */
        result = ContextCompat.checkSelfPermission(activity,requestPermission);
        return (Integer.valueOf(result.toString()).intValue()) ;

    }

    public static boolean shouldShowRequestPermissionRationale(final Activity activity, final String requestPermission, String met) {

        Object result = null;
        /*
        try {
            Class<?> cls = Class.forName("android.support.v4.app.ActivityCompat");
            Object object = cls.newInstance();
            Method method = cls.getMethod(met, Activity.class, String.class);
            result = method.invoke(object, activity, requestPermission);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        */
        result = ActivityCompat.shouldShowRequestPermissionRationale(activity,requestPermission);

        return Boolean.parseBoolean(result.toString());

    }


    public static void requestPermissions(final Activity activity, String[] requestPermission, int requestCode, String met) {

        /**
        try {
            Class<?> cls = Class.forName("android.support.v4.app.ActivityCompat");
            Object object = cls.newInstance();
            Method method = cls.getMethod(met, Activity.class, String[].class, int.class);
            method.invoke(object, activity, requestPermission, requestCode);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
         **/
        ActivityCompat.requestPermissions(activity,requestPermission,requestCode);


    }

    public static int checkIsHasPermission(Activity activity,int requestCode){
        if (activity == null || Build.VERSION.SDK_INT < 23) {
            return 0;
        }
        final String requestPermission = requestPermissions[requestCode];
        int checkSelfPermission = 0;

        try {

            checkSelfPermission = checkSelfPermission(activity, requestPermission, "checkSelfPermission");

        } catch (RuntimeException e) {


        }
        return checkSelfPermission;
    }



    public static void requestPermission(final Activity activity, final int requestCode) {

        if (activity == null || Build.VERSION.SDK_INT < 23) {
            return;
        }


        final String requestPermission = requestPermissions[requestCode];
        int checkSelfPermission;


        try {

            checkSelfPermission = checkSelfPermission(activity, requestPermission, "checkSelfPermission");
            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
                YYLog.i("ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");

                if (shouldShowRequestPermissionRationale(activity, requestPermission, "shouldShowRequestPermissionRationale")) {
                    YYLog.i("requestPermission shouldShowRequestPermissionRationale");
                    requestPermissions(activity, new String[]{requestPermission}, requestCode, "requestPermissions");
                } else {
                    requestPermissions(activity, new String[]{requestPermission}, requestCode, "requestPermissions");
                }
            } else {
                YYLog.i("ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");

            }
        } catch (RuntimeException e) {

            YYLog.i("RuntimeException:" + e.getMessage());
            return;
        }

    }


}