package com.duben.microtribe.ui.activitys

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.duben.library.utils.nodoubleclick.AntiShake
import com.duben.microtribe.R
import com.duben.microtribe.manager.LocalVedioManager
import com.duben.microtribe.mvp.model.BannerList
import com.duben.microtribe.mvp.model.RecommendBannerList
import com.duben.microtribe.mvp.model.VedioBean
import com.duben.microtribe.mvp.presenters.RecommendBannerPresenter
import com.duben.microtribe.mvp.views.RecommendBannerView
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.adapter.VipBannerAdapter
import com.youth.banner.listener.OnPageChangeListener
import kotlinx.android.synthetic.main.activity_recommend_banner.*

class RecommendBannerActivity : BaseActivity(),RecommendBannerView, View.OnClickListener {

    private val recommendBannerPresenter by lazy { RecommendBannerPresenter() }
    private var banner:VedioBean?=null

    override fun getContentViewLayoutID() = R.layout.activity_recommend_banner

    override fun isApplyKitKatTranslucency() = false

    override fun toggleOverridePendingTransition() = true

    override fun getOverridePendingTransitionMode() = TransitionMode.FADE

    override fun initViewsAndEvents() {

        recommendBannerPresenter.attachView(this)
        recommendBannerPresenter.orders()

        initListener()
    }

    private fun initListener() {
        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)

        iv_recommend_banner_close.setOnClickListener(this)
        btn_recommend_next.setOnClickListener(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        recommendBannerPresenter.detachView()
    }

    override fun ordersSuc(data: RecommendBannerList?) {
        data?.let {
            setBanner(it.list)
        }
    }

    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.iv_recommend_banner_close -> {
                finish()
            }
            R.id.btn_recommend_next -> {
//                val bundle = Bundle()
//                bundle.putString(Constant.VEDIO_BEAN, JsonUtil.toJson(recommendBean?.vedioMsg))
//                readyGoThenKill(NewTxVideoActivity::class.java, bundle)

                LocalVedioManager.startVedioDetailActivityForType(
                        this@RecommendBannerActivity,
                        banner
                )
                finish()
            }
        }
    }

    private fun setBanner(data: List<VedioBean>) {
        val adapter = VipBannerAdapter(data)
        // 绑定生命周期
        vip_banner.addBannerLifecycleObserver(this)
            .setBannerGalleryEffect(80, 15, 0.8f)
            .setAdapter(adapter)
            .setOnBannerListener { data2, position ->
                val banner = data.get(position)
                LocalVedioManager.startVedioDetailActivityForType(
                        this@RecommendBannerActivity,
                        banner
                )
                finish()
            }
            .addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                }

                override fun onPageSelected(position: Int) {
                    banner=data[position]
                    tv_banner_title.text = String.format("《%1s》", data[position].title)
                }

                override fun onPageScrollStateChanged(state: Int) {}
            })
        if(data!=null&&data.size>0){
            tv_banner_title.text = String.format("《%1s》", data[0].title)
            banner=data[0]
        }

    }
}