package com.dlc.exam.ui.common

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dlc.exam.ui.widget.ListPageView
import com.rc.core.R
import io.reactivex.disposables.Disposable

/**
 * info
 *
 * @author ReiChin_
 */
abstract class CommonListActivity<T, VB : ViewBinding>(val mAdapter: PageQuickAdapter<T>) :
    BaseCountDownActivity<VB>(),
    OnItemClickListener,
    OnItemChildClickListener {

    protected abstract val mSrlRefresh: SwipeRefreshLayout
    protected abstract val mRvContent: RecyclerView
    protected abstract val mListPageView: ListPageView

    protected var mCurrentPage = ListPageView.FIRST_PAGE
    protected var mRefresh = false

    override fun initListener() {
        mSrlRefresh.setOnRefreshListener { loadData(true) }
        mListPageView.setPageListener {
            mCurrentPage = it
            loadData()
        }
    }

    override fun initViews(savedInstanceState: Bundle?) {
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mAdapter.setOnItemClickListener(this)
        mAdapter.setOnItemChildClickListener(this)

        mRvContent.layoutManager = createLayoutManager()
        mAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInBottom)

        createItemDecoration()?.let {
            mRvContent.addItemDecoration(it)
        }

        mRvContent.adapter = mAdapter
    }

    override fun createItemDecoration(): RecyclerView.ItemDecoration? {
        val decor = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        decor.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_item_divider)!!)
        return decor
    }

    protected open fun createLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(this)
    }

    override fun initData() {
        super.initData()
        if (autoLoadData()) {
            loadData()
        }
    }

    protected open fun autoLoadData() = true

    /**
     * 数据加载成功的处理逻辑
     *
     * @param data 从服务端查询的数据
     */
    protected open fun dispatchLoadDataSuccess(data: List<T>?, totalCnt: Int = 0) {
        if (isDestroyed) return

        if (mRefresh) mSrlRefresh.isRefreshing = false

        if (data.isNullOrEmpty()) {
            mAdapter.setEmptyView(R.layout.view_list_empty)
        }
        mAdapter.pageOffsite = (mCurrentPage - 1) * ListPageView.PAGE_SIZE
        mAdapter.setNewInstance(data?.toMutableList())
        mListPageView.setPageInfo(totalCnt, mCurrentPage)
    }

    /**
     * 数据加载失败的处理逻辑
     */
    protected open fun dispatchLoadDataFailure(throwable: Throwable) {
        if (isDestroyed) return

        if (mRefresh) mSrlRefresh.isRefreshing = false

        mAdapter.setNewInstance(null)
        mAdapter.setEmptyView(R.layout.view_list_empty)
        throwable.printStackTrace()
        showNetError(throwable)
        if (mCurrentPage == ListPageView.FIRST_PAGE) {
            mListPageView.setPageInfo(0, mCurrentPage)
        }
    }

    protected fun loadData(refresh: Boolean = false) {
        mRefresh = refresh
        queryData()?.let { addDisposable(it) }
    }

    /**
     * 查询数据
     */
    abstract fun queryData(): Disposable?

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
    }

}