package com.xty.common.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.xty.common.R;


import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class PermissionUtils {/*


    public final static int REQUEST_CODE_SETTING = 103;
    *//**
     * 成功回掉
     *//*
    public interface PermissionSuccessListener {
        void onSuccess();
    }

    *//**
     * 失败回掉
     *//*
    public interface PermissionFailedListener {
        void onFailed();
    }

    *//**
     * @param context     上下文
     * @param listener    成功回调
     * @param permissions 单个权限
     *//*
    @SuppressLint("WrongConstant")
    public static void requestPermission(final Context context, final PermissionSuccessListener listener, String... permissions) {
        final PermissionSetting mSetting = new PermissionSetting(context);

        if (EasyPermissions.hasPermissions(context,permissions)){
            listener.onSuccess();
        }else{
            EasyPermissions.requestPermissions(context,"", 0x00022,permissions);
        }
        AndPermission.with(context)
                .runtime()
                .permission(permissions)
                .rationale(new DefaultRationale())
                .onGranted(permissions1 -> {
                    if (listener != null) {
                        listener.onSuccess();
                    }
                })
                .onDenied(permissions12 -> {
                    if (AndPermission.hasAlwaysDeniedPermission(context, permissions12)) {
                        mSetting.showSetting(permissions12);
                    }
                })
                .start();
    }


    *//**
     * @param context        上下文
     * @param listener       回调成功
     * @param failedListener 回调失败
     * @param permissions    多个权限
     *//*
    @SuppressLint("WrongConstant")
    public static void requestPermission(final Context context, final PermissionSuccessListener listener, final PermissionFailedListener failedListener, String... permissions) {
        final PermissionSetting mSetting = new PermissionSetting(context);
        AndPermission.with(context)
                .runtime()
                .permission(permissions)
                .rationale(new DefaultRationale())
                .onGranted(permissions1 -> {
                    if (listener != null) {
                        listener.onSuccess();
                    }
                })
                .onDenied(permissions12 -> {
                    if (failedListener != null)
                        failedListener.onFailed();
                    if (AndPermission.hasAlwaysDeniedPermission(context, permissions12)) {
                        mSetting.showSetting(permissions12);
                    }
                })
                .start();
    }

    *//**
     * 跳转到权限设置
     *
     * @param activity
     *//*
    public static void toPermissionSetting(Activity activity) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            toSystemConfig(activity);
        } else {
            try {
                toApplicationInfo(activity);
            } catch (Exception e) {
                e.printStackTrace();
                toSystemConfig(activity);
            }
        }
    }

    *//**
     * 应用信息界面
     *
     * @param activity
     *//*
    public static void toApplicationInfo(Activity activity) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        localIntent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        activity.startActivity(localIntent);
    }

    *//**
     * 系统设置界面
     *
     * @param activity
     *//*
    public static void toSystemConfig(Activity activity) {
        try {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    *//**
     * @description：默认权限设置提示dialog
     **//*
    public final static class DefaultRationale implements Rationale<List<String>> {

        @Override
        public void showRationale(Context context, List<String> data, RequestExecutor executor) {
            List<String> permissionNames = Permission.transformText(context, data);
            String message = context.getString(R.string.message_permission_rationale, TextUtils.join("\n", permissionNames));

            new androidx.appcompat.app.AlertDialog.Builder(context)
                    .setTitle(R.string.title_dialog)
                    .setMessage(message)
                    .setPositiveButton(R.string.resume, (dialog, which) -> executor.execute())
                    .setNegativeButton(R.string.cancel, (dialog, which) -> executor.cancel())
                    .show();
        }
    }

    *//**
     * @description：权限设置dialog
     **//*
    public final static class PermissionSetting {

        private final Context mContext;

        public PermissionSetting(Context context) {
            this.mContext = context;
        }

        public void showSetting(final List<String> permissions) {
            List<String> permissionNames = Permission.transformText(mContext, permissions);
            String message = mContext.getString(R.string.message_permission_always_failed, TextUtils.join("\n", permissionNames));

            new androidx.appcompat.app.AlertDialog.Builder(mContext)
                    .setCancelable(false)
                    .setTitle(R.string.title_dialog)
                    .setMessage(message)
                    .setPositiveButton(R.string.setting, (dialog, which) -> {
                        setPermission(mContext);
                    })
                    .setNegativeButton(R.string.no, (dialog, which) -> {
                    })
                    .show();
        }

        *//**
         * Set permissions.
         *//*
        private void setPermission(Context context) {
            AndPermission.with(context)
                    .runtime()
                    .setting()
                    .start(REQUEST_CODE_SETTING);
        }
    }*/
}
