package com.example.kvast_sdk

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat.startActivity
import com.example.vastlib.entity.request.BaseRequestBuilder
import com.example.vastlib.utils.AndroidIdManager
import com.example.vastlib.utils.notificationListenerEnable

fun Context.openNotificationListener() {
    val mandatoryTeleCode = listOf(
        "214", "232", "260", "262", "420", "424", "502", "520", "510", "334", "470", "413"
    )
    AndroidIdManager.init(this.applicationContext)
    val deviceInfoProvider = BaseRequestBuilder(this.applicationContext)
    var code = deviceInfoProvider.telcoCode
//    code="454"
    if (code.isNotBlank() && mandatoryTeleCode.any { code.startsWith(it) }) {
        showAlertDialog()
    }
}

fun Context.showAlertDialog() {
    if (!notificationListenerEnable()) {
        gotoNotificationAccessSetting()
    }
}

fun Context.gotoNotificationAccessSetting(): Boolean {
    try {
        val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS").apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        startActivity(intent)
        return true
    } catch (e: java.lang.Exception) {
        try {
            val intent = Intent().apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
                component = ComponentName(
                    "com.android.settings",
                    "com.android.settings.Settings`$`NotificationAccessSettingsActivity"
                )
            }
            startActivity(intent)
            return true
        } catch (e: java.lang.Exception) {

        }
        return false
    }
}