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.core.view.updatePadding
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.BarUtils
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
import com.youth.banner.listener.OnPageChangeListener


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

    private val TAG = "BannerAdapter"
    var playAction: ((shortPlay: ShortPlay) -> Unit)? = null
    var binding: ItemBannerBinding? = null
    private var paletteMap = HashMap<String, Int>()
    var paletteCallBack: ((color: Int) -> 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
        binding = ItemBannerBinding.bind(holder.itemView)
        val context = holder.itemView.context
        LogEx.logDebug(TAG, "onBindViewHolder")

        val top = BarUtils.getStatusBarHeight() + context.resources.getDimensionPixelSize(R.dimen.dp_66)
        binding?.root?.updatePadding(top = top)

        val innerAdapter = binding?.banner?.adapter as GalleryAdapter?
        if (innerAdapter == null && item.isNotEmpty()) {
            LogEx.logDebug(TAG, "onBindViewHolder banner")
            binding?.banner?.setAdapter(GalleryAdapter(item))
            binding?.banner?.setLoopTime(2500)
            //添加画廊效果
            binding?.banner?.setBannerGalleryEffect(
                context.resources.getDimensionPixelSize(R.dimen.dp_30),
                context.resources.getDimensionPixelSize(R.dimen.dp_10)
            )
            binding?.banner?.setIndicator(binding?.indicator, false)
            binding?.banner?.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                }

                override fun onPageSelected(position: Int) {
                    val bean = item[position]
                    val coverImage = bean.coverImage
                    val color = paletteMap[coverImage] ?: Color.BLACK
                    LogEx.logDebug(TAG, "position=$position coverImage=$coverImage color=$color")
                    paletteCallBack?.invoke(color)
                    binding?.root?.setBackgroundColor(color)

                }

                override fun onPageScrollStateChanged(state: Int) {
                }
            })
        } 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)
                        // 使用这些颜色进行后续操作，例如设置背景色等
                        paletteMap[coverImage] = mutedColor
                    }
                }

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

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