package importantedto.appliededto.surgeonedto.promiseedto.petedto

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.graphics.Color
import android.net.Uri
import android.provider.Settings
import android.view.View
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.AdmitedtoBinding
import importantedto.admireedto.sweptedto.Differenceedto
import importantedto.admireedto.sweptedto.Differenceedto.showBackInterAdSp
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.concreteedto.cameedto.Ridgeedto
import importantedto.deserveedto.manufactureredto.viableedto.Factoredto
import importantedto.gallonedto.Intentionedto.ramPair
import importantedto.gallonedto.Transcendedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showAppProcessExitDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class Negligenceedto : Murderedto<AdmitedtoBinding>() {

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

    private lateinit var adapter: Factoredto

    private var ePercent = 0

    @SuppressLint("SetTextI18n")
    override fun initView() {
        Transcendedto.setStatusBarLightMode(this, true)
        Transcendedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Transcendedto.getStatusBarHeight())

        val ramPair = ramPair()
        val percent = ((ramPair.first.toFloat() / ramPair.second.toFloat()) * 100).toInt()
        ePercent = percent
        binding.zoneedto.text = percent.toString()
        binding.brotheredto.progress = percent
        binding.ignoranceedto.text = "${ramPair.first.toFormatSize()}/${ramPair.second.toFormatSize()}"

        adapter = Factoredto { pkg ->
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            val uri = Uri.fromParts("package", pkg, null) // 替换为需要跳转的应用包名
            intent.setData(uri)
            startActivity(intent)
        }
        binding.questedto.adapter = adapter

        initData()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showAppProcessExitDialog(ePercent) {
                if (showBackInterAdSp()) {
                    Differenceedto.admobMaxShowInterstitialAd(this@Negligenceedto, false) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }
        }
        binding.bangedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
    }

    @SuppressLint("QueryPermissionsNeeded")
    private fun initData() = lifecycleScope.launch(Dispatchers.IO) {
        val pm = packageManager
        val packages = pm.getInstalledPackages(0)
        val list = arrayListOf<Ridgeedto>()

        packages.forEach { app ->
            if (isLaunchApp(this@Negligenceedto, app)) {

                val appBean = Ridgeedto(
                    app.applicationInfo.loadIcon(pm),
                    app.applicationInfo.loadLabel(pm).toString(),
                    app.applicationInfo.packageName
                )
                list.add(appBean)
            }
        }
        launch(Dispatchers.Main) {
            binding.notoriousedto.visibility = View.GONE
            adapter.setData(list)
        }
    }

    private fun isLaunchApp(context: Context, app: PackageInfo, filterSystem: Boolean = true): Boolean {

        val flagSystem = (app.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) < 1
        val flag = if (filterSystem) flagSystem else true

        return flag && app.applicationInfo.packageName != context.packageName
    }

}