package com.gh.gamecenter.cloudarchive

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.utils.formatTime
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.ItemArchiveLimitBinding
import com.gh.gamecenter.entity.ArchiveEntity
import java.text.SimpleDateFormat
import java.util.*

class ArchiveLimitAdapter(context: Context) : ListAdapter<ArchiveLimitAdapter.ArchiveItem>(context) {

    private var currentSelectedPosition = 0

    public override fun setListData(updateData: MutableList<ArchiveItem>?) {
        // 默认定一个为选中状态
        updateData?.firstOrNull()?.isChecked = true
        super.setListData(updateData)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ArchiveLimitViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ArchiveLimitViewHolder) {
            val item = mEntityList[position]
            holder.binding.tvTitle.text = item.data.name
            holder.binding.tvTime.text = item.data.time.create.formatTime("yyyy-MM-dd HH:mm")

            val resId = if (item.isChecked) com.gh.gamecenter.common.R.drawable.ic_selector_selected else com.gh.gamecenter.common.R.drawable.ic_selector_default
            holder.binding.ivSelector.setImageResource(resId)

            holder.itemView.setOnClickListener {
                if (currentSelectedPosition == holder.bindingAdapterPosition) return@setOnClickListener

                // 重置上一次选中状态
                mEntityList[currentSelectedPosition].isChecked = false
                notifyItemChanged(currentSelectedPosition, PAYLOAD_SELECTED_CHANGED)

                // 设置新的选中状态
                currentSelectedPosition = holder.bindingAdapterPosition
                item.isChecked = true
                notifyItemChanged(currentSelectedPosition, PAYLOAD_SELECTED_CHANGED)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any?>) {
        if (holder is ArchiveLimitViewHolder) {
            if (payloads.isEmpty()) {
                onBindViewHolder(holder, position)
            } else {
                val item = mEntityList[position]
                val resId = if (item.isChecked) com.gh.gamecenter.common.R.drawable.ic_selector_selected else com.gh.gamecenter.common.R.drawable.ic_selector_default
                holder.binding.ivSelector.setImageResource(resId)
            }
        }
    }

    override fun getItemCount(): Int = mEntityList.size

    fun getSelectedArchive() = mEntityList[currentSelectedPosition].data

    companion object {

        private const val PAYLOAD_SELECTED_CHANGED = "payload_selected_changed"
    }

    data class ArchiveItem(
        val data: ArchiveEntity,
        var isChecked: Boolean = false
    )

    private class ArchiveLimitViewHolder(
        val binding: ItemArchiveLimitBinding
    ) : RecyclerView.ViewHolder(binding.root)
}