package rowedto.jockeyedto.sheetedto.atticedto.salonedto.safetyedto.makeupedto

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.FededtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showAppProcessExitDialog
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Toneedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Suitcaseedto.toFormatSize
import rowedto.jockeyedto.sheetedto.meadowedto.naturaledto.idealedto.Nominationedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Nodeedto.ramPair
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto.showBackInterAdSp


class Conceiveedto : Crossingedto<FededtoBinding>() {

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

    private lateinit var adapter: Nominationedto

    private var ePercent = 0

    @SuppressLint("SetTextI18n")
    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Spareedto.getStatusBarHeight())
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
        val ramPair = ramPair()
        val percent = ((ramPair.first.toFloat() / ramPair.second.toFloat()) * 100).toInt()
        ePercent = percent
        binding.vainedto.text = percent.toString()
        binding.modernizeedto.progress = percent
        binding.bulletinedto.text = "${ramPair.first.toFormatSize()}/${ramPair.second.toFormatSize()}"

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

        initData()
    }

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

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

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

                val appBean = Toneedto(
                    app.applicationInfo.loadIcon(pm),
                    app.applicationInfo.loadLabel(pm).toString(),
                    app.applicationInfo.packageName
                )
                list.add(appBean)
            }
        }
        launch(Dispatchers.Main) {
            binding.toweredto.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
    }

}