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.IResponse
import com.ysnows.base.view.BRView
import io.reactivex.Observable

/**
 *
 */
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 createRepository(): BRRepository {
        return BRRepository()
    }


    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(api() as Observable<IResponse<Any?>>).subscribe()

    }

    override fun repository(): BRRepository {
        return repository as BRRepository
    }

    /**
     * 初始化Presenter
     *
     * @return
     */
    override fun initRepository(): BRRepository {
        repository = createRepository()
        repository.attachView(this)
        return repository as BRRepository
    }

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

    override fun adapter(): A = mAdapter

}