package com.swago.baseswago.util

import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView
import com.zy.multistatepage.MultiStateContainer
import com.zy.multistatepage.state.EmptyState
import com.zy.multistatepage.state.ErrorState
import com.zy.multistatepage.state.LoadingState
import com.zy.multistatepage.state.SuccessState

/**
 *@date 2021/8/1 10:51
 *description:
 */
class SwagoRefreshUtil<T>(
    val adapter: BaseQuickAdapter<T, *>,
    private val recyclerView: RecyclerView,
    private val refreshLayout: SwipeRefreshLayout,
    private val container: MultiStateContainer,
    private val preLoadPage: Int,
    private val isNeedLoadMore: Boolean = true,
    private val isNeedRefresh: Boolean = true,
    private val loadData: (page: Int) -> Unit
) {

    private var isLoadMoreing = false
    private var isRefreshing = false
    private var currentPage = 0

    /**
     * 每一页的数据个数
     */
    private var eachPageDataSize = 0

    init {
        adapter.setEnableLoadMore(isNeedLoadMore)
        if (isNeedLoadMore) {
            adapter.setPreLoadNumber(preLoadPage)
            val loadMoreView = SimpleLoadMoreView()
            adapter.setLoadMoreView(loadMoreView)
            loadMoreView.setLoadMoreEndGone(true)
            adapter.setOnLoadMoreListener({
                loadMore()
            }, recyclerView)
        }

        if (isNeedRefresh) {
            refreshLayout.setOnRefreshListener {
                refresh(false)
            }
        }
    }


    /**
     * 获取到分页数据 设置下拉刷新和上拉的状态
     */
    fun onFetchDataFinish(data: List<T>?,isHeaderFooterVisible:Boolean) {
        onFetchDataFinish(data, true,isHeaderFooterVisible)
    }


    fun onFetchDataFinish(data: List<T>?, goneIfNoData: Boolean, sureLoadMoreEnd: Boolean,isHeaderFooterVisible:Boolean) {

        refreshLayout.isRefreshing = false
        if (data != null) {
            if (currentPage == 0 && isRefreshing) {
                eachPageDataSize = data.size
            }

            if (isLoadMoreing) {
                currentPage++
                adapter.addData(data)
            } else {
                adapter.setNewData(data)
            }

            if (sureLoadMoreEnd) {
                adapter.loadMoreEnd(goneIfNoData)
            } else {
                if (data.size < eachPageDataSize) {
                    adapter.loadMoreEnd(goneIfNoData)
                } else {
                    adapter.loadMoreComplete()
                }
            }

            if (currentPage == 0){
                if (data.isEmpty()) {
                    if (isHeaderFooterVisible){
                        container.show<SuccessState>()
                    }else{
                        container.show<EmptyState>()
                    }
                } else {
                    container.show<SuccessState>()
                }
            }else{
                if (adapter.data.isEmpty()) {
                    if (isHeaderFooterVisible){
                        container.show<SuccessState>()
                    }else{
                        container.show<EmptyState>()
                    }
                } else {
                    container.show<SuccessState>()
                }
            }
        }

        isLoadMoreing = false
        isRefreshing = false
    }

    fun onFetchDataFinish(data: List<T>?, goneIfNoData: Boolean,isHeaderFooterVisible:Boolean=false) {
        onFetchDataFinish(data, goneIfNoData, false,isHeaderFooterVisible)
    }

    /**
     * 加载错误
     */
    fun loadError() {
        if (isRefreshing) {
            refreshLayout.isRefreshing = false
        } else {
            adapter.loadMoreFail()
        }

        val disconnected = !NetUtil.isNetAvailable(recyclerView.context)
        if (disconnected) {
            container.show<ErrorState>{
                it.retry {
                    loadData(currentPage)
                }
            }
        } else {
            container.show<ErrorState>{
                it.retry {
                    loadData(currentPage)
                }
            }
        }
        isLoadMoreing = false
        isRefreshing = false
    }

    /**
     * isShowLoading
     * 用于判断是否是下拉刷新时不展示中间的loading
     */
    fun refresh(isShowLoading: Boolean) {
        if (isRefreshing || isLoadMoreing) {
            if (isRefreshing) {
                isRefreshing = false
            }
            refreshLayout.isRefreshing = true
            return
        }
        isRefreshing = true
        if (isShowLoading) {
            container.show<LoadingState>()
        }
        currentPage = 0
        loadData(0)
    }

    private fun loadMore() {
        if (isRefreshing || isLoadMoreing) {
            if (isLoadMoreing) {
                isLoadMoreing = false
            }
            return
        }
        isLoadMoreing = true
        loadData(currentPage + 1)
    }


}