package com.gh.gamecenter.qa.video.detail.desc

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.utils.ShareUtils
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.LazyListFragment
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.entity.ForumVideoEntity
import com.gh.gamecenter.entity.VideoDescItemEntity
import com.gh.gamecenter.common.eventbus.EBShare
import com.gh.gamecenter.common.mvvm.Status
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VideoDescFragment : LazyListFragment<VideoDescItemEntity, VideoDescViewModel>() {

    private var mAdapter: VideoDescAdapter? = null
    private lateinit var mVideoDetailViewModel: ForumVideoDetailViewModel

    override fun getRealLayoutId(): Int = R.layout.fragment_list_base

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter ?: VideoDescAdapter(requireContext(), mVideoDetailViewModel, mListViewModel)
            .apply { mAdapter = this }
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration? = null

    override fun onFragmentFirstVisible() {
        mVideoDetailViewModel = viewModelProviderFromParent()
        super.onFragmentFirstVisible()
        mListRefresh?.isEnabled = false
        mListRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    val position = mLayoutManager.findLastCompletelyVisibleItemPosition()
                    NewLogUtils.logSlideVideoDetail(position)
                }
            }
        })
        observeData()
    }

    private fun observeData() {
        mVideoDetailViewModel.detailLiveData.observeNonNull(this) {
            if (it.status == Status.SUCCESS) {
                mListViewModel.topVideoDetail = it.data as ForumVideoEntity
                mListViewModel.mergeListData(mListViewModel.listLiveData.value)
                mListLoading?.visibility = View.GONE
                onRefresh()
            }
        }

        mVideoDetailViewModel.updateDetailLiveData.observe(this, Observer {
            mAdapter?.notifyItemChanged(0)
        })

        mVideoDetailViewModel.needToUpdateShareCount.observe(this, Observer {
            mAdapter?.notifyItemChanged(0)
        })

        mListViewModel.handleSuccessLiveData.observe(this, Observer {
            mAdapter?.notifyItemChanged(0)
        })
    }

    override fun isAutomaticLoad(): Boolean = false

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(share: EBShare?) {
        postDelayedRunnable({
            if (share != null && share.shareEntrance == ShareUtils.ShareEntrance.video && isSupportVisible) {
                mVideoDetailViewModel.shareVideoStatistics(mListViewModel.topVideoDetail)
            }
        }, 200)
    }
}