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

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
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.commonui.widget.banner.adapter.BannerAdapter
import com.adealink.weparty.playmate.databinding.ItemPlaymateDetailContentBinding
import com.adealink.weparty.playmate.databinding.LayoutPlaymateDetailBannerItemBinding
import com.adealink.weparty.playmate.detail.data.PlaymateDetailImg
import com.adealink.weparty.playmate.detail.data.PlaymateDetailItemData
import com.adealink.weparty.playmate.detail.data.PlaymateDetailLabel

class PlaymateDetailViewBinder(
    val lifecycleOwner: LifecycleOwner
) : ItemViewBinder<PlaymateDetailItemData, PlaymateDetailViewBinder.ViewHolder>() {

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ViewHolder {
        return ViewHolder(ItemPlaymateDetailContentBinding.inflate(inflater, parent, false)).apply {
            initView()
        }
    }

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

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

        private val labelAdapter by fastLazy { MultiTypeListAdapter<PlaymateDetailLabel>() }

        private lateinit var imgAdapter: BannerImgAdapter
        fun initView() {
            labelAdapter.register(PlaymateLabelViewBinder())
            binding.rvLabel.adapter = labelAdapter
            binding.rvLabel.layoutManager =
                FlowLayoutManager(OrientationHelper.VERTICAL, Gravity.START, 8.dp(), 6.dp())

            imgAdapter = BannerImgAdapter()
            binding.bannerImg.setUserInputEnabled(false)
            binding.bannerImg.apply {
                setLoopTime(6 * 1000)
                addBannerLifecycleObserver(lifecycleOwner)
                setAdapter(imgAdapter)
                setOnBannerListener { data, _ ->
                    val imgData = data as? PlaymateDetailImg ?: return@setOnBannerListener

                }
            }
        }


        fun update(item: PlaymateDetailItemData) {
            binding.tvTitle.text = item.data.categoryName
            binding.tvSummary.text = item.data.summary

            labelAdapter.submitList(
                item.data.labels.map {
                    PlaymateDetailLabel(it)
                }
            )

            imgAdapter.datas = item.data.images.map {
                PlaymateDetailImg(it)
            }
        }
    }

    class BannerImgAdapter() :
        BannerAdapter<PlaymateDetailImg, BannerImgItemViewHolder>(emptyList()) {
        override fun onCreateHolder(
            parent: ViewGroup,
            viewType: Int,
        ): BannerImgItemViewHolder {
            val inflater = LayoutInflater.from(parent.context)
            return BannerImgItemViewHolder(
                LayoutPlaymateDetailBannerItemBinding.inflate(
                    inflater,
                    parent,
                    false
                )
            )
        }

        override fun onBindView(
            holder: BannerImgItemViewHolder,
            data: PlaymateDetailImg,
            position: Int,
            size: Int
        ) {
            holder.update(data)
        }

    }

    class BannerImgItemViewHolder(binding: LayoutPlaymateDetailBannerItemBinding) :
        BindingViewHolder<LayoutPlaymateDetailBannerItemBinding>(binding) {

        fun update(data: PlaymateDetailImg) {
            binding.root.setImageUrl(data.url)
        }
    }

}