package com.zxhyis.toolss.ui.page.appmanager

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.zxhyis.toolss.R
import com.zxhyis.toolss.databinding.ItemAppListBinding
import com.zxhyis.toolss.logic.AppBean
import com.zxhyis.toolss.logic.XmlEx.inflate
import com.zxhyis.toolss.logic.setOnClick
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

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) {

    }

    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 = context.getString(R.string.install_time_f, installTime)

            val needPermission = context.resources.getString(R.string.need_permission)
            if (data.appSize == -1L) {
                holder.binding.tvAppSize.text = context.getString(R.string.app_space_size_f, needPermission)
            } else {
                val size = Formatter.formatFileSize(context, data.appSize)
                holder.binding.tvAppSize.text = context.getString(R.string.app_space_size_f, size)
            }
            when (data.lastUsedTime) {
                -1L -> {
                    holder.binding.tvAppLastUse.text = context.getString(R.string.last_time_f, needPermission)
                }

                -2L -> {
                    val noUse = context.getString(R.string.more_30_days_no_use)
                    holder.binding.tvAppLastUse.text = context.getString(R.string.last_time_f, noUse)
                }

                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 = context.getString(R.string.last_time_f, lastTime)
                }
            }


            holder.binding.ivSelector.isSelected = data.isSelected
            holder.binding.ivSelector.setOnClick(javaClass.simpleName) {
                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.setOnClick(javaClass.simpleName) {
                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()
    }


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