package com.adealink.weparty.playmate.detail.adapter

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.OrientationHelper
import com.adealink.frame.base.fastLazy
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.recycleview.layoutmanager.FlowLayoutManager
import com.adealink.weparty.playmate.databinding.ItemPlaymateDetailCommentLabelBinding
import com.adealink.weparty.playmate.detail.data.PlaymateCommentLabel
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentLabelItemData

class PlaymateDetailCommentLabelViewBinder() :
    ItemViewBinder<PlaymateDetailCommentLabelItemData, PlaymateDetailCommentLabelViewBinder.ViewHolder>() {
    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ViewHolder {
        return ViewHolder(
            ItemPlaymateDetailCommentLabelBinding.inflate(
                inflater,
                parent,
                false
            )
        ).apply {
            initView()
        }
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        item: PlaymateDetailCommentLabelItemData
    ) {
        holder.update(item)
    }


    inner class ViewHolder(
        rootBinding: ItemPlaymateDetailCommentLabelBinding,
    ) : BindingViewHolder<ItemPlaymateDetailCommentLabelBinding>(rootBinding) {

        private val labelAdapter by fastLazy { MultiTypeListAdapter<PlaymateCommentLabel>() }
        fun initView() {
            labelAdapter.register(PlaymateCommentLabelViewBinder())
            binding.root.adapter = labelAdapter
            binding.rvLabel.layoutManager =
                FlowLayoutManager(OrientationHelper.VERTICAL, Gravity.START, 10.dp(), 6.dp())
        }

        fun update(data: PlaymateDetailCommentLabelItemData) {
            labelAdapter.submitList(data.commentLabels)
        }
    }
}