package com.base.appzxhy.ui.concatAdapter

import android.content.Context
import android.view.ViewGroup
import com.base.appzxhy.R
import com.base.appzxhy.databinding.ItemShortplayCategoryBinding
import com.base.appzxhy.databinding.ItemShortplayStyle3Binding
import com.base.appzxhy.ui.adapter.CommonViewHolder
import com.base.appzxhy.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.chad.library.adapter4.BaseQuickAdapter

class ShortPlayCategoryAdapter(
    val category: String = "",
    val shortList: List<ShortPlay>
) : BaseQuickAdapter<Any, CommonViewHolder>() {

    var itemClick: ((shortPlay: ShortPlay) -> Unit)? = null
    var categoryClick: (() -> Unit)? = null

    override fun getItemCount(items: List<Any>): Int {
        return 1
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: Any?) {
        val binding = ItemShortplayCategoryBinding.bind(holder.itemView)
        binding.tvCategory.text = category
        if (binding.rv.adapter == null) {
            val adapter = WatchShortAdapter()
            binding.rv.adapter = adapter
            adapter.submitList(shortList)
        }
        binding.root.setOnClickListener {
            categoryClick?.invoke()
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
        return CommonViewHolder(R.layout.item_shortplay_category.inflate(parent))
    }

    inner class WatchShortAdapter : BaseQuickAdapter<ShortPlay, CommonViewHolder>() {
        override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: ShortPlay?) {
            item ?: return
            val binding = ItemShortplayStyle3Binding.bind(holder.itemView)
            binding.tvName.text = item.title
            Glide.with(context).load(item.coverImage).into(binding.iv)
            binding.root.setOnClickListener {
                itemClick?.invoke(item)
            }
        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
            return CommonViewHolder(R.layout.item_shortplay_style_3.inflate(parent))
        }

    }
}