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

import android.annotation.SuppressLint
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.ConfigHelper
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.NoticationSettingBinding
import com.zxhyis.toolss.logic.setOnClick
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

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

    private val appList = mutableListOf<AppUtils.AppInfo>()
    override fun init() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        vb.idBackSetting.setOnClick(javaClass.simpleName) {
            finish()
        }
        vb.notifSwitch.setOnCheckedChangeListener { p0, p1 ->
            Log.e("MXL", "init: " + p1)
            if (!p1) {

            }

        }
        if (ConfigHelper.appList.isNullOrEmpty()) {
            MainScope().launch(Dispatchers.Main) {
                launch(Dispatchers.IO) {
                    ConfigHelper.appList = AppUtils.getAppsInfo().shuffled()
                }
                appList.addAll(ConfigHelper.appList?.filter { !it.isSystem } ?: mutableListOf())
                vb.idSettingRl.run {
                    layoutManager = LinearLayoutManager(context)
                    adapter = mAdapter
                }
            }
        } else {
            appList.addAll(ConfigHelper.appList?.filter { !it.isSystem } ?: mutableListOf())
            vb.idSettingRl.run {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter
            }
        }
    }

    private val mAdapter by lazy {
        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val ivImage: ImageView
            val tvName: TextView?
            val ivSwitch: SwitchCompat

            init {
                ivImage = view.findViewById(R.id.id_app_icon)
                tvName = view.findViewById(R.id.id_app_name)
                ivSwitch = view.findViewById(R.id.notif_switch)
            }
        }

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

            override fun getItemCount() = appList.size

            @SuppressLint("RecyclerView")
            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = appList[position]
                holder.ivImage.setImageDrawable(item.icon)
                holder.tvName?.text = item.name
                holder.ivSwitch.setOnCheckedChangeListener { p0, p1 ->
                    if(!p1){
                      //  Log.e("MXL", "关闭当前通知: ", )

                    }
                }


            }

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


            }
        }
    }
}