package com.ama.core.architecture.util.permission;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.ama.core.architecture.BaseApp;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

public class PermissionUtil {
    public static final int PERMISSION_RESULT_ALL_GRANTED = 1;
    public static final int PERMISSION_RESULT_PARTIAL_GRANTED = 2;
    public static final int PERMISSION_RESULT_ALL_REJECTED = 3;



    public static final String PERMISSION_BROADCAST_ACTION = "permission_broadcast_action";
    public static final String PERMISSION_BROADCAST_ACTION_EXTRA_UUID = "permission_broadcast_action_extra_uuid";
    public static final String PERMISSION_BROADCAST_ACTION_EXTRA_RESULT = "permission_broadcast_action_extra_result";


    public static abstract class ICallback implements Serializable {
        public abstract void onAllGranted();
        public abstract void onPartialGranted();
        public abstract void onAllRejected();
    }

    public static void checkPermission(@NonNull String permission,
                                       @NonNull ICallback callback) {
        if (!TextUtils.isEmpty(permission)) {
            checkPermission(new String[]{permission}, callback);
        }
    }

    public static void checkPermission(@NonNull String[] permissionArr,
                                       @NonNull ICallback callback) {
        if (permissionArr == null || permissionArr.length == 0) {
            callback.onAllGranted();
            return;
        }

        String actionUUID = UUID.randomUUID().toString();
        regisBroadcast(actionUUID, callback);

        Intent intent = new Intent(BaseApp.Companion.appContext(), PermissionActivity.class);
        intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(PermissionActivity.EXTRA_ACTION_UUID, actionUUID);
        intent.putStringArrayListExtra(PermissionActivity.EXTRA_PERMISSION, new ArrayList<String>(Arrays.asList(permissionArr)));
        BaseApp.Companion.appContext().startActivity(intent);
    }

    private static void regisBroadcast(String actionUUID, ICallback callback) {
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(BaseApp.Companion.appContext());
        broadcastManager.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String resultActionUUID = intent.getStringExtra(PERMISSION_BROADCAST_ACTION_EXTRA_UUID);
                int permissionResult = intent.getIntExtra(PERMISSION_BROADCAST_ACTION_EXTRA_RESULT, PERMISSION_RESULT_ALL_REJECTED);
                broadcastManager.unregisterReceiver(this);

                if (callback != null && actionUUID.equalsIgnoreCase(resultActionUUID)) {
                    switch (permissionResult) {
                        case PERMISSION_RESULT_ALL_GRANTED:
                            callback.onAllGranted();
                            break;

                        case PERMISSION_RESULT_PARTIAL_GRANTED:
                            callback.onPartialGranted();
                            break;

                        case PERMISSION_RESULT_ALL_REJECTED:
                            callback.onAllRejected();
                            break;
                    }
                }
            }
        }, new IntentFilter(PERMISSION_BROADCAST_ACTION));
    }

}
