package com.xyxsbj.mylibrary.tool;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人 mQ
 * 创建时间 2019/5/20 0020 10:32
 * 文件名称 PermissionTool
 * 说明: 权限申请工具类
 **/
public class PermissionTool {

    private String[] mPermissions;

    private static final int mRequestPermissionCode = 1001;

//    private Activity mActivity;

    private PermissionTool() {
    }

    private static class SingleHelper {
        private static final PermissionTool INSTANCE = new PermissionTool();
    }

    public static PermissionTool get() {
        return SingleHelper.INSTANCE;
    }

    public PermissionTool init(String[] permissions) {
        mPermissions = permissions;
        return this;
    }

    /**
     * 权限是否全部授权
     */
    public boolean checkPermissionAll(Activity mActivity) {
        for (String p : mPermissions) {
            if (!(ContextCompat.checkSelfPermission(mActivity, p) == PackageManager.PERMISSION_GRANTED)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 申请权限
     */
    public PermissionTool requestPermission(Activity mActivity) {
        //申请权限
        ActivityCompat.requestPermissions(mActivity, mPermissions, mRequestPermissionCode);
        return this;
    }

    /**
     * 是否可以显示申请UI
     */
    public boolean getIsRequestUI(Activity mActivity, String[] permissions) {
        //是否可以显示UI
        boolean isRequestUI = false;
        for (String permission : permissions) {
            LogTool.i("requestPermission: " + ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permission));
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permission)) {
                isRequestUI = true;
            }
        }
        return isRequestUI;
    }

    /**
     * 处理权限申请结果
     */
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults, PermissionCallBack callBack) {
        if (requestCode == mRequestPermissionCode) {
            if (grantResults.length > 0) {
                List<String> mDeniedPermissions = new ArrayList<>();
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        mDeniedPermissions.add(permissions[i]);
                        LogTool.i("权限被拒绝: " + permissions[i]);
                    }
                }
                if (mDeniedPermissions.size() == 0) {
                    LogTool.i("权限全部通过");
                    if (callBack != null)
                        callBack.onGrantedPermissionAll();
                } else {
                    //权限申请被拒绝
                    if (callBack != null)
                        callBack.onDeniedPermission(mDeniedPermissions.toArray(new String[mDeniedPermissions.size()]));
                }
            }
        }
    }

    public interface PermissionCallBack {
        void onGrantedPermissionAll();

        void onDeniedPermission(String[] deniedPermissions);

    }



}
