package com.adealink.weparty.profile.ui.about.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.base.fastLazy
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
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.profile.databinding.LayoutProfileInterestsBinding
import com.adealink.weparty.profile.ui.about.data.InterestsItemData


class InterestsViewBinder() :
    ItemViewBinder<InterestsItemData, InterestsViewBinder.ViewHolder>() {

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

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return ViewHolder(
            LayoutProfileInterestsBinding.inflate(inflater, parent, false)
        ).also {
            it.initView()
        }
    }

    inner class ViewHolder(
        binding: LayoutProfileInterestsBinding,
    ) : BindingViewHolder<LayoutProfileInterestsBinding>(binding) {

        private val interestAdapter by fastLazy { MultiTypeListAdapter<ProfileInterestItemData>() }

        fun initView() {
            interestAdapter.register(ProfileInterestViewBinder())
            binding.rvInterest.adapter = interestAdapter
            binding.rvInterest.layoutManager = FlowLayoutManager(
                FlowLayoutManager.VERTICAL,
                8.dp(),
                10.dp()
            )
        }

        fun update(item: InterestsItemData) {
            val interests = item.interests.map {
                ProfileInterestItemData(it)
            }
            if (interests.isEmpty()) {
                binding.root.gone()
            } else {
                binding.root.show()
                interestAdapter.submitList(interests)
            }

        }

    }
}