package com.duben.happyplaylet.video.tx.adapter

import android.view.View
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.duben.happyplaylet.R
import com.duben.happyplaylet.mvp.model.VedioBean
import com.duben.happyplaylet.video.tx.TXVideoBaseView

class TxRecommendVideoAdapter :
    BaseQuickAdapter<VedioBean, BaseViewHolder>(R.layout.item_tx_video) {

    override fun convert(holder: BaseViewHolder, item: VedioBean) {
        if (item.completeStatus == 0) {
            holder.setText(R.id.episode_tv, "共" + item.vedioTotal + "集 已完结")
        } else {
            holder.setText(R.id.episode_tv, "共" + item.vedioTotal + "集 更新中")
        }
        //用户名
        holder.setText(R.id.username_tv, item.title)
        //标题
        holder.setText(R.id.usertitle_tv, "第" + item.recommendIndex + "集")
        //缩略图
        Glide.with(context).load(item.coverImage)
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
            .into(holder.getView<TXVideoBaseView>(R.id.baseItemView).findViewById(R.id.iv_cover))

        if (item.collect == 0) {
            // 未收藏
            setCollectImage(holder.getView(R.id.iv_collect))
        } else {
            // 已收藏
            setCancelCollectImage(holder.getView(R.id.iv_collect))
        }
        // 热度
        holder.setText(R.id.tv_collect_num, item.hot)


        holder.getView<View>(R.id.ll_collect).setOnClickListener {
            if (item.collect == 0) {
                playCollectAnim(holder.getView(R.id.iv_collect))
            } else {
                playCancelCollectAnim(holder.getView(R.id.iv_collect))
            }
            mOnCustomChildClickListener?.onCustomChildClick(it, holder.adapterPosition)
        }
        holder.getView<View>(R.id.ll_bottom).setOnClickListener {
            mOnCustomChildClickListener?.onCustomChildClick(it, holder.adapterPosition)
        }

        holder.getView<TXVideoBaseView>(R.id.baseItemView)
            .setOnVideoEndListener(mOnVideoEndListener)

    }

    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
        holder.getView<TXVideoBaseView>(R.id.baseItemView).stopForPlaying()
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
        Glide.with(context).clear(holder.getView<View>(R.id.iv_cover))
    }

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

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

    private fun playCollectAnim(view: LottieAnimationView) {
        view.setImageDrawable(null)
        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) {
        view.setImageDrawable(null)
        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 mOnVideoEndListener: TXVideoBaseView.OnVideoEndListener? = null

    fun setOnVideoEndListener(onVideoEndListener: TXVideoBaseView.OnVideoEndListener) {
        this.mOnVideoEndListener = onVideoEndListener
    }

    private var mOnCustomChildClickListener: OnCustomChildClickListener? = null

    fun setOnCustomChildClickListener(onCustomChildClickListener: OnCustomChildClickListener) {
        this.mOnCustomChildClickListener = onCustomChildClickListener
    }

    interface OnCustomChildClickListener {
        fun onCustomChildClick(view: View, position: Int)
    }
}

 