package com.test.basd.smartjunkcleaner.adapter

import android.annotation.SuppressLint
import android.text.format.Formatter
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.test.basd.smartjunkcleaner.R
import com.test.basd.smartjunkcleaner.bean.AppBean
import com.test.basd.smartjunkcleaner.databinding.ItemAppListBinding
import com.test.basd.smartjunkcleaner.view.XmlEx.inflate
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class AppListAdapter(
    val itemClick: (data: AppBean) -> Unit, val itemSelect: ((selectList: List<AppBean>) -> Unit)? = null
) : RecyclerView.Adapter<AppListAdapter.AAAA>() {


    private var list = arrayListOf<AppBean>()

    inner class AAAA(view: View) : ViewHolder(view) {
        val binding = ItemAppListBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AAAA {
        return AAAA(R.layout.item_app_list.inflate(parent))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    @SuppressLint("SimpleDateFormat")
    override fun onBindViewHolder(holder: AAAA, position: Int) {

    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: AAAA, position: Int, payloads: MutableList<Any>) {
        val data = list[position]
        val context = holder.itemView.context

        if (payloads.isEmpty()) {
            holder.binding.tvAppName.text = data.appName
            holder.binding.ivIcon.setImageDrawable(data.icIcon)

            val dateFormat = SimpleDateFormat("MM dd", Locale.ENGLISH)
            val installTimeDate = Date(data.installTime)
            val installTime = String.format("%tb", installTimeDate) + " " + String.format("%te", installTimeDate)
            holder.binding.tvAppInstall.text = "Installed: $installTime"

            if (data.appSize == -1L) {
                holder.binding.tvAppSize.text = "Size used: need permission"
            } else {
                val size = Formatter.formatFileSize(context, data.appSize)
                holder.binding.tvAppSize.text = "Size used: $size"
            }
            when (data.lastUsedTime) {
                -1L -> {
                    holder.binding.tvAppLastUse.text = "Last use: need permission"
                }

                -2L -> {
                    holder.binding.tvAppLastUse.text = "Last use: more 30 days no use"
                }

                else -> {
//                    val lastTime = dateFormat.format(data.lastUsedTime)
                    val lastTimeDate = Date(data.lastUsedTime)
                    val lastTime = String.format("%tb", lastTimeDate) + " " + String.format("%te", lastTimeDate)
                    holder.binding.tvAppLastUse.text = "Last use: $lastTime"
                }
            }


            holder.binding.ivSelector.isSelected = data.isSelected
            holder.binding.ivSelector.setOnClickListener {
                data.isSelected = !data.isSelected
                holder.binding.ivSelector.isSelected = data.isSelected
                notifyItemChanged(position, "sdadd")
                val selectedList = list.filter { it.isSelected }
                itemSelect?.invoke(selectedList)
            }
            holder.binding.root.setOnClickListener {
                itemClick.invoke(data)
            }
        } else {
            holder.binding.ivSelector.isSelected = data.isSelected
            super.onBindViewHolder(holder, position, payloads)
        }


    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(dataList: List<AppBean>) {
        list.clear()
        list.addAll(dataList)
        notifyDataSetChanged()
    }


    @SuppressLint("NotifyDataSetChanged")
    fun removeData(appBean: AppBean) {
        list.remove(appBean)
        notifyDataSetChanged()
    }
}