package com.duben.microtribe.ui.adapter

import android.view.View
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.duben.library.utils.GlideUtils
import com.duben.microtribe.R
import com.duben.microtribe.mvp.model.VedioBean

/**
 *  @author Assen
 *  @date 2023/7/7
 *  @desc
 */
class WatchRecordAdapter :
    BaseQuickAdapter<VedioBean, BaseViewHolder>(R.layout.item_watch_record) {

    override fun convert(holder: BaseViewHolder, item: VedioBean) {
        GlideUtils.loadImageViewNoAnim2(context, item.coverImage, holder.getView(R.id.image_iv))
        holder.getView<TextView>(R.id.title_tv).text = item.title
        holder.getView<TextView>(R.id.info_tv).text = "观看至第${item.seeIndex}集"

        if (item.collect == 0) {
            // 未收藏
            holder.getView<LottieAnimationView>(R.id.iv_collect)
                .setImageResource(R.mipmap.home_collect_img_0)
        } else {
            // 已收藏
            holder.getView<LottieAnimationView>(R.id.iv_collect)
                .setImageResource(R.mipmap.home_collect_img_1)
        }

        holder.getView<View>(R.id.ll_collect).setOnClickListener {
            if (item.collect == 0) {
                mOnItemCollectClickListener?.onItemCollectClick(holder.adapterPosition)
                playCollectAnim(holder.getView(R.id.iv_collect))
            } else {
                mOnItemCollectClickListener?.onItemCollectClick(holder.adapterPosition)
                playCancelCollectAnim(holder.getView(R.id.iv_collect))
            }
        }
    }

    /**
     * 判断position对应的Item是否是组的第一项
     *
     * @param position
     * @return
     */
    fun isItemHeader(position: Int): Boolean {
        if (data.size == 0 || position >= data.size) return false
        return if (position == 0) {
            true
        } else {
            val lastGroupName: String = data[position - 1].topTabsReamrk
            val currentGroupName: String = data[position].topTabsReamrk
            //判断上一个数据的组别和下一个数据的组别是否一致，如果不一致则是不同组，也就是为第一项（头部）
            lastGroupName != currentGroupName
        }
    }

    /**
     * 获取position对应的Item组名
     *
     * @param position
     * @return
     */
    fun getGroupName(position: Int): String {
        if (data.size == 0) return ""
        return data[position].topTabsReamrk
    }

    private fun playCollectAnim(view: LottieAnimationView) {
        val lottieDrawable = LottieDrawable()
        LottieCompositionFactory.fromAsset(context, "home_collect.json")
            .addListener { result: LottieComposition? ->
                lottieDrawable.setImagesAssetsFolder("images/")
                lottieDrawable.composition = result
                lottieDrawable.loop(false)
                lottieDrawable.playAnimation()
            }
        view.setImageDrawable(lottieDrawable)
    }

    private fun playCancelCollectAnim(view: LottieAnimationView) {
        val lottieDrawable = LottieDrawable()
        LottieCompositionFactory.fromAsset(context, "home_cancel_collect.json")
            .addListener { result: LottieComposition? ->
                lottieDrawable.setImagesAssetsFolder("images/")
                lottieDrawable.composition = result
                lottieDrawable.loop(false)
                lottieDrawable.playAnimation()
            }
        view.setImageDrawable(lottieDrawable)
    }

    private var mOnItemCollectClickListener: OnItemCollectClickListener? = null

    fun setOnItemCollectClickListener(onItemCollectClickListener: OnItemCollectClickListener) {
        this.mOnItemCollectClickListener = onItemCollectClickListener
    }

    interface OnItemCollectClickListener {
        fun onItemCollectClick(position: Int)
    }


}