package com.yidianling.tests.home.widget

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.GridLayoutManager
import android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE
import com.yidianling.tests.home.adapter.AdapterWrapper
import com.yidianling.tests.home.config.ITestHomeConfig


/**
 * @author yuanwai
 * @描述:上拉加载更多帮助类
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/7/26
 */
class SwipeToLoadHelper(recyclerView: RecyclerView, private val mAdapterWrapper: AdapterWrapper) : RecyclerView.OnScrollListener() ,AdapterWrapper.FooterCallBack{

    private val mRecyclerView: RecyclerView? = null
    private val mLayoutManager: RecyclerView.LayoutManager
    private var mListener: LoadMoreListener? = null
    /** 是否正在加载中  */
    private var mLoading = false
    /** 上拉刷新功能是否开启  */
    private var mIsSwipeToLoadEnabled = true

    init {
        mLayoutManager = recyclerView.layoutManager

        if (mLayoutManager is GridLayoutManager) {
            mAdapterWrapper.setAdapterType(AdapterWrapper.ADAPTER_TYPE_GRID)
            mAdapterWrapper.setSpanCount(mLayoutManager.spanCount)
        } else if (mLayoutManager is LinearLayoutManager) {
            mAdapterWrapper.setAdapterType(AdapterWrapper.ADAPTER_TYPE_LINEAR)
        }
        mAdapterWrapper.setFooterCallBack(this)
        // 将OnScrollListener设置RecyclerView
        recyclerView.addOnScrollListener(this)
    }

    override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
        if (mIsSwipeToLoadEnabled && SCROLL_STATE_IDLE === newState && !mLoading) {
            if (mLayoutManager is GridLayoutManager) {
                val gridLayoutManager = mLayoutManager
                gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int {
                        return if (mIsSwipeToLoadEnabled) {
                            // 功能开启, 根据位置判断, 最后一个item时返回整个宽度, 其他位置返回1
                            // AdapterWrapper会保证最后一个item会从新的一行开始
                            if (position == mLayoutManager.getItemCount() - 1) {
                                gridLayoutManager.spanCount
                            } else {
                                1
                            }
                        } else {
                            1
                        }
                    }
                }
            }

            if (mLayoutManager is LinearLayoutManager) {
                val linearLayoutManager = mLayoutManager
                val lastCompletePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition()
                // only when the complete visible item is second last
                if (lastCompletePosition == mLayoutManager.getItemCount() - 2) {
                    val firstCompletePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition()
                    val child = linearLayoutManager.findViewByPosition(lastCompletePosition)
                            ?: return
                    val deltaY = recyclerView!!.bottom - recyclerView.paddingBottom - child.bottom
                    if (deltaY > 0 && firstCompletePosition != 0) {
                        recyclerView.smoothScrollBy(0, -deltaY)
                    }
                } else if (lastCompletePosition == mLayoutManager.getItemCount() - 1) {
                    // 最后一项完全显示, 触发操作, 执行加载更多操作 禁用回弹判断
                    mLoading = true
                    mAdapterWrapper.setLoadItemState(ITestHomeConfig.STATUS_LOADING)
                    if (mListener != null) {
                        mListener!!.onLoad()
                    }
                }
            }
        }
    }

    override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
    }

    /** 设置下拉刷新功能是否开启  */
    fun setSwipeToLoadEnabled(isSwipeToLoadEnabled: Boolean) {
        if (mIsSwipeToLoadEnabled != isSwipeToLoadEnabled) {
            mIsSwipeToLoadEnabled = isSwipeToLoadEnabled
            mAdapterWrapper.setLoadItemVisibility(isSwipeToLoadEnabled)
        }
    }

    /** 设置LoadMore Item为加载完成状态, 上拉加载更多完成时调用  */
    fun setLoadMoreFinish() {
        mLoading = false
        mAdapterWrapper.setLoadItemState(ITestHomeConfig.STATUS_UP_LOAD)
    }

    /**
     * 设置LoadMore Item为没有更多数据状态 显示查看全部测评
     */
    fun setNoMoreData(){
        mLoading = false
        mAdapterWrapper.setLoadItemState(ITestHomeConfig.STATUS_SELECT_ALL)
    }

    override fun selectAll() {
        mListener!!.selectAll()
    }

    /** 上拉操作触发时调用的接口  */
    fun setLoadMoreListener(loadMoreListener: LoadMoreListener) {
        mListener = loadMoreListener
    }

    interface LoadMoreListener {
        /**
         * 加载更多
         */
        fun onLoad()
        /**
         * 查看全部测评
         */
        fun selectAll()
    }
}