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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.module.profile.data.UserPlaymateSkill
import com.adealink.weparty.profile.databinding.LayoutProfileSkillBinding
import com.adealink.weparty.profile.ui.about.data.SkillsItemData


class SkillsViewBinder(
    private val isSelf: () -> Boolean,
    private val goOrder: (skill: UserPlaymateSkill) -> Unit
) :
    ItemViewBinder<SkillsItemData, SkillsViewBinder.ViewHolder>() {

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

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

    inner class ViewHolder(
        binding: LayoutProfileSkillBinding,
    ) : BindingViewHolder<LayoutProfileSkillBinding>(binding) {
        private val skillAdapter by fastLazy { MultiTypeListAdapter<SkillItemData>() }

        fun initView() {
            skillAdapter.register(
                SKillViewBinder(
                    isSelf = {
                        isSelf.invoke()
                    },
                    order = {
                        goOrder.invoke(it)
                    }
                ))
            binding.rvSkil.adapter = skillAdapter
            binding.rvSkil.layoutManager =
                LinearLayoutManager(binding.rvSkil.context, RecyclerView.VERTICAL, false)
            binding.rvSkil.addItemDecoration(
                VerticalSpaceItemDecoration(6.dp())
            )
        }

        fun update(item: SkillsItemData) {
            if (layoutPosition == 0) {
                binding.root.setPadding(0, 16.dp(), 0, 0)
            } else {
                binding.root.setPadding(0, 28.dp(), 0, 0)
            }
            val interests = item.skills.map {
                SkillItemData(it)
            }
            if (interests.isEmpty()) {
                binding.root.gone()
            } else {
                binding.root.show()
                skillAdapter.submitList(interests)
            }

        }

    }
}