package aicg.wipf.vzuj

import aicg.wipf.keyu.bzsb.Qhrr
import aicg.wipf.keyu.bzsb.Vaya
import aicg.wipf.rxxn.Gdmk
import aicg.wipf.vzuj.Kmqp.string
import android.app.AppOpsManager
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.PermissionUtils
import com.test.basd.smartjunkcleaner.R
import org.json.JSONObject

abstract class Hqlm<T : ViewBinding> : AppCompatActivity() {

    var dialog: Dialog? = null

    private val accessLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {

        }

    private var isRequestedAccess = false

    private val permissionLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                onPermissionsResult(Environment.isExternalStorageManager())
            }
        }

    protected open val isLightMode: Boolean? = null

    protected abstract val binding: T

    protected open fun initListener() {
        ޕފޗޖޓޙޔޤ()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ސބނޜޓރޙޡ(savedInstanceState)
        super.onCreate(savedInstanceState)
        ޏޜކވޅޑޘޞ(savedInstanceState)
    }

    private fun ޗޡޞސދޥގޅ() {
        val ށޢޑމޓޓޒޜ = 1138
        val ރޅދޕލޛޟނ = 8637
        val ރޣޔޢސޡޙމ = ށޢޑމޓޓޒޜ * ރޅދޕލޛޟނ * ރޅދޕލޛޟނ
        println(ރޣޔޢސޡޙމ)
        ޘށއމވޤޢގ()
    }

    open fun onAccesSettingsResult(isGranted: Boolean) {
        class ޓޗޟޜފކޙޚ {
            fun ޢގޡޗގގރދ() {
                println("މޙޖވޅޕޜބ")
            }
        }
        ޓޗޟޜފކޙޚ().ޢގޡޗގގރދ()
        ބޔޖޟޤކއޙ(isGranted)
    }

    private fun ގޅޚޝޑޢޕޤ(savedInstanceState: Bundle?) {
        ޜނތޕޢނޕޢ(savedInstanceState)
    }

    private fun ޚޟޓޗޜޚޖޢ(savedInstanceState: Bundle?) {
        abstract class ވޚޒޡޘޜޘޏ {
            abstract fun ޓލޜޏސތކޏ()
        }
        class ރތލށޗއނމ : ވޚޒޡޘޜޘޏ() {
            override fun ޓލޜޏސތކޏ() {
                println("ތފޚޒޤށޒޔ")
            }
        }
        val ޡލޔނޟޗކޏ = ރތލށޗއނމ()
        ޡލޔނޟޗކޏ.ޓލޜޏސތކޏ()
        ގޅޚޝޑޢޕޤ(savedInstanceState)
    }

    private fun ޚޝޠބރޜޥޞ() {
    }

    protected fun finishToMain() {
        class ޠޥޛލޓޑޗލ<T>(val ޔޣޙޓގގހޑ: T)
        val ޗސހގޓޕޜޝ = ޠޥޛލޓޑޗލ(2110)
        val ތތޓލހޠޖވ = ޠޥޛލޓޑޗލ("ހޔކޓޕލދސ")
        println(ޗސހގޓޕޜޝ.ޔޣޙޓގގހޑ)
        println(ތތޓލހޠޖވ.ޔޣޙޓގގހޑ)
        ސޜޔހޜޏދލ()
    }

    override fun onDestroy() {
        ޚޝޠބރޜޥޞ()
        class ރޜޥޞސޘޤޟ<T>(val ޕއމޠޅގރޣ: T, val ޙޠސށޠޢދމ: Int) {
            constructor(ޒޙޚދޡޘޓޡ: T) : this(ޒޙޚދޡޘޓޡ, 1723) {
                println("ޣށޘޒމއމޥ")
            }
        }
        println(ރޜޥޞސޘޤޟ("ވޥހޝބޝމޓ"))
        super.onDestroy()
        ޡލޕމޖޢޙޙ()
    }

    fun checkPermission(needcheck: Boolean = false) {
        if (needcheck) {
            onPermissionsResult(
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    class ޒތމޝޑޓއރ(val ޅމޒނކޟތތ: String, val ށޡތޅލއބޓ: Int)
                    println(ޒތމޝޑޓއރ("ވދމޒޓށޙޘ", 9817))
                    Environment.isExternalStorageManager()
                } else {
                    open class ފދޗތޏށޗޤ {
                        open fun ޥޣފޜގހހޘ() {}
                    }
                    class މތޓގޑޓޢޑ : ފދޗތޏށޗޤ() {
                        override fun ޥޣފޜގހހޘ() {
                            println("ޅޥޜޤޜޥތތ")
                        }
                    }
                    val ޅޢޤތޢޓޘލ = މތޓގޑޓޢޑ()
                    println(ޅޢޤތޢޓޘލ)
                    PermissionUtils.isGranted(PermissionConstants.STORAGE)
                }
            )
        } else {
            if (isRequested) {
                onPermissionsResult(
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        abstract class މޘޙފޕތޙޣ {
                            abstract fun ޒޑޗޥޤޅޑޛ()
                        }
                        class ޔނމސލބވރ : މޘޙފޕތޙޣ() {
                            override fun ޒޑޗޥޤޅޑޛ() {
                                println("ދޞމޞފމލޓ")
                            }
                        }
                        val ޥޙށޢޔޠވރ = ޔނމސލބވރ()
                        ޥޙށޢޔޠވރ.ޒޑޗޥޤޅޑޛ()
                        Environment.isExternalStorageManager()
                    } else {
                        PermissionUtils.isGranted(PermissionConstants.STORAGE)
                    }
                )
                return
            }
            abstract class ޗޝސވއފރޕ {
                abstract fun ޅޔޠޕޔޚޡޖ()
            }
            class ޗޡޠށޔޤސގ : ޗޝސވއފރޕ() {
                override fun ޅޔޠޕޔޚޡޖ() {
                    println("ޓހޑޑޘމޑޘ")
                }
            }
            val ޙކޢޏޡޠޥޞ = ޗޡޠށޔޤސގ()
            ޙކޢޏޡޠޥޞ.ޅޔޠޕޔޚޡޖ()
            isRequested = true

        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                onPermissionsResult(true)
            } else {
                fun ނމޟޥޞކއޥ(ޖނސޠޝހޥވ: Int, ޖޗރޞޥޏޘނ: Int): Int {
                    return ޖނސޠޝހޥވ + ޖޗރޞޥޏޘނ
                }
                println(ނމޟޥޞކއޥ(7502, 714))
                if (isFinishing || isDestroyed) {
                    return
                }
                dialog = Gdmk.showGerPermission(this, {

                    val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                    Log.d("ސއޟޜޚޡލށ", "ސއޟޜޚޡލށ")
                    intent.addCategory(R.string.ދގޣޒޜ_D.string())
                    intent.data = Uri.parse(R.string.ދވގވޞ_D.string((packageName).toString()))
                    permissionLauncher.launch(intent)
                    Qhrr.launch(this)
                }, {
                    finishToMain()
                })
            }

        } else {
            PermissionUtils.permissionGroup(PermissionConstants.STORAGE)
                .callback { isAllGranted, _, _, _ ->
                    if (isAllGranted) {
                        class ޡޤޘޢޡބގރ<T>(val ޘޥށޜޅޛފޛ: T, val ބލޣކޅޙޛދ: Int) {
                            constructor(ޞކޝޑޢޠށރ: T) : this(ޞކޝޑޢޠށރ, 2529) {
                                println("ޅޘދޏޞޤޢޏ")
                            }
                        }
                        println(ޡޤޘޢޡބގރ("ޚޢޠޥޗތލޤ"))
                        onPermissionsResult(true)
                    } else {
                        fun ޡރޡތލޘދޣ(ޢޜޞޕރޑޘޔ: Int, ޖއޑބޒގދށ: Int): Int {
                            return ޢޜޞޕރޑޘޔ + ޖއޑބޒގދށ
                        }
                        val ޚޘޟހޠޣޝޠ = ޡރޡތލޘދޣ(6496, 5540)
                        println(ޚޘޟހޠޣޝޠ)
                        onPermissionsResult(false)
                    }
                }.request()
        }
    }

    private fun ޜނތޕޢނޕޢ(savedInstanceState: Bundle?) {
        ރޕޏބޟޕޜރ(savedInstanceState)
    }

    private fun ދގޜބދޥޣބ() {
        val ހޓފކޗޙލޥ = 2734
        val ޝޑޞވޜކޜސ = 152
        val ލވޛއޞރވޖ = ހޓފކޗޙލޥ * ޝޑޞވޜކޜސ * ޝޑޞވޜކޜސ
        println(ލވޛއޞރވޖ)
        ޘޕޒޔއޥތޟ()
    }

    private fun ޖވޠސޓވނއ(isGranted: Boolean) {
        އޚސރހޘވޠ(isGranted)
    }

    protected abstract fun initView()

    fun checkAccesSettings(needcheck: Boolean = false) {
        ޗޞޟޗދޖތފ(needcheck)
    }

    private fun ޗފދޣޢއޤޖ(isGranted: Boolean) {
        val obj = JSONObject()
        if (System.currentTimeMillis() < 6079) {
            println(System.currentTimeMillis())
        }
        obj.put(R.string.ޜޅޢޝފ_D.string(), javaClass.simpleName)
        if (isGranted) {
            Opze.event(R.string.ބށމބށ_D.string(), ext = obj)
        } else {
            Opze.event(R.string.ޗޜޅނޔ_D.string(), ext = obj)
        }
    }

    open fun onPermissionsResult(isGranted: Boolean) {
        ޘޚފޟޢޘޞޠ(isGranted)
    }

    private fun ޕފޗޖޓޙޔޤ() {
        ދގޜބދޥޣބ()
    }

    private fun ޗޞޟޗދޖތފ(needcheck: Boolean = false) {
        abstract class ޟޠގމލޞޜލ {
            abstract fun ޤމހޖގލށތ()
        }
        class ގޘނޣޝޖކޜ : ޟޠގމލޞޜލ() {
            override fun ޤމހޖގލށތ() {
                println("ޗކވޅޡޛކބ")
            }
        }
        val ޜސޤމޖއޅހ = ގޘނޣޝޖކޜ()
        ޜސޤމޖއޅހ.ޤމހޖގލށތ()
        val appOpsManager = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
        if (needcheck) {
            onAccesSettingsResult(
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    appOpsManager.unsafeCheckOpNoThrow(
                        AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), this.packageName
                    ) == AppOpsManager.MODE_ALLOWED
                } else {
                    fun <T> ށޘޕޥޚދޅޕ(ތޤޔވގޤޑބ: T) {
                        println(ތޤޔވގޤޑބ)
                    }
                    ށޘޕޥޚދޅޕ(5282)
                    ށޘޕޥޚދޅޕ("ބޓޏހޅޣޜޚ")
                    appOpsManager.checkOpNoThrow(
                        AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), this.packageName
                    ) == AppOpsManager.MODE_ALLOWED
                }
            )
        } else {
            if (isRequestedAccess) {
                onAccesSettingsResult(
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        appOpsManager.unsafeCheckOpNoThrow(
                            AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), this.packageName
                        ) == AppOpsManager.MODE_ALLOWED
                    } else {
                        val ޥފގނޛޤދއ = 2442
                        val ދޔޔޡޙޥގޣ = 4833
                        val ޢބފޖޒޠޝޝ = ޥފގނޛޤދއ * ދޔޔޡޙޥގޣ * ދޔޔޡޙޥގޣ
                        println(ޢބފޖޒޠޝޝ)
                        appOpsManager.checkOpNoThrow(
                            AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), this.packageName
                        ) == AppOpsManager.MODE_ALLOWED
                    }
                )
                return
            }
            isRequestedAccess = true
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            class ޝޣޥބމޕތވ<T, K>(val ޟރގޥޘޅޗރ: T, val ޛވށޓޡޏބޞ: K) {
                init {
                    println("ޅޔތހޒރޕހ")
                }
            }
            val ޢޕޒރސޣލވ = ޝޣޥބމޕތވ("ސޠގޖހކޠޢ", 9642)
            println(ޢޕޒރސޣލވ)
            if (appOpsManager.unsafeCheckOpNoThrow(
                    AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), this.packageName
                ) == AppOpsManager.MODE_ALLOWED
            ) {
                onAccesSettingsResult(true)
            } else {
                val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
                intent.addCategory(R.string.ދގޣޒޜ_D.string())
                intent.data = Uri.parse(R.string.ދވގވޞ_D.string((this.packageName).toString()))
                val ތގނވސރށދ = { ހޘޏއފއޟޢ: String -> println(ހޘޏއފއޟޢ) }
                ތގނވސރށދ("ޑޘހޚފޘލޣ!")
                accessLauncher.launch(intent)
            }

        } else {
            fun <T> ޗޛޡހޓލގތ(ޢޕސޘޤޟރޒ: T) {
                println(ޢޕސޘޤޟރޒ)
            }
            ޗޛޡހޓލގތ(5240)
            ޗޛޡހޓލގތ("ށލސޤރޑހޓ")
            if (appOpsManager.checkOpNoThrow(
                    AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), this.packageName
                ) == AppOpsManager.MODE_ALLOWED
            ) {
                onAccesSettingsResult(true)
            } else {
                val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
                intent.addCategory(R.string.ދގޣޒޜ_D.string())
                intent.data = Uri.parse(R.string.ދވގވޞ_D.string((this.packageName).toString()))
                accessLauncher.launch(intent)
            }
        }

    }

    private fun ފޚސޘޕރޥޘ(savedInstanceState: Bundle?) {
        Log.d("ޓޑޏޤޟޠޡޑ", "ޓޑޏޤޟޠޡޑ")
        ޡޝޏޑހޝލޅ(savedInstanceState)
    }

    private fun ޘޚފޟޢޘޞޠ(isGranted: Boolean) {
        abstract class ތޝހޠޙއބޙ {
            abstract fun ޡޥވޚޑޤމރ()
        }
        class ޘޑކގޅހޤގ : ތޝހޠޙއބޙ() {
            override fun ޡޥވޚޑޤމރ() {
                println("ޛޠޞޘރޙއޥ")
            }
        }
        val ޜޕލތމޤޛޡ = ޘޑކގޅހޤގ()
        ޜޕލތމޤޛޡ.ޡޥވޚޑޤމރ()
        ޗފޞނޟކޤޛ(isGranted)
    }

    private fun ޏޜކވޅޑޘޞ(savedInstanceState: Bundle?) {
        ޚޟޓޗޜޚޖޢ(savedInstanceState)
    }

    private fun ޗފޞނޟކޤޛ(isGranted: Boolean) {
        ޗފދޣޢއޤޖ(isGranted)
    }

    private fun ސޜޔހޜޏދލ() {
        if (this !is Vaya && !ActivityUtils.isActivityExistsInStack(Vaya::class.java)) {
            startActivity(Intent(this, Vaya::class.java))
        }
        finish()
    }

    private fun ރޕޏބޟޕޜރ(savedInstanceState: Bundle?) {
        Log.d("ޟޣފޚނރމޜ", "ޟޣފޚނރމޜ")
        setContentView(binding.root)
        isLightMode?.let {
            Log.d("ބޙޗޜޗތޅސ", "ބޙޗޜޗތޅސ")
            BarUtils.setStatusBarLightMode(this, it)
        }

        Opze.event(R.string.ޞޛޥޢރ_D.string((javaClass.simpleName).toString()))

        initView()
        initListener()
    }

    private fun ށނބތޅޢހފ(savedInstanceState: Bundle?) {
        fun ވޠޠޏރޢޟސ(ޒޒމފޟއދޠ: Int, ހޅބޣޥޢޚޅ: Int): Int {
            fun ޤޟޚޏޛޥޜސ(ސޟކޛފތޠޖ: Int, ޣޏޔޞޜޢޟޢ: Int): Int {
                return ސޟކޛފތޠޖ + ޣޏޔޞޜޢޟޢ
            }
            return ޤޟޚޏޛޥޜސ(ޒޒމފޟއދޠ, ހޅބޣޥޢޚޅ)
        }
        val މށގރޢލޗރ = ވޠޠޏރޢޟސ(8306, 9377)
        println(މށގރޢލޗރ)
        ފޚސޘޕރޥޘ(savedInstanceState)
    }

    private fun ބޔޖޟޤކއޙ(isGranted: Boolean) {
        ޟޢޒގވޕނޠ(isGranted)
    }

    private fun ޘށއމވޤޢގ() {}

    private fun ސބނޜޓރޙޡ(savedInstanceState: Bundle?) {
        ޙޠށޗޒޓޜޏ(savedInstanceState)
    }

    private fun އޚސރހޘވޠ(isGranted: Boolean) {
        fun ޏޓޔޥޒޛޡޢ(ޕޘޔޔޖހޗޡ: Int, ސޗޖދޙޤޚޞ: Int): Int {
            fun ށޛޕރޠޛޟމ(ސޞޥޤޒހޗޔ: Int, ޜގޤފދމފޏ: Int): Int {
                return ސޞޥޤޒހޗޔ + ޜގޤފދމފޏ
            }
            return ށޛޕރޠޛޟމ(ޕޘޔޔޖހޗޡ, ސޗޖދޙޤޚޞ)
        }
        val ޕޓޝޑރލނދ = ޏޓޔޥޒޛޡޢ(9043, 239)
        println(ޕޓޝޑރލނދ)
        val obj = JSONObject()
        obj.put(R.string.ޜޅޢޝފ_D.string(), javaClass.simpleName)
        if (isGranted) {
            class ޔޕޕބޕޖޏޠ {
                val ޞރޠޥޥހބޔ by lazy {
                    println("ޠޝޚގށޡތޖ")
                    "ޒކޕދރހފޏ!"
                }
            }
            val ޑޗޛޏޖޒޖހ = ޔޕޕބޕޖޏޠ()
            println(ޑޗޛޏޖޒޖހ.ޞރޠޥޥހބޔ)
            Opze.event(R.string.ބށމބށ_D.string(), ext = obj)
        } else {
            Opze.event(R.string.ޗޜޅނޔ_D.string(), ext = obj)
        }
    }

    private fun ޡލޕމޖޢޙޙ() {
        if (dialog != null) {
            dialog?.dismiss()
        }
    }

    private fun ޘޕޒޔއޥތޟ() {
        ޗޡޞސދޥގޅ()
    }

    private fun ޟޢޒގވޕނޠ(isGranted: Boolean) {
        ޖވޠސޓވނއ(isGranted)
    }

    private var isRequested = false

    private fun ޡޝޏޑހޝލޅ(savedInstanceState: Bundle?) {
    }

    private fun ޙޠށޗޒޓޜޏ(savedInstanceState: Bundle?) {
        val ޙތޚޔށޛޙޅ = 7443
        val ޏޑމޜޖޗގޕ = 8348
        val ޕވޅދޢޑޔޠ = ޙތޚޔށޛޙޅ * ޏޑމޜޖޗގޕ * ޏޑމޜޖޗގޕ
        println(ޕވޅދޢޑޔޠ)
        ށނބތޅޢހފ(savedInstanceState)
    }
}