package mtp.polymer.com.autowidget.utils

import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.autoWidget.StatusLayout
import cn.muchinfo.rma.view.autoWidget.SwipeToLoadLayout
import com.chad.library.adapter.base.BaseQuickAdapter

import mtp.polymer.com.automodel.PageInfo

import mtp.polymer.com.autowidget.adapter.*
import org.jetbrains.anko.toast

fun <DATA> BaseListViewState.autoBindState(statusLayout: StatusLayout?, swipeToLoadLayout: SwipeToLoadLayout?, adapter: BaseAdapterDelegate<DATA>?) {
    adapter ?: return
    @Suppress("UNCHECKED_CAST")
    when (this) {
        is BaseListViewStateInProgress -> bindProgress(statusLayout, swipeToLoadLayout)
        is BaseListViewStateSuccess<*> -> try {
            (this as BaseListViewStateSuccess<DATA>).bindSuccess(statusLayout, swipeToLoadLayout)
            adapter.setData(listStatus, commonList.list)
        } catch (e: Exception) {
        }
        is BaseListViewStateFailed -> bindFailed(statusLayout, swipeToLoadLayout, adapter)
    }
}

fun <DATA> BaseListViewState.autoBindState(statusLayout: StatusLayout?, swipeToLoadLayout: SwipeToLoadLayout?, renderList: ((ListStatus, List<DATA>) -> Unit)) {
    @Suppress("UNCHECKED_CAST")
    when (this) {
        is BaseListViewStateInProgress -> bindProgress(statusLayout, swipeToLoadLayout)
        is BaseListViewStateSuccess<*> -> try {
            (this as BaseListViewStateSuccess<DATA>).bindSuccess(statusLayout, swipeToLoadLayout)
            renderList.invoke(listStatus, commonList.list)
        } catch (e: Exception) {
        }
        is BaseListViewStateFailed -> bindFailed(statusLayout, swipeToLoadLayout)
    }
}

internal fun BaseListViewStateInProgress.bindProgress(statusLayout: StatusLayout?, swipeToLoadLayout: SwipeToLoadLayout?) {
    statusLayout?.showLoading(listStatus)
    if ((listStatus == ListStatus.INITIAL)) {
        swipeToLoadLayout?.apply {
            finishLoadMore()
            val cacheIsLoadMore = (getTag(R.id.is_enable_load_more) as? Boolean) ?: getEnableLoadMore()
            setTag(R.id.is_enable_load_more, cacheIsLoadMore)
            setEnableLoadMore(false)
        }
    }
    swipeToLoadLayout?.showLoading(listStatus)
}

internal fun <DATA> BaseListViewStateSuccess<DATA>.bindSuccess(statusLayout: StatusLayout?, swipeToLoadLayout: SwipeToLoadLayout?) {
    if (commonList.pageInfo.isFirstPage().and(commonList.list.isEmpty())) {
        statusLayout?.showEmpty()
    } else {
        statusLayout?.showSuccess()
    }
    swipeToLoadLayout?.finish(commonList.pageInfo, listStatus)
}

internal fun BaseListViewStateFailed.bindFailed(statusLayout: StatusLayout?, swipeToLoadLayout: SwipeToLoadLayout?, adapter: BaseAdapterDelegate<*>? = null) {
    statusLayout?.showError(listStatus, msg, adapter?.getDataCount() ?: 0 > 0)
    swipeToLoadLayout?.let {
        if (swipeToLoadLayout.getIsRefreshing().not() && swipeToLoadLayout.getIsLoadingMore().not() && statusLayout?.isError()?.not() == true) {
            swipeToLoadLayout.context.toast(msg)
        }
        swipeToLoadLayout.finish(null, listStatus)
    }
}


internal fun <DATA> BaseAdapterDelegate<DATA>.setData(listStatus: ListStatus, data: List<DATA>) {
    @Suppress("UNCHECKED_CAST")
    (this as? BaseQuickAdapter<DATA, *>)?.apply {
        if (listStatus == ListStatus.REFRESH || listStatus == ListStatus.INITIAL) {
            setNewData(data)
        } else {
            addData(data)
        }
        notifyDataSetChanged()
    }
}

internal fun StatusLayout.showLoading(listStatus: ListStatus) {
    if (listStatus == ListStatus.INITIAL) {
        showLoading()
    }
}

internal fun StatusLayout.showError(listStatus: ListStatus, hint: CharSequence = "", hasData: Boolean = false) {
    if (focusSuccess) {
        showSuccess()
        context.toast(hint)
    } else if (listStatus == ListStatus.INITIAL) {
        if (hasData) {
            context.toast(hint)
        } else {
            showError(hint)
        }
    }
}

internal fun SwipeToLoadLayout.showLoading(listStatus: ListStatus) {
    if (getIsRefreshing().not() && listStatus == ListStatus.REFRESH) {
        autoRefresh()
    } else if (getIsLoadingMore().not() && listStatus == ListStatus.LOAD_MORE) {
        autoLoadMore()
    }
}

internal fun SwipeToLoadLayout.finish(pageInfo: PageInfo? = null, listStatus: ListStatus) {
    if (listStatus == ListStatus.REFRESH || listStatus == ListStatus.INITIAL) {
        finishRefresh(pageInfo != null)
        if (pageInfo?.hasNextPage() == true) {
            finishLoadMore()
            (getTag(R.id.is_enable_load_more) as? Boolean)?.let {
                setEnableLoadMore(it)
            }
        } else {
            finishLoadMoreWithNoMoreData()
        }
    } else if (listStatus == ListStatus.LOAD_MORE) {
        if (pageInfo?.hasNextPage() == true) {
            finishLoadMore()
            (getTag(R.id.is_enable_load_more) as? Boolean)?.let {
                setEnableLoadMore(it)
            }
        } else {
            finishLoadMoreWithNoMoreData()
        }
    }

}