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

import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
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.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.happyplaylet.R
import com.duben.happyplaylet.ad.draw.DrawExpressManager
import com.duben.happyplaylet.ad.express.ExpressAdCallback
import com.duben.happyplaylet.ad.express.ExpressManager
import com.duben.happyplaylet.manager.UserManager
import com.duben.happyplaylet.mvp.model.*
import com.duben.happyplaylet.utils.UIUtils
import com.duben.happyplaylet.video.tx.TXVideoBaseView

class TxVideoAdapter : BaseMultiItemQuickAdapter<VideoMultiItemEntity, BaseViewHolder>() {

    private var vedioBean: VedioBean? = null

    init {
        addItemType(MULTI_ITEM_1, R.layout.player_item_short_video_play)
        addItemType(MULTI_ITEM_2, R.layout.item_block_view)
        addItemType(MULTI_ITEM_3, R.layout.item_draw_ad)
    }

    override fun convert(holder: BaseViewHolder, item: VideoMultiItemEntity) {
        when (holder.itemViewType) {
            MULTI_ITEM_1 -> {
                initVideoHolder(holder, item.video)
            }
            MULTI_ITEM_2 -> {
                initLockHolder(holder)
            }
            else -> {
                initAdHolder(holder)
            }
        }
    }

    private fun initAdHolder(holder: BaseViewHolder) {
        DrawExpressManager.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)
                }

                DrawExpressManager.instance.preLoadAd()
            }

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

            override fun loadFail() {
            }
        })
    }

    private fun initLockHolder(holder: BaseViewHolder) {
        Glide.with(context).load(vedioBean?.coverImage)
            .into(holder.getView(R.id.iv_bg))
        val vipBtn = holder.getView<View>(R.id.vip)
        if (UserManager.getInstance().newFlag) {
            // 匹配
            vipBtn.visibility = View.VISIBLE
            vipBtn.setOnClickListener {
                mOnCustomChildClickListener?.onCustomChildClick(it, holder.adapterPosition)
            }
        } else {
            // 未匹配
            vipBtn.visibility = View.GONE
        }

        vedioBean?.let {
            holder.getView<TextView>(R.id.tv_title).text = String.format(
                "解锁%s·第%d-%d集",
                it.title,
                it.unlockIndex + 1,
                it.unlockIndex + it.adGiveVedioNum
            )
            holder.getView<TextView>(R.id.tv_info).text = String.format(
                "看激励视频解锁%d集剧情\n请按照顺序解锁",
                it.adGiveVedioNum
            )
            holder.getView<TextView>(R.id.unlock).text = String.format("查看激励视频")
            holder.getView<View>(R.id.unlock).setOnClickListener { it2 ->
                mOnCustomChildClickListener?.onCustomChildClick(it2, holder.adapterPosition)
            }
        }

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

                ExpressManager.instance.preLoadAd()
            }

            override fun renderSuccess(adView: FrameLayout?): Boolean {
                adView?.let {
                    UIUtils.removeFromParent(it)
                    val fmAd = holder.getView<FrameLayout>(R.id.fl_ad)
                    fmAd.removeAllViews()
                    fmAd.addView(it)
                }

                ExpressManager.instance.preLoadAd()
                return false
            }

            override fun loadFail() {
            }
        })
    }

    private fun initVideoHolder(holder: BaseViewHolder, item: IndexList.VedioEpisodeBean?) {
        //标题
        holder.setText(R.id.title_tv, vedioBean?.title)
        //集数
        holder.setText(R.id.info_tv, "第" + item?.vedioIndex + "集")
        //收藏数量
        holder.setText(R.id.zan_num_tv, "" + vedioBean?.hot)
        //缩略图
        Glide.with(context).load(vedioBean?.coverImage)
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
            .into(holder.getView<TXVideoBaseView>(R.id.baseItemView).findViewById(R.id.iv_cover))

        holder.getView<LinearLayout>(R.id.ll_collect).setOnClickListener {
            mOnCustomChildClickListener?.onCustomChildClick(it, holder.adapterPosition)
            if (vedioBean?.collect == 0) {
                playCollectAnim(holder.getView(R.id.zan_iv))
            } else {
                playCancelCollectAnim(holder.getView(R.id.zan_iv))
            }
        }

        if (vedioBean?.collect == 0) {
            // 未收藏
            setCollectImage(holder.getView(R.id.zan_iv))
        } else {
            // 已收藏
            setCancelCollectImage(holder.getView(R.id.zan_iv))
        }

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

    }

    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if (holder.itemViewType == MULTI_ITEM_1) {
            vedioBean?.let {
                if (it.seeIndex == data[holder.layoutPosition].video!!.vedioIndex) {
                    holder.getView<TXVideoBaseView>(R.id.baseItemView).pausePlayer()
                    holder.getView<TXVideoBaseView>(R.id.baseItemView).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) {
        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 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)
    }

    fun setVedioBean(vedioBean: VedioBean) {
        this.vedioBean = vedioBean
    }
}

 