package com.base.appzxhy.ui.concatAdapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.widget.ImageView
import androidx.palette.graphics.Palette
import com.base.appzxhy.R
import com.base.appzxhy.databinding.ItemBannerBinding
import com.base.appzxhy.databinding.ItemGalleryBannerBinding
import com.base.appzxhy.ui.adapter.CommonViewHolder
import com.base.appzxhy.utils.LogEx
import com.base.appzxhy.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.chad.library.adapter4.BaseQuickAdapter
import com.youth.banner.adapter.BannerAdapter


class BannerAdapter : BaseQuickAdapter<List<ShortPlay>, CommonViewHolder>() {

    private val TAG = "BannerAdapter"
    var paletteAction: ((colorInt: Int) -> Unit)? = null
    var playAction: ((shortPlay: ShortPlay) -> Unit)? = null
    var binding: ItemBannerBinding? = null

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

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: List<ShortPlay>?) {

        item ?: return
        binding = ItemBannerBinding.bind(holder.itemView)
        val context = holder.itemView.context
        LogEx.logDebug(TAG, "onBindViewHolder")

        val innerAdapter = binding?.banner?.adapter as GalleryAdapter?
        if (innerAdapter == null && item.isNotEmpty()) {
            LogEx.logDebug(TAG, "onBindViewHolder banner")
            binding?.banner?.setAdapter(GalleryAdapter(item))
            //添加画廊效果
            binding?.banner?.setBannerGalleryEffect(
                context.resources.getDimensionPixelSize(R.dimen.dp_30),
                context.resources.getDimensionPixelSize(R.dimen.dp_10)
            )
            binding?.banner?.setIndicator(binding?.indicator, false)
        } else {
            innerAdapter?.setDatas(item)
        }
    }

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

    inner class GalleryAdapter(list: List<ShortPlay>) : BannerAdapter<ShortPlay, CommonViewHolder>(list) {

        override fun onCreateHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
            return CommonViewHolder(R.layout.item_gallery_banner.inflate(parent))
        }

        override fun onBindView(holder: CommonViewHolder, data: ShortPlay?, position: Int, size: Int) {
            data ?: return
            val context = holder.itemView.context
            val binding = ItemGalleryBannerBinding.bind(holder.itemView)
            binding.llPlay.setOnClickListener {
                playAction?.invoke(data)
            }
            paletteImage(binding.iv, data.coverImage)
        }

    }

    private fun paletteImage(imageView: ImageView, coverImage: String) {
        Glide.with(context)
            .asBitmap()
            .load(coverImage)
            .into(object : CustomTarget<Bitmap?>() {
                override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap?>?) {
                    imageView.setImageBitmap(bitmap)
                    Palette.from(bitmap).generate { palette ->
                        palette ?: return@generate
                        // 获取主题色
                        val vibrantColor: Int = palette.getVibrantColor(Color.TRANSPARENT)
                        val mutedColor: Int = palette.getMutedColor(Color.TRANSPARENT)
                        // 使用这些颜色进行后续操作，例如设置背景色等
                        paletteAction?.invoke(mutedColor)
                    }
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    // 图片加载失败或清除时的处理
                }
            })
    }

    fun setBannerAutoLoop(isAutoLoop: Boolean) {
        binding?.banner?.isAutoLoop(isAutoLoop)
    }
}