package com.swago.home.innerhome

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.PersonDataDFragment
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.MomentModel
import com.swago.baseswago.model.home.HomeModel
import com.swago.home.databinding.FragmentFollowBinding
import com.swago.home.databinding.FragmentHotBinding

/**
 *@date 2021/11/23 21:45
 *description:
 */
class FollowFragment : BaseListFragment<FragmentHotBinding, MomentModel>(){

    private val homeVm  by viewModels<HomeVm>()
    private var isFirstLoadData = true
    override val smartRecyclerView: SwagoRecyclerView<MomentModel> by lazy {
        binding.rv as SwagoRecyclerView<MomentModel>
    }
    override val adapter: BaseQuickAdapter<MomentModel, *> by lazy {
        HomeAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        GridLayoutManager(context,2)
    }
    override val loadData: (page: Int) -> Unit = {
        homeVm.getHomeList(3, it+1)
    }

    override fun initViewData() {
        smartRecyclerView.recyclerView?.addItemDecoration(HomeGridItemDecoration())
        adapter.setOnItemClickListener { _, _, position ->
            if (adapter.data.size>position){
                if (adapter.data[position].user_broadcast_status == 1){
                    ARouter.getInstance()
                        .build(ARouteConstant.Room.user)
                        .withInt("position",position)
                        .withParcelableArrayList("data",adapter.data as ArrayList<MomentModel>)
                        .navigation()
                }else{
                    PersonDataDFragment.newInstance(adapter.data[position].id,
                        isAnchor = true,
                        inRoom = false
                    ).show(childFragmentManager,"PersonDataDFragment")
                }
            }

        }
    }

    override fun initLiveData() {
        homeVm.homeListLiveData.observe(this){
            if (it == null){
                smartRecyclerView.onFetchDataError()
            }else {
                smartRecyclerView.onFetchDataFinish(it,true)
            }
        }
    }

    fun loadData(){
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
        isFirstLoadData = false
    }

}