package com.zxhyis.toolss.ui.page.notifaction

import android.annotation.SuppressLint
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.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.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.ad.ConfigHelper
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.cx.FileHelps.toFormatTime
import com.zxhyis.toolss.databinding.ActivityLayoutNotificationCleanBinding
import com.zxhyis.toolss.logic.setOnClick
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch


class NotificationCleanActivity : TopActivity<ActivityLayoutNotificationCleanBinding>() {
    override fun getViewBinding1(): ActivityLayoutNotificationCleanBinding {
        return ActivityLayoutNotificationCleanBinding.inflate(layoutInflater)
    }

    private val historyDao by lazy {
        HistoryDatabase.getInstance(this)?.historyDao()
    }
    private val notifList = mutableListOf<NotificationData>()
    private var isRequest = 0
    override fun onStart() {
        super.onStart()
        //Log.e("MXL", "onStart: " + ConfigHelper.isOpenNotification)
        vb.idNeedNotifcationPremison.isVisible = !ConfigHelper.isOpenNotification
        vb.idImgShezhi.isVisible = false
        if (ConfigHelper.isOpenNotification) {
            isRequest += 1
            playAnimal()
        }
    }

    private fun playAnimal() {
        if (isRequest == 1) {
            vb.idLlAnimation.isVisible = true
            vb.root.postDelayed({
                vb.idLlAnimation.isVisible = false
                AdmobUtils.showInterstitialAd(this)
            }, 6000)
        }
    }

    override fun init() {

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

        vb.idGant.setOnClick(javaClass.simpleName) {
            val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
            startActivity(intent)
        }
        val intent = Intent(this, AppNotificationListenerService::class.java)
        startService(intent)
        vb.idBackNotification.setOnClick(javaClass.simpleName) {
            finishToMain()
        }
        vb.idTvCleanAllT.setOnClick(javaClass.simpleName) {
            AdmobUtils.showInterstitialAd(this) {
                MainScope().launch(Dispatchers.IO) {
                    historyDao?.deleteAll()

                }

                mAdapter.notifyDataSetChanged()
                showEmptydata()
                // startActivity(Intent(this,Re))
            }


        }
        vb.idImgShezhi.setOnClick(javaClass.simpleName) {
            startActivity(Intent(this, NotifSettingActivity::class.java))
        }

        historyDao?.getAll()?.observe(this) {
            if (it.isNotEmpty()) {
                vb.idNoNotification.isVisible = false
                vb.idNotifyClean.isVisible = true
                vb.idViewLine.isVisible = true
                vb.idTvCleanAllT.isVisible = true
                notifList.clear()
                notifList.addAll(it)
                vb.idNotifyClean.run {
                    layoutManager = LinearLayoutManager(this@NotificationCleanActivity)
                    adapter = mAdapter
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showEmptydata()
            }

        }
//        val list = NotifSqliteDatebase(this).listContacts()
        //   Log.e("MXL", "init: ."+list.size )
    }

    private fun showEmptydata() {
        vb.idNotifyClean.isVisible = false
        vb.idNoNotification.isVisible = true
        vb.idViewLine.isVisible = false
        vb.idTvCleanAllT.isVisible = false
    }

    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.id_t_icon)
                tvTitle = view.findViewById(R.id.id_title)
                tvContent = view.findViewById(R.id.id_content)
                tvTime = view.findViewById(R.id.id_time)
                ivBig = view.findViewById(R.id.id_big_img)
            }
        }

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

            override fun getItemCount() = notifList.size

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

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

                }

            }

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