package com.swago.room.ranklist

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.model.live.RankListModel
import com.swago.room.databinding.FragmentRankListBinding
import com.swago.room.vm.RoomOtherVm

/**
 *@date 2021/12/1 20:21
 *description:
 */
class RankListFragment :  BaseListFragment<FragmentRankListBinding, RankListModel.ListBean>(){

    private val roomOtherVm by viewModels<RoomOtherVm>()

    private var type = 1
    private var roomId = ""

    override val smartRecyclerView: SwagoRecyclerView<RankListModel.ListBean> by lazy {
        binding.rv as SwagoRecyclerView<RankListModel.ListBean>
    }
    override val adapter: BaseQuickAdapter<RankListModel.ListBean, *> by lazy {
        RankListAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(context)
    }
    override val loadData: (page: Int) -> Unit = {
        if (roomId.isNotEmpty()){
            roomOtherVm.getRankList(roomId,type,it+1)
        }

    }

    override fun initViewData() {
        arguments?.let {
            type = it.getInt("type",1)
            roomId = it.getString("roomId","")

        }
    }

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

    companion object{
        fun newInstance(roomId:String,type:Int):RankListFragment{
            val fragment = RankListFragment()
            val bundle = Bundle()
            bundle.putInt("type",type)
            bundle.putString("roomId",roomId)
            fragment.arguments = bundle
            return fragment
        }
    }


}