package com.swiftcleaner.chovey.model.util;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.activity.result.ActivityResultLauncher;
import android.provider.Settings;

public class PermissionUtils {

    public static void requestNotificationPermission(
            Context context,
            ActivityResultLauncher<Intent> launcher,
            ActivityResultLauncher<String[]> permissionsLauncher
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            String[] array = new String[]{android.Manifest.permission.POST_NOTIFICATIONS};
            permissionsLauncher.launch(array);
        } else {
            notificationSettings(context, launcher);
        }
    }

    public static void notificationSettings(
            Context context,
            ActivityResultLauncher<Intent> launcher
    ) {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
        } else {
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
        }
        launcher.launch(intent);
    }
}
