package com.hongyegroup.cpt_newsfeed.ui

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBLazyLoadingFragment
import com.guadou.cs_cptservices.interfaces.IFragmentRefresh
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.utils.log.YYLogUtils
import com.guadou.lib_baselib.view.gloading.Gloading
import com.guadou.lib_baselib.view.gloading.GloadingPlaceHolderlAdapter
import com.hongyegroup.cpt_newsfeed.R
import com.hongyegroup.cpt_newsfeed.databinding.FragmentNewsfeedFollowingBinding
import com.hongyegroup.cpt_newsfeed.mvvm.NewsFeedFollowingViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * 多类型的动态列表页面
 */
class NewsFeedListFragment(private val type: Int) : YYBaseVDBLazyLoadingFragment<NewsFeedFollowingViewModel, FragmentNewsfeedFollowingBinding>(),
    IFragmentRefresh, OnRefreshListener, OnLoadMoreListener {

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_newsfeed_following)
    }

    //重新生成GLoading对象
    override fun generateGLoading(view: View): Gloading.Holder {
        return Gloading.from(GloadingPlaceHolderlAdapter(R.layout.layout_placeholder_newsfeed_hot)).wrap(view)
            .withRetry { onLazyInitData() }
    }

    override fun init(savedInstanceState: Bundle?) {
        initRV()
        initListener()
    }

    override fun onLazyInitData() {
        YYLogUtils.w("onLazyInitData-1")
        initData()
    }

    private fun initData() {
        mViewModel.getNewsFeedList().observe(this) {
            mBinding.refreshLayout.finishRefresh()
            mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = true
        }
    }

    private fun initListener() {
        //Adapter的滑动监听，监听加载更多
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false
        mViewModel.mAdapter.loadMoreModule.preLoadNumber = 4
        mViewModel.mAdapter.loadMoreModule.setOnLoadMoreListener(this)

        //刷新控件初始化
        mBinding.refreshLayout.setEnableNestedScroll(true)
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setEnableRefresh(true)
        mBinding.refreshLayout.setOnRefreshListener(this)

        //Item点击
        mViewModel.mAdapter.addChildClickViewIds(R.id.ll_news_feed_root)
        mViewModel.mAdapter.setOnItemChildClickListener { _, _, position ->
            val item = mViewModel.mDatas[position]
            toast("点击Item")
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.isNeedCleanAllData = true
        mViewModel.mAdapter.loadMoreModule.loadMoreComplete()
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false
        //直接调用，参数从成员变量中获取
        mViewModel.mCurPage = 1
        initData()
    }

    override fun onLoadMore() {
        mViewModel.isNeedCleanAllData = false
        mViewModel.mCurPage++
        initData()
    }

    private fun initRV() {
        mBinding.recyclerView.vertical().apply {
            adapter = mViewModel.mAdapter
        }
    }

    override fun scrollTopRefresh() {
        if (mViewModel.mDatas.size > 1) {
            mBinding.recyclerView.scrollToPosition(0)
        }
        mBinding.refreshLayout.autoRefresh()
    }


    override fun startObserve() {

    }

}