package com.yidian.shenghuoquan.border.adapter

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.yidian.shenghuoquan.border.R
import com.yidian.shenghuoquan.border.databinding.BorderItemOrderDetailCommodityListBinding
import com.yidian.shenghuoquan.border.dto.MergeOrderItem
import com.yidian.shenghuoquan.border.viewmodel.OrderDetailCommodityListItemViewModel
import com.yidian.utils.DensityUtil

/**
 * author: yinjiacheng
 * date: 2021/8/23 16:18
 * description: 订单详情商品列表
 */
class OrderDetailCommodityListAdapter(data: List<MergeOrderItem>?) :
  RecyclerView.Adapter<OrderDetailCommodityListAdapter.OrderDetailCommodityListViewHolder>() {

  private val data by lazy { ArrayList<MergeOrderItem>() }

  init {
    data?.let {
      this.data.addAll(it)
    }
  }

  /**
   * 更新数据
   */
  fun updateData(data: List<MergeOrderItem>?) {
    data?.let {
      notifyItemRangeRemoved(0, this.data.size)
      this.data.clear()
      this.data.addAll(it)
      notifyItemRangeInserted(0, this.data.size)
    }
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderDetailCommodityListViewHolder {
    val binding = DataBindingUtil.inflate<BorderItemOrderDetailCommodityListBinding>(
      LayoutInflater.from(parent.context),
      R.layout.border_item_order_detail_commodity_list,
      parent,
      false
    )
    return OrderDetailCommodityListViewHolder(binding.root, binding)
  }

  override fun onBindViewHolder(holder: OrderDetailCommodityListViewHolder, position: Int) {
    holder.binding.vm = OrderDetailCommodityListItemViewModel()
    holder.binding.data = data[position]
  }

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

  class OrderDetailCommodityListViewHolder(itemView: View, val binding: BorderItemOrderDetailCommodityListBinding) :
    RecyclerView.ViewHolder(itemView)

  class OrderDetailCommodityListItemDecoration : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
      outRect.top = DensityUtil.dp2px(16f)
    }
  }

}
