package com.duben.microtribe.ui.activitys

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
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.google.gson.Gson
import com.duben.microtribe.R
import com.duben.microtribe.common.Constant
import com.duben.microtribe.mvp.model.RecommendBean
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.video.tx.newrecommend.NewTxVideoActivity
import com.duben.microtribe.video.tx.TXVodPlayerWrapper
import com.duben.microtribe.video.tx.VideoModel
import com.duben.library.utils.json.JsonUtil
import com.duben.library.utils.nodoubleclick.AntiShake
import kotlinx.android.synthetic.main.activity_recommend.*

/**
 * 描述：推荐弹窗
 * 作者：孟崔广
 */
class RecommendActivity : BaseActivity(), View.OnClickListener {

    private var recommendBean: RecommendBean? = null

    override fun getContentViewLayoutID() = R.layout.activity_recommend

    override fun isApplyKitKatTranslucency() = false

    override fun toggleOverridePendingTransition() = true

    override fun getOverridePendingTransitionMode() = TransitionMode.SCALE

    override fun getBundleExtras(extras: Bundle?) {
        super.getBundleExtras(extras)
        extras?.let {
            val json = extras.getString(Constant.RECOMMEND_BEAN)
            recommendBean = Gson().fromJson(json, RecommendBean::class.java)
        }
    }

    override fun initViewsAndEvents() {
        initView()
        initListener()
    }

    private fun initView() {
        val lottieDrawable = LottieDrawable()
        LottieCompositionFactory.fromAsset(context, "recommend.json")
            .addListener { result: LottieComposition? ->
                lottieDrawable.setImagesAssetsFolder("images/")
                lottieDrawable.composition = result
                lottieDrawable.loop(true)
                lottieDrawable.playAnimation()
            }
        iv_recommend_top.setImageDrawable(lottieDrawable)

        Glide.with(context).load(recommendBean?.vedioMsg?.coverImage)
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
            .into(baseItemView.findViewById(R.id.iv_cover))

        val videoModel = VideoModel(recommendBean?.showVedioUrl)
        val txVodPlayerWrapper = TXVodPlayerWrapper(mContext)
        txVodPlayerWrapper.preStartPlay(videoModel)
        txVodPlayerWrapper.setAutoLoop()
        baseItemView.hideEmptyView()
        baseItemView.setTXVodPlayer(txVodPlayerWrapper)
        baseItemView.startPlay()

        tv_recommend_name.text = recommendBean?.vedioMsg?.title
        tv_recommend_text.text =
            "${recommendBean?.vedioMsg?.title}热播中共${recommendBean?.vedioMsg?.vedioTotal}集"
    }

    private fun initListener() {
        ic_recommend_quit.setOnClickListener(this)
        btn_recommend_next.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        baseItemView.startPlay()
    }

    override fun onPause() {
        super.onPause()
        baseItemView.pausePlayer()
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, R.anim.scale_out)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(
            keyCode,
            event
        )
    }

    override fun onClick(v: View) {
        if (AntiShake.check(v.id)) return

        when (v.id) {
            R.id.ic_recommend_quit -> {
                finish()
            }
            R.id.btn_recommend_next -> {
                val bundle = Bundle()
                bundle.putString(Constant.VEDIO_BEAN, JsonUtil.toJson(recommendBean?.vedioMsg))
                readyGoThenKill(NewTxVideoActivity::class.java, bundle)
            }
        }
    }
}