package com.base.browserwhite.utils

import android.app.role.RoleManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings

object RoleManagerUtils {

    fun Context.requestRoleBrowser(launcher: ActivityLauncher) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val roleManager = getSystemService(RoleManager::class.java) as RoleManager
            if (!roleManager.isRoleAvailable("android.app.role.BROWSER")) {
                val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
                launcher.launch(intent)
            } else if (roleManager.isRoleHeld("android.app.role.BROWSER")) {
                val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
                startActivity(intent)
            } else {
                val intent = roleManager.createRequestRoleIntent("android.app.role.BROWSER")
                launcher.launch(intent)
            }

        } else {
            val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
            startActivity(intent)
        }
    }
}