package com.base.locationsharewhite.ui.locationmap

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.locationsharewhite.R
import com.base.locationsharewhite.bean.ViewingBean
import com.base.locationsharewhite.databinding.ItemViewingBinding
import com.base.locationsharewhite.ui.views.XmlEx.inflate
import com.base.locationsharewhite.utils.KotlinExt.toFormatHHMinute
import com.chad.library.adapter4.BaseQuickAdapter

class ViewingAdapter(
    val itemClick: (item: ViewingBean) -> Unit,
    val moreClick: (view: View, item: ViewingBean) -> Unit,
) : BaseQuickAdapter<ViewingBean, ViewingAdapter.ViewingViewHolder>() {
    inner class ViewingViewHolder(view: View) : ViewHolder(view)

    override fun onBindViewHolder(holder: ViewingViewHolder, position: Int, item: ViewingBean?) {
        item ?: return
        val binding = ItemViewingBinding.bind(holder.itemView)
        binding.tvNameLetters.text = item.nickname.substring(0, 1)
        binding.tvNameCode.text = item.nickname
        when (item.locatDate?.power) {
            in 0..20 -> {
                binding.ivPower.setImageResource(R.mipmap.dianchi5)
            }

            in 21..40 -> {
                binding.ivPower.setImageResource(R.mipmap.dianchi4)
            }

            in 41..60 -> {
                binding.ivPower.setImageResource(R.mipmap.dianchi3)
            }

            in 61..80 -> {
                binding.ivPower.setImageResource(R.mipmap.dianchi2)
            }

            in 81..100 -> {
                binding.ivPower.setImageResource(R.mipmap.dianchi1)
            }
        }
        val time = item.locatDate?.timezone ?: 0
        binding.tvTime.text = time.toFormatHHMinute()
        binding.ivMore.setOnClickListener {
            moreClick.invoke(it, item)
        }
        binding.root.setOnClickListener {
            itemClick.invoke(item)
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): ViewingViewHolder {
        return ViewingViewHolder(R.layout.item_viewing.inflate(parent))
    }
}