package com.base.appzxhy.ui.appuninstall

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import androidx.activity.viewModels
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.base.LottieEnum
import com.base.appzxhy.bean.FeatureBean.Companion.UNINSTALL_APP
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.business.ads.AdsShowCallBack
import com.base.appzxhy.databinding.ActivityAppUninstallBinding
import com.base.appzxhy.ui.cleanresult.CleanResultActivity
import com.base.appzxhy.utils.ToastUtils.toast

class AppUninstallActivity : BaseActivity<ActivityAppUninstallBinding>(ActivityAppUninstallBinding::inflate) {

    private val viewModel: AppUninstallViewModel by viewModels()


    private val adapter = AppAdapter()

    override fun initView() {
        super.initView()

        binding.rv.adapter = adapter

        showAdAnimation(LottieEnum.SCREENSHOT_LOTTIE) {

        }
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)

    }

    override fun initListener() {
        super.initListener()

        binding.ivBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvOk.setOnClickListener {
            CleanResultActivity.functionKey = UNINSTALL_APP
            CleanResultActivity.titleName = this.getString(R.string.app_uninstall)
            goToAc(CleanResultActivity::class.java)
            finish()
        }

        binding.llSize.setOnClickListener {
            binding.ivInstallTime.setImageResource(R.drawable.icon_paixu_normal)
            when (viewModel.sortMode) {
                0 -> {
                    viewModel.sortMode = 1
                    binding.ivSize.setImageResource(R.drawable.icon_paixu_up)
                }

                else -> {
                    viewModel.sortMode = 0
                    binding.ivSize.setImageResource(R.drawable.icon_paixu_down)
                }
            }

            viewModel.changeSort {
                adapter.submitList(viewModel.appList)
            }
        }

        binding.llInstallTime.setOnClickListener {
            binding.ivSize.setImageResource(R.drawable.icon_paixu_normal)
            when (viewModel.sortMode) {
                2 -> {
                    viewModel.sortMode = 3
                    binding.ivInstallTime.setImageResource(R.drawable.icon_paixu_up)
                }

                else -> {
                    viewModel.sortMode = 2
                    binding.ivInstallTime.setImageResource(R.drawable.icon_paixu_down)
                }
            }

            viewModel.changeSort {
                adapter.submitList(viewModel.appList)
            }
        }


        adapter.uninstallAction = { bean ->

            val uninstall = {
                try {
                    val deleteIntent = Intent(Intent.ACTION_DELETE).apply {
                        data = "package:${bean.packageName}".toUri()
                    }
                    launcher.launch(deleteIntent) {
                        if (!isAppInstalled(this, bean.packageName)) {
                            toast("uninstall success", true)
                            viewModel.appList.removeIf { it.packageName == bean.packageName }
                            viewModel.changeSort {
                                adapter.submitList(viewModel.appList)
                            }
                        }
                    }
                } catch (e: Exception) {
                    toast("uninstall error", true)
                }
            }

            AdsMgr.showInsert(this, showCallBack = object : AdsShowCallBack() {
                override fun next() {
                    uninstall.invoke()
                }
            })

        }

        viewModel.initData(this) {
            binding.llSize.callOnClick()
        }

    }

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            binding.flTop.setPadding(0, systemBars.top, 0, 0)
            insets
        }
    }

    fun isAppInstalled(context: Context, packageName: String): Boolean {
        val packageManager = context.packageManager
        return try {
            // 尝试获取应用信息
            packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
            true // 如果没有抛出异常，说明应用已安装
        } catch (e: PackageManager.NameNotFoundException) {
            false // 如果抛出异常，说明应用未安装
        }
    }

}