package com.app.duck.util;

import android.Manifest;
import android.app.AlertDialog;
import android.view.View;

import com.app.duck.R;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.RomUtils;
import com.blankj.utilcode.util.StringUtils;

import org.jetbrains.annotations.NotNull;

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

public class PermissionsUtils {
    public static void getPermission(final PermissionAction action, String[] p, boolean isHaveSms) {
        PermissionUtils.permission(p)
                .rationale((activity, shouldRequest) -> pDialog(view -> shouldRequest.again(true)))
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(@NotNull List<String> permissionsGranted) {
                        if (permissionsGranted.size() == p.length) {
                            if (isHaveSms && RomUtils.isXiaomi()) {
                                HxUtils.QueryXiaoMiSmsInbox();
                            }
                            action.onGranted();
                        }
                    }

                    @Override
                    public void onDenied(@NotNull List<String> forever, @NotNull List<String> denied) {
                        if (!forever.isEmpty()) {
                            pDialog(view -> {
                                AppUtils.launchAppDetailsSettings();
                            });
                        } else {
                            getPermission(action, p, isHaveSms);
                        }
                    }
                }).request();
    }

    public static String[] permissions(boolean b1, boolean b2) {
        List<String> permission = new ArrayList<>();
        if (b1) {
            permission.add(Manifest.permission.RECEIVE_BOOT_COMPLETED);
            permission.add(Manifest.permission.READ_PHONE_STATE);
            permission.add(Manifest.permission.WAKE_LOCK);
            permission.add(Manifest.permission.CALL_PHONE);
            permission.add(Manifest.permission.DISABLE_KEYGUARD);
            permission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            permission.add(Manifest.permission.RECORD_AUDIO);
            permission.add(Manifest.permission.FOREGROUND_SERVICE);
            permission.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (b2) {
            permission.add(Manifest.permission.READ_SMS);
            permission.add(Manifest.permission.SEND_SMS);
            permission.add(Manifest.permission.RECEIVE_SMS);
        }
        return permission.toArray(new String[0]);
    }

    public static void checkPermission(boolean b1, boolean b2) {
        if (!b1 && !b2) {
            return;
        }
        getPermission(() -> {
        }, permissions(b1, b2), b2);
    }


    public static void pDialog(View.OnClickListener onClickListener) {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(ActivityUtils.getTopActivity());
        normalDialog.setIcon(AppUtils.getAppIcon());
        normalDialog.setMessage(StringUtils.getString(R.string.go_setting_1_s, AppUtils.getAppName()));
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton(R.string.go_bt, (dialog, which) -> onClickListener.onClick(null));
        normalDialog.show();

    }


    public interface PermissionAction {
        void onGranted();
    }
}
