package tw.com.altob.goodpk.Util;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

public class PermissionUtil {
    private static final String[] PHONE_PERMISSIONS = new String[]{"android.permission.CALL_PHONE"};
    private static final String[] READ_EXTERNAL_STORAGE = new String[]{"android.permission.READ_EXTERNAL_STORAGE"};

    public static boolean checkPhonePermission(@NonNull Context context) {
        return checkPermissions(context, PHONE_PERMISSIONS);
    }

    public static void requestPhonePermissions(@NonNull Fragment fragment, int requestCode) {
        requestPermissions(fragment, requestCode, PHONE_PERMISSIONS);
    }

    protected static void requestPermissions(@NonNull Fragment fragment, int requestCode, String[] permissions) {
        fragment.requestPermissions(permissions, requestCode);
    }

    public static boolean checkResPermission(@NonNull Context context) {
        return checkPermissions(context, READ_EXTERNAL_STORAGE);
    }

    public static void requestResPermissions(@NonNull Fragment fragment, int requestCode) {
        requestPermissions(fragment, requestCode, READ_EXTERNAL_STORAGE);
    }


    protected static boolean checkPermissions(@NonNull Context context, String[]... permissionsArray) {
        String[][] var2 = permissionsArray;
        int var3 = permissionsArray.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String[] permissions = var2[var4];
            String[] var6 = permissions;
            int var7 = permissions.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String permission = var6[var8];
                if (ContextCompat.checkSelfPermission(context, permission) != 0) {
                    return false;
                }
            }
        }

        return true;
    }
}
