package pxds.hpls.xkxo

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.supercleanermax.R
import com.test.basd.supercleanermax.databinding.WbslxhBinding
import pxds.dlpf.buoz.fico.Ipnk.inflate
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.vazp.Rorl
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

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

    private var list = arrayListOf<Rorl>()

    fun removeData(appBean: Rorl) {
        list.remove(appBean)
        notifyDataSetChanged()
    }

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

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

        if (payloads.isEmpty()) {
            holder.binding.ggrnnov.text = data.appName
            holder.binding.yukpgfw.setImageDrawable(data.icIcon)

            val dateFormat = SimpleDateFormat(R.string.ޓބޕގޔ_D.string(), Locale.ENGLISH)
            val installTimeDate = Date(data.installTime)
            val installTime = String.format(R.string.ސޠމޝޛ_D.string(), installTimeDate) + " " + String.format(R.string.ސރޙވނ_D.string(), installTimeDate)
            holder.binding.utxveuj.text = R.string.ޕޛޙއޣ_D.string((installTime).toString())

            if (data.appSize == -1L) {
                holder.binding.eozdctv.text = R.string.ޙސހޒޛ_D.string()
            } else {
                val size = Formatter.formatFileSize(context, data.appSize)
                holder.binding.eozdctv.text = R.string.ރޛޓޤޑ_D.string((size).toString())
            }
            when (data.lastUsedTime) {
                -1L -> {
                    holder.binding.hzvifqb.text = R.string.އޟޜހޔ_D.string()
                }

                -2L -> {
                    holder.binding.hzvifqb.text = R.string.ދޝއޖޢ_D.string()
                }

                else -> {

                    val lastTimeDate = Date(data.lastUsedTime)
                    val lastTime = String.format(R.string.ސޠމޝޛ_D.string(), lastTimeDate) + " " + String.format(R.string.ސރޙވނ_D.string(), lastTimeDate)
                    holder.binding.hzvifqb.text = R.string.ޑކޞޒޑ_D.string((lastTime).toString())
                }
            }

            holder.binding.khqvveq.isSelected = data.isSelected
            holder.binding.khqvveq.setTrackedOnClickListener {
                data.isSelected = !data.isSelected
                holder.binding.khqvveq.isSelected = data.isSelected
                notifyItemChanged(position, R.string.ޣށޣޖވ_D.string())
                val selectedList = list.filter { it.isSelected }
                itemSelect?.invoke(selectedList)
            }
            holder.binding.root.setTrackedOnClickListener {
                itemClick.invoke(data)
            }
        } else {
            holder.binding.khqvveq.isSelected = data.isSelected
            super.onBindViewHolder(holder, position, payloads)
        }

    }

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

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

    override fun onBindViewHolder(holder: AAAA, position: Int) {

    }

    fun setData(dataList: List<Rorl>) {
        list.clear()
        list.addAll(dataList)
        notifyDataSetChanged()
    }
}