package pxds.etmo

import android.Manifest
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.NameNotFoundException
import android.net.Uri
import android.provider.Settings
import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.GmnhbxBinding
import pxds.dlpf.buoz.fico.Yung.showPermissionFilterDialog
import pxds.hpls.xkxo.Fkee
import pxds.lmmd.zmwf.Bpfw
import pxds.lmmd.zmwf.Egac
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.vazp.Mrzg
import pxds.vazp.Mrzg.Companion.setDesc
import pxds.vazp.Mrzg.Companion.setTittle
import pxds.vazp.Rorl

class Ntfp : Bpfw<GmnhbxBinding>() {

    override val binding: GmnhbxBinding by lazy {
        GmnhbxBinding.inflate(layoutInflater)
    }

    private val appSensitivePermissions = arrayListOf<Mrzg>()

    private val appNormalPermissions = arrayListOf<Mrzg>()

    private lateinit var sensitiveAdapter: Fkee

    private lateinit var normalAdapter: Fkee

    private var isAllPermission = true

    private var sensitiveExpand = true

    private var normalExpand = true

    private var appBean: Rorl? = null

    private val TAG = R.string.ޑވޓޜހ_D.string()

    override val isLightMode = true

    override fun initView() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.fosyiss)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val json = intent.extras?.getString(R.string.ހޒށށޛ_D.string())
        Egac.logDebug(TAG, R.string.ހޖޤގގ_D.string((json).toString()))
        appBean = json?.let { Rorl.appBeanGson.fromJson(it, Rorl::class.java) }
        Egac.logDebug(TAG, R.string.އޟޏޟއ_D.string((appBean).toString()))

        appBean?.apply {
            val applicationInfo = packageManager.getApplicationInfo(this.pkg, PackageManager.GET_META_DATA)
            binding.yukpgfw.setImageDrawable(packageManager.getApplicationIcon(applicationInfo))
            binding.ivrxehl.text = this.appName
            getAppPermissions(this.pkg)
        }
    }

    override fun initListener() {
        binding.ngdqagq.setTrackedOnClickListener {
            finish()
        }
        binding.fdhvnvb.setTrackedOnClickListener {
            val intent = Intent()
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            intent.setData(Uri.parse(R.string.ނވވށޔ_D.string() + appBean?.pkg))
            startActivity(intent)
        }
        binding.fstvzit.setTrackedOnClickListener {
            normalExpand = !normalExpand
            setExpandCollapseNormal()
        }
        binding.iifajof.setTrackedOnClickListener {
            sensitiveExpand = !sensitiveExpand
            setExpandCollapseSensitive()
        }
        binding.mdykyou.setTrackedOnClickListener {
            showPermissionFilterDialog(
                allClick = {
                    if (!isAllPermission) {
                        changeAllPermission()
                        normalExpand = true
                        setExpandCollapseNormal()
                    }
                    isAllPermission = true
                }, authorizedClick = {
                    if (isAllPermission) {
                        changeOnlyAuthorized()
                        sensitiveExpand = true
                        setExpandCollapseSensitive()
                    }
                    isAllPermission = false
                }
            )
        }
    }

    companion object {

        val commonPermissions = arrayListOf(
            Mrzg(Manifest.permission.ACCESS_NETWORK_STATE), Mrzg(Manifest.permission.ACCESS_WIFI_STATE), Mrzg(Manifest.permission.BLUETOOTH), Mrzg(Manifest.permission.BLUETOOTH_ADMIN), Mrzg(Manifest.permission.BLUETOOTH_CONNECT), Mrzg(Manifest.permission.BLUETOOTH_PRIVILEGED), Mrzg(Manifest.permission.BLUETOOTH_SCAN), Mrzg(Manifest.permission.CHANGE_NETWORK_STATE), Mrzg(Manifest.permission.CHANGE_WIFI_STATE), Mrzg(Manifest.permission.CONTROL_LOCATION_UPDATES), Mrzg(Manifest.permission.DELETE_PACKAGES), Mrzg(Manifest.permission.INSTALL_PACKAGES), Mrzg(Manifest.permission.INTERNET), Mrzg(Manifest.permission.MANAGE_EXTERNAL_STORAGE), Mrzg(Manifest.permission.MANAGE_MEDIA), Mrzg(Manifest.permission.MANAGE_WIFI_NETWORK_SELECTION), Mrzg(Manifest.permission.MODIFY_AUDIO_SETTINGS), Mrzg(Manifest.permission.NFC), Mrzg(Manifest.permission.READ_EXTERNAL_STORAGE), Mrzg(Manifest.permission.READ_MEDIA_AUDIO), Mrzg(Manifest.permission.READ_MEDIA_IMAGES), Mrzg(Manifest.permission.READ_MEDIA_VIDEO), Mrzg(Manifest.permission.READ_SYNC_SETTINGS), Mrzg(Manifest.permission.READ_SYNC_STATS), Mrzg(Manifest.permission.REQUEST_DELETE_PACKAGES), Mrzg(Manifest.permission.UPDATE_PACKAGES_WITHOUT_USER_ACTION), Mrzg(Manifest.permission.USE_FINGERPRINT), Mrzg(Manifest.permission.WRITE_EXTERNAL_STORAGE), Mrzg(Manifest.permission.WRITE_SETTINGS), Mrzg(Manifest.permission.WRITE_SYNC_SETTINGS), )

        val sensitivePermissions = arrayListOf(
            Mrzg(Manifest.permission.ACCESS_BACKGROUND_LOCATION, isSensitive = true), Mrzg(Manifest.permission.ACCESS_COARSE_LOCATION, isSensitive = true), Mrzg(Manifest.permission.ACCESS_FINE_LOCATION, isSensitive = true), Mrzg(Manifest.permission.ACCESS_MEDIA_LOCATION, isSensitive = true), Mrzg(Manifest.permission.CAMERA, isSensitive = true), Mrzg(Manifest.permission.RECORD_AUDIO, isSensitive = true), Mrzg(Manifest.permission.RECEIVE_BOOT_COMPLETED, isSensitive = true), Mrzg(Manifest.permission.READ_CONTACTS, isSensitive = true), Mrzg(Manifest.permission.WRITE_CONTACTS, isSensitive = true), Mrzg(Manifest.permission.GET_ACCOUNTS, isSensitive = true), Mrzg(Manifest.permission.READ_PHONE_STATE, isSensitive = true), Mrzg(Manifest.permission.READ_PHONE_NUMBERS, isSensitive = true), Mrzg(Manifest.permission.CALL_PHONE, isSensitive = true), Mrzg(Manifest.permission.READ_CALL_LOG, isSensitive = true), Mrzg(Manifest.permission.WRITE_CALL_LOG, isSensitive = true), Mrzg(Manifest.permission.ANSWER_PHONE_CALLS, isSensitive = true), Mrzg(Manifest.permission.BODY_SENSORS, isSensitive = true), Mrzg(Manifest.permission.ACTIVITY_RECOGNITION, isSensitive = true), Mrzg(Manifest.permission.SEND_SMS, isSensitive = true), Mrzg(Manifest.permission.RECEIVE_SMS, isSensitive = true), Mrzg(Manifest.permission.READ_SMS, isSensitive = true), Mrzg(Manifest.permission.READ_CALENDAR, isSensitive = true), Mrzg(Manifest.permission.WRITE_CALENDAR, isSensitive = true), Mrzg(Manifest.permission.QUERY_ALL_PACKAGES, isSensitive = true), )

    }

    private fun getAppPermissions(pkg: String) {
        val pm = this.packageManager
        val info: PackageInfo
        try {
            info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS)
            val packagePermissions: Array<String>? = info.requestedPermissions ?: null
            packagePermissions?.forEach { permission ->
                Egac.logDebug(TAG, R.string.ޟޚޥނއ_D.string((permission).toString()))
                val common = commonPermissions.find { it.permissionMatch == permission }
                common?.let {
                    val ss = pm.checkPermission(it.permissionMatch, pkg) == PackageManager.PERMISSION_GRANTED
                    if (it.permissionMatch == Manifest.permission.INTERNET) {
                        Egac.logDebug(TAG, R.string.ޗޙނގޝ_D.string((ss).toString()))
                    }
                    it.isAuthorized = ss
                    appNormalPermissions.add(it)
                }

                val sensitive = sensitivePermissions.find { it.permissionMatch == permission }
                sensitive?.let {
                    it.isAuthorized = pm.checkPermission(it.permissionMatch, pkg) == PackageManager.PERMISSION_GRANTED
                    appSensitivePermissions.add(it)
                }

            }

        } catch (e: NameNotFoundException) {
            e.printStackTrace()
        }

        binding.iiyuoks.text = R.string.ދގޙގޔ_D.string((appSensitivePermissions.size).toString())
        appNormalPermissions.forEach {
            it.setTittle()
            it.setDesc()
        }
        sensitivePermissions.forEach {
            it.setTittle()
            it.setDesc()
        }
        binding.nvxqemg.text = appNormalPermissions.size.toString()
        binding.wyxyteb.text = appSensitivePermissions.size.toString()
        intiRv()
    }

    private fun changeOnlyAuthorized() {
        val list1 = appNormalPermissions.filter { it.isAuthorized }
        normalAdapter.setData(list1)
        binding.nvxqemg.text = list1.size.toString()

        val list2 = appSensitivePermissions.filter { it.isAuthorized }
        sensitiveAdapter.setData(list2)
        binding.wyxyteb.text = list2.size.toString()
    }

    private fun setExpandCollapseNormal() {
        if (normalExpand) {
            binding.iycrovo.setImageResource(R.mipmap.hxybbzue)
            binding.herscyb.visibility = View.VISIBLE
        } else {
            binding.iycrovo.setImageResource(R.mipmap.nbyrlxqu)
            binding.herscyb.visibility = View.GONE
        }

    }

    private fun changeAllPermission() {
        normalAdapter.setData(appNormalPermissions)
        binding.nvxqemg.text = appNormalPermissions.size.toString()

        sensitiveAdapter.setData(appSensitivePermissions)
        binding.wyxyteb.text = appSensitivePermissions.size.toString()
    }

    private fun intiRv() {
        normalAdapter = Fkee()
        binding.herscyb.adapter = normalAdapter
        normalAdapter.setData(appNormalPermissions)

        sensitiveAdapter = Fkee()
        binding.ehbkdwj.adapter = sensitiveAdapter
        sensitiveAdapter.setData(appSensitivePermissions)

        setExpandCollapseSensitive()
        setExpandCollapseNormal()
    }

    private fun setExpandCollapseSensitive() {
        if (sensitiveExpand) {
            binding.zirzrov.setImageResource(R.mipmap.lofeoqgv)
            binding.ehbkdwj.visibility = View.VISIBLE
        } else {
            binding.zirzrov.setImageResource(R.mipmap.zhskhpqt)
            binding.ehbkdwj.visibility = View.GONE
        }
    }
}