package com.base.appzxhy.ui.concatAdapter

import android.content.Context
import android.view.ViewGroup
import com.base.appzxhy.R
import com.base.appzxhy.databinding.ItemShortplayColumnsBinding
import com.base.appzxhy.databinding.ItemShortplayStyle1Binding
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

/**
 * 多列的adapter
 */
class ShortPlayColumnsAdapter(
    val category: String = "",
) : BaseQuickAdapter<List<ShortPlay>, CommonViewHolder>() {

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

    override fun getItemCount(items: List<List<ShortPlay>>): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: List<ShortPlay>?) {
        item ?: return
        val binding = ItemShortplayColumnsBinding.bind(holder.itemView)
        binding.tvCategory.text = category
        var innerAdapter = binding.rv.adapter as InnerShortPlayAdapter?
        if (innerAdapter == null) {
            innerAdapter = InnerShortPlayAdapter()
            binding.rv.adapter = innerAdapter
        }
        innerAdapter.submitList(item)
    }

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

    inner class InnerShortPlayAdapter() : BaseQuickAdapter<ShortPlay, CommonViewHolder>() {
        override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: ShortPlay?) {
            item ?: return
            val binding = ItemShortplayStyle1Binding.bind(holder.itemView)
            val context = holder.itemView.context
            Glide.with(context).load(item.coverImage).into(binding.iv)
            binding.tvName.text = item.title
            kotlin.runCatching {
                binding.tvCategory.text = item.categories[0].name
            }
            binding.root.setOnClickListener {
                itemClick?.invoke(item)
            }
        }

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

    }

}