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.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.FragmentHotBinding
import com.swago.home.databinding.FragmentNewBinding

/**
 *@date 2021/11/23 21:44
 *description:
 */
class NewFragment  : 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(2, it+1)
    }

    override fun initViewData() {
        smartRecyclerView.recyclerView?.addItemDecoration(HomeGridItemDecoration())
        adapter.setOnItemClickListener { _, _, position ->
            ARouter.getInstance()
                .build(ARouteConstant.Room.user)
                .withInt("position",position)
                .withParcelableArrayList("data",adapter.data as ArrayList<MomentModel>)
                .navigation()
        }
    }

    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 onResume() {
        super.onResume()
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
        isFirstLoadData = false
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }
}