package com.duben.microtribe.video.tx

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.duben.microtribe.R
import com.duben.microtribe.ad.draw.HomeDrawExpressManager
import com.duben.microtribe.common.AppConfig
import com.duben.microtribe.common.Constant
import com.duben.microtribe.manager.LocalVedioManager
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.mvp.model.BannerList
import com.duben.microtribe.mvp.presenters.RecommendPresenter
import com.duben.microtribe.mvp.views.RecommendView
import com.duben.microtribe.ui.fragment.base.BaseFragment
import com.duben.microtribe.video.tx.adapter.TxRecommendVideoAdapter
import kotlinx.android.synthetic.main.fragment_tx_video.*

/**
 *  @author Assen
 *  @date 2023/7/5
 *  @desc 主页 -> 推荐 —> 推荐
 */
class TxVideoFragment : BaseFragment(), RecommendView {

    private val recommendPresenter by lazy { RecommendPresenter() }

    private var recommendPage = 1 // 分页

    companion object {
        fun newInstance(): Fragment {
            val args = Bundle()
            val fragment = TxVideoFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initViewsAndEvents() {
        recommendPresenter.attachView(this)

        recommendPresenter.autoList()

        recommend_view.setOnLoadMoreListener {
            recommendPage++
            recommendPresenter.autoList()
        }
        recommend_view.setOnVideoEndListener {
            LocalVedioManager.startVedioDetailActivityForType(
                requireActivity(),
                recommend_view.data[recommend_view.currentPosition].video,
                true
            )
        }
        recommend_view.setOnCustomChildClickListener(object :
            TxRecommendVideoAdapter.OnCustomChildClickListener {
            override fun onCustomChildClick(view: View, position: Int) {
                when (view.id) {
                    R.id.ll_bottom -> {
                        LocalVedioManager.startVedioDetailActivityForType(
                            requireActivity(),
                            recommend_view.data[position].video,
                            true
                        )
                    }
                    R.id.ll_collect -> {
                        if (recommend_view.data[position].video?.collect == 0) {
                            recommend_view.data[position].video?.collect = 1
                            recommendPresenter.collect("" + recommend_view.data[position].video?.vedioId)
                        } else {
                            recommend_view.data[position].video?.collect = 0
                            recommendPresenter.cancelCollect("" + recommend_view.data[position].video?.vedioId)
                        }
                    }
                    else -> {}
                }
            }
        })
    }

    override fun getContentViewLayoutID() = R.layout.fragment_tx_video

    override fun onHiddenChanged(hidden: Boolean) {
        if (hidden) { // 不在最前端界面显示
            onPause()
        } else { // 重新显示到最前端中
            onResume()
        }
        super.onHiddenChanged(hidden)
    }

    override fun onResume() {
        super.onResume()
        if (AppConfig.fragmentClickFlag != Constant.FRAGMENT_CLICK_TWO) return

//        if (!UserManager.getInstance().vipFlag) {
//            HomeDrawExpressManager.instance.preLoadAd()
//        }

        recommend_view.resume()
    }

    override fun onPause() {
        super.onPause()
        recommend_view.pause()
    }

    override fun onDestroyView() {
        recommend_view.onDestroy()
        super.onDestroyView()
        recommend_view.releasePlayer()

        HomeDrawExpressManager.instance.destroy()
    }

    override fun onDetach() {
        super.onDetach()
        recommendPresenter.detachView()
    }

    override fun collectSuc() {

    }

    override fun collectFail() {

    }

    override fun cancelCollectSuc() {

    }

    override fun cancelCollectFail() {

    }

    override fun autoListSuc(list: BannerList) {
        if (recommendPage == 1) {
            recommend_view.loadData(list.list)
        } else {
            recommend_view.loadMoreData(list.list)
            recommend_view.hideLoadMore()
        }
    }

    override fun autoListFail() {

    }


}