package pxds.apch.ldfl.lijc

import android.content.Intent
import android.graphics.Color
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.GaqddaBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import pxds.lmmd.zmwf.Bpfw
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.toFormatTime
import pxds.lmmd.zmwf.Uawv
import pxds.lmmd.zmwf.jyyf.hwbn.wzxb.Utdc
import kotlin.random.Random

class Kpde : Bpfw<GaqddaBinding>() {

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

    private val notificationList = mutableListOf<Kwiv>()

    private val historyDao by lazy {
        Njwa.getInstance(this)?.historyDao()
    }

    private val mAdapter by lazy {
        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val ivImage: ImageView
            val tvTitle: TextView
            val tvContent: TextView
            val tvTime: TextView
            val ivBig: ImageView

            init {
                ivImage = view.findViewById(R.id.mtigqig)
                tvTitle = view.findViewById(R.id.krnnbke)
                tvContent = view.findViewById(R.id.xouqnjd)
                tvTime = view.findViewById(R.id.xvefiqu)
                ivBig = view.findViewById(R.id.dgqbutn)
            }
        }

        object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val view = LayoutInflater.from(this@Kpde).inflate(
                    R.layout.cucqss, parent, false
                )
                return ViewHolder(view)
            }

            override fun getItemCount() = notificationList.size

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val c = Uawv.appList?.firstOrNull {
                    it.packageName == notificationList[position].packageName
                }
                holder.ivImage.setImageDrawable(c?.icon)
                if (notificationList[position].notificationText.isNotBlank()) {
                    holder.tvContent.visibility = View.VISIBLE
                    holder.tvContent.text = notificationList[position].notificationText
                } else {
                    holder.tvContent.visibility = View.GONE
                }
                holder.tvTitle.text = notificationList[position].notificationTitle

                holder.tvTime.text = notificationList[position].notificaonTime.toFormatTime()
                holder.itemView.setTrackedOnClickListener {
                    MainScope().launch(Dispatchers.IO) {
                        historyDao?.delete(notificationList[position])
                    }
                    notifyDataSetChanged()
                    val intent = packageManager.getLaunchIntentForPackage(notificationList[position].packageName)
                    if (intent != null) {
                        startActivity(intent)
                    }

                }

            }

            override fun onBindViewHolder(
                holder: ViewHolder, position: Int, payloads: MutableList<Any>
            ) {
                super.onBindViewHolder(holder, position, payloads)
            }
        }
    }

    private var isRequest = 0

    private fun showEmptydata() {
        binding.jmrulan.isVisible = false
        binding.sizhtof.isVisible = true
        binding.jdfvrkq.isVisible = false
        binding.ecvtujg.isVisible = false
    }

    override fun onStart() {
        super.onStart()
        val intent = Intent(this, Pmeh::class.java)
        startService(intent)
        binding.abqoyfh.isVisible = !Uawv.isOpenNotification
        binding.gbciaxv.isVisible = false
        if (Uawv.isOpenNotification) {
            isRequest += 1
            playAnimal()
        }
    }

    override fun initView() {

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        binding.qwohrnn.setTrackedOnClickListener {
            val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
            startActivity(intent)
        }

        binding.ptvobzc.setTrackedOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                Utdc.showInterstitialAd(this@Kpde){
                    finishToMain()
                }

            }
        })
        binding.ecvtujg.setTrackedOnClickListener {
            Utdc.showInterstitialAd(this) {
                MainScope().launch(Dispatchers.IO) {
                    historyDao?.deleteAll()

                }

                mAdapter.notifyDataSetChanged()
                showEmptydata()
            }

        }

        historyDao?.getAll()?.observe(this) {
            if (it.isNotEmpty()) {
                binding.sizhtof.isVisible = false
                binding.jmrulan.isVisible = true
                binding.jdfvrkq.isVisible = true
                binding.ecvtujg.isVisible = true
                notificationList.clear()
                notificationList.addAll(it)
                binding.jmrulan.run {
                    layoutManager = LinearLayoutManager(this@Kpde)
                    adapter = mAdapter
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showEmptydata()
            }
        }
    }

    private fun playAnimal() {
        if (isRequest == 1) {
            binding.albgbfe.isVisible = true
            binding.root.postDelayed({
                binding.albgbfe.isVisible = false
                Utdc.showInterstitialAd(this)
            }, Random.nextLong(4000, 6000))
        }
    }
}