package com.duben.microtribe.video.tx.newrecommend

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.microtribe.R
import com.duben.microtribe.ad.express.ExpressAdCallback
import com.duben.microtribe.ad.express.ExpressManager
import com.duben.microtribe.common.AppConfig
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.mvp.model.*
import com.duben.microtribe.utils.UIUtils
import com.duben.microtribe.video.tx.TXVideoBaseView

class NewTxVideoAdapter : BaseMultiItemQuickAdapter<VideoMultiItemEntity4, 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)
    }

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

    private fun initLockHolder(holder: BaseViewHolder, type: String?) {
        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
        }

        val adBtn = holder.getView<View>(R.id.unlock)
        vedioBean?.let {
            try {
                val unlockText = String.format(
                        "解锁%s·第%d-%d集",
                        it.title,
                        it.unlockIndex + 1,
                        it.unlockIndex + it.adGiveVedioNum
                )
                AppConfig.detailDialogUnlockText = unlockText
            }catch (e:Exception){

            }
            when (type) {
                VEDIO3DOS_AD -> { // 观看激励视频解锁
                    vipBtn.visibility = View.GONE
                    adBtn.visibility = View.VISIBLE
                    holder.getView<TextView>(R.id.tv_title).text =
                        if (AppConfig.firstVipPrice == 0.0) "解锁后续剧集" else "${AppConfig.firstVipPrice}元解锁后续剧集"

                }
                VEDIO3DOS_PAY -> { // 开通会员解锁
                    adBtn.visibility = View.GONE
                    vipBtn.visibility = View.VISIBLE

                    holder.getView<TextView>(R.id.tv_title).text =
                        if (AppConfig.firstVipPrice == 0.0) "解锁后续剧集" else "${AppConfig.firstVipPrice}元解锁后续剧集"
                }
                else -> {
                    adBtn.visibility = View.GONE
                    vipBtn.visibility = View.VISIBLE
                    holder.getView<TextView>(R.id.tv_title).text =
                        if (AppConfig.firstVipPrice == 0.0) "解锁后续剧集" else "${AppConfig.firstVipPrice}元解锁后续剧集"
                }
            }

            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 + "集")

        //缩略图
        Glide.with(context).load(vedioBean?.coverImage)
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
            .into(holder.getView<TXVideoBaseView>(R.id.baseItemView).findViewById(R.id.iv_cover))

        vedioBean?.let {
            if (it.isRecommendVedio) {
                holder.getView<LinearLayout>(R.id.ll_collect).visibility = View.GONE
            } else {
                holder.getView<LinearLayout>(R.id.ll_collect).visibility = View.VISIBLE

                //收藏数量
                holder.setText(R.id.zan_num_tv, "" + it.hot)
                if (vedioBean?.collect == 0) {
                    // 未收藏
                    setCollectImage(holder.getView(R.id.zan_iv))
                } else {
                    // 已收藏
                    setCancelCollectImage(holder.getView(R.id.zan_iv))
                }

                holder.getView<LinearLayout>(R.id.ll_collect).setOnClickListener { it2 ->
                    mOnCustomChildClickListener?.onCustomChildClick(it2, holder.adapterPosition)
                    if (it.collect == 0) {
                        playCollectAnim(holder.getView(R.id.zan_iv))
                    } else {
                        playCancelCollectAnim(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
    }
}

 