package com.ysnows.base.base

import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.ysnows.base.R
import com.ysnows.base.net.IResp
import com.ysnows.base.view.BRView

/**
 *
 */
abstract class BRFragment<VM : BRViewModel<*>, A : BaseQuickAdapter<*, out BaseViewHolder>, B : ViewDataBinding> : BFragment<VM, B>(), BRView<A> {

    protected lateinit var mAdapter: A

    private var recyclerView: RecyclerView? = null

    override fun initView(view: View?) {
        super.initView(view)
        recyclerView = view?.findViewById(R.id.recycler_view)
        mAdapter = initAdapter()
        mAdapter.loadMoreModule.isEnableLoadMore = needLoadMore()
        recyclerView?.adapter = mAdapter
        //      adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
    }

    override fun listeners() {
        super.listeners()
        /*设置Item点击事件*/
//            adapter.setOnItemClickListener(this);
//            adapter.setOnItemChildClickListener(this);
        mAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
        mAdapter.loadMoreModule.isEnableLoadMore = needLoadMore()
        if (needLoadMore()) {
            /*上拉加载设置*/
            mAdapter.loadMoreModule.setOnLoadMoreListener { loadMore() }
        }

        /*列表预加载*/
//            adapter.setPreLoadNumber(2);
    }


    override fun vmClass(): Class<VM> {
        return BRViewModel::class.java as Class<VM>
    }


    override fun needLoadMore(): Boolean {
        return true
    }

    override val isNeedRefresh: Boolean
        get() = true

    override fun loadMore() {
//        vm.repository().reqList().subscribe()
        vm.reqMore()

    }

    override fun onListReceive(res: IResp<*>) {}
    override fun getTotal(total: Int) {}

    override fun adapter(): A = mAdapter

}