package com.duben.microtribe.video.tx.adapter

import android.view.View
import android.widget.FrameLayout
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.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.duben.microtribe.R
import com.duben.microtribe.ad.draw.HomeDrawExpressManager
import com.duben.microtribe.ad.express.ExpressAdCallback
import com.duben.microtribe.mvp.model.*
import com.duben.microtribe.utils.UIUtils
import com.duben.microtribe.video.tx.TXVideoBaseView

class TxRecommendVideoAdapter :
    BaseMultiItemQuickAdapter<VideoMultiItemEntity2, BaseViewHolder>() {

    private var mCurrentPosition = 0

    init {
        addItemType(MULTI_ITEM_1, R.layout.item_tx_video)
        addItemType(MULTI_ITEM_3, R.layout.item_draw_ad)
    }

    override fun convert(holder: BaseViewHolder, item: VideoMultiItemEntity2) {
        if (holder.itemViewType == MULTI_ITEM_1) {
            initVideoHolder(holder, item.video)
        } else {
//            initAdHolder(holder)
        }
    }

    private fun initVideoHolder(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)
    }

    private fun initAdHolder(holder: BaseViewHolder) {
        HomeDrawExpressManager.instance.getAdView(object : ExpressAdCallback {
            override fun loadSuccess(adView: FrameLayout?) {
                adView?.let {
                    UIUtils.removeFromParent(it)
                    val fmAd = holder.getView<FrameLayout>(R.id.fl_ad)
                    fmAd.removeAllViews()
                    fmAd.addView(it)
                }
//                HomeDrawExpressManager.instance.preLoadAd()
            }

            override fun renderSuccess(adView: FrameLayout?): Boolean {
                return false
            }

            override fun loadFail() {
            }
        })
    }

    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if (holder.itemViewType == MULTI_ITEM_1) {
            val tXVideoBaseView = holder.getView<TXVideoBaseView>(R.id.baseItemView)
            if (data[mCurrentPosition].itemType == MULTI_ITEM_1 && tXVideoBaseView.player != null) {
                if (tXVideoBaseView.player.url != data[mCurrentPosition].video!!.recommendUrl) {
                    tXVideoBaseView.stopForPlaying()
                }
            } else {
                tXVideoBaseView.stopForPlaying()
            }
        }
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
        if (holder.itemViewType == MULTI_ITEM_1) {
            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)
    }

    fun setCurrentPosition(currentPosition: Int) {
        this.mCurrentPosition = currentPosition
    }

    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)
    }


}

 