package pxds.dlpf.buoz.fico

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.view.Gravity
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.KkgktsBinding
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string

class Shzt(private val activity: Activity) : PopupWindow() {

    private val binding by lazy {
        KkgktsBinding.inflate(activity.layoutInflater)
    }

    private var star = 5

    companion object {
        fun show(activity: Activity) {
            val n = SPUtils.getInstance().getInt(R.string.ޣތޥބޚ_D.string(), 0)
            SPUtils.getInstance().put(R.string.ޣތޥބޚ_D.string(), n + 1)
            if (SPUtils.getInstance().getBoolean(R.string.ނޗކކޚ_D.string(), false)) {
                return
            }
            if (n > 0 && n % 8 == 0) {
                Shzt(activity).show()
            } else {
                return
            }

        }
    }

    override fun dismiss() {
        super.dismiss()
        BarUtils.setStatusBarLightMode(activity, true)
    }

    init {
        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT
        isOutsideTouchable = false
        isFocusable = true
        isClippingEnabled = false
    }

    private fun setStar(star: Int) {
        this.star = star
        listOf(
            binding.mixkhxz, binding.aahjlfv, binding.bsjcyxc, binding.wgkegsi, binding.oazvabb
        ).forEachIndexed { index, it ->
            it.setImageResource(
                if (index <= star) {
                    R.mipmap.zakylxcp
                } else {
                    R.mipmap.dheyoxfo
                }
            )

        }
    }

    fun show() {
        contentView = binding.root
        activity.window.decorView.post {
            binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
            BarUtils.setStatusBarLightMode(activity, false)
            showAtLocation(activity.window.decorView, Gravity.CENTER, 0, 0)
        }

        listOf(
            binding.mixkhxz, binding.aahjlfv, binding.bsjcyxc, binding.wgkegsi, binding.oazvabb
        ).forEachIndexed { index, it ->
            it.setTrackedOnClickListener {
                setStar(index)
            }
        }

        binding.dghrucr.setTrackedOnClickListener {
            dismiss()
        }

        binding.qsbqjwb.setTrackedOnClickListener {
            if (star > 0) {
                if (star >= 5) {
                    SPUtils.getInstance().put(R.string.ނޗކކޚ_D.string(), true)
                    try {
                        val uri = Uri.parse(R.string.ޘޘޣނރ_D.string() + activity.packageName)
                        val intent = Intent(Intent.ACTION_VIEW, uri)
                        activity.startActivity(intent)
                    } catch (_: Exception) {
                    }
                }
                dismiss()
            }

        }
    }
}