package pxds.dlpf.buoz.fico

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.AppUtils
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.LflbsdBinding
import pxds.etmo.Ntfp
import pxds.lmmd.zmwf.Hoph
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.vazp.Rorl
import pxds.vazp.Rorl.Companion.appBeanGson
import java.text.SimpleDateFormat

object Vzaf {

    private val TAG = R.string.ދއގޣޞ_D.string()

    fun Activity.showAppDetailDialog(
        appBean: Rorl, launcher: Hoph, unInstallAction: ((unInstalled: Boolean) -> Unit)? = null
    ) {
        val dialog = BottomSheetDialog(this)
        val binding = LflbsdBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()

        binding.ggrnnov.text = appBean.appName
        binding.bgcdlcf.text = appBean.pkg
        binding.yukpgfw.setImageDrawable(appBean.icIcon)

        binding.dadknye.text = AppUtils.getAppVersionName(appBean.pkg)

        val size = Formatter.formatFileSize(this, appBean.appSize)
        binding.wkrduju.text = size

        val dateFormat = SimpleDateFormat(R.string.ޟމވޟށ_D.string())
        val installTime = dateFormat.format(appBean.installTime)
        binding.jqvuzka.text = installTime

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                val installSourceInfo = packageManager.getInstallSourceInfo(appBean.pkg)

                if (R.string.ޟޛޟދގ_D.string() == installSourceInfo.installingPackageName) {
                    binding.dvaihxm.text = getString(R.string.ޕޘހފޙ)
                } else {
                    binding.jyewzwh.visibility = View.GONE
                }
            } else {
                binding.jyewzwh.visibility = View.GONE
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        val lp = dialog.window?.attributes
        lp?.width = ViewGroup.LayoutParams.MATCH_PARENT
        lp?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = lp
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        binding.tpofgjm.setTrackedOnClickListener {
            val intent = Intent()
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            intent.setData(Uri.parse(R.string.ނވވށޔ_D.string() + appBean.pkg))
            startActivity(intent)
            dialog.dismiss()
        }
        binding.dpcvyzv.setTrackedOnClickListener {
            dialog.dismiss()
            val uri = R.string.ޢޕކހހ_D.string((appBean.pkg).toString())

            val intentView = Intent(Intent.ACTION_VIEW)
            intentView.setData(Uri.parse(uri))

            if (intentView.resolveActivity(packageManager) != null) {

                startActivity(intentView)
            } else {

                val intent = Intent()
                intent.setAction(Intent.ACTION_SEND)
                intent.putExtra(Intent.EXTRA_TEXT, uri)
                intent.setType(R.string.ޗތޚނޅ_D.string())
                startActivity(intent)
            }
        }

        binding.arlhadt.setTrackedOnClickListener {
            dialog.dismiss()
            val newIntent = Intent(this, Ntfp::class.java)

            val json = appBeanGson.toJson(appBean)
            newIntent.putExtra(R.string.ހޒށށޛ_D.string(), json)
            startActivity(newIntent)
        }
        binding.mrsyiad.setTrackedOnClickListener {
            dialog.dismiss()

            val intent = Intent(Intent.ACTION_DELETE, Uri.parse(R.string.ޙތޛޥބ_D.string((appBean.pkg).toString())))
            launcher.launch(intent) {
                unInstallAction?.invoke(!isInstalled(this, appBean.pkg))
            }
        }

    }

    private fun isInstalled(context: Context, pkg: String, error: (() -> Unit)? = null): Boolean {
        return try {
            context.packageManager.getPackageInfo(pkg, 0)
            true
        } catch (e: Exception) {
            error?.invoke()
            false
        }
    }
}