package cn.muchinfo.rma.view.autoWidget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import cn.muchinfo.rma.R
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader


class SwipeToLoadLayout(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : SmartRefreshLayout(context, attributeSet, defStyle) {

    companion object {
        init {
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ -> ClassicsHeader(context).setPrimaryColor(context.resources.getColor(
                R.color.white)).setAccentColor(Color.BLACK) }
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ -> ClassicsFooter(context).setPrimaryColor(context.resources.getColor(R.color.white)).setAccentColor(Color.BLACK) }
        }
    }

    constructor(context: Context) : this(context, null, 0)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    fun getEnableLoadMore(): Boolean = mEnableLoadMore

    fun getIsRefreshing(): Boolean = mState == RefreshState.Refreshing

    fun getIsLoadingMore(): Boolean = mState == RefreshState.Loading

    init {
        setDisableContentWhenLoading(true)
        setDisableContentWhenRefresh(true)
    }

    inline fun <T : View> T.lparams(
            c: Context?,
            attrs: AttributeSet?,
            init: SmartRefreshLayout.LayoutParams.() -> Unit
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(c!!, attrs!!)
        layoutParams.init()
        this@lparams.layoutParams = layoutParams
        return this
    }

    fun <T : View> T.lparams(
            c: Context?,
            attrs: AttributeSet?
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(c!!, attrs!!)
        this@lparams.layoutParams = layoutParams
        return this
    }

    inline fun <T : View> T.lparams(
            width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            init: SmartRefreshLayout.LayoutParams.() -> Unit
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(width, height)
        layoutParams.init()
        this@lparams.layoutParams = layoutParams
        return this
    }

    fun <T : View> T.lparams(
            width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(width, height)
        this@lparams.layoutParams = layoutParams
        return this
    }

    inline fun <T : View> T.lparams(
            source: ViewGroup.LayoutParams?,
            init: SmartRefreshLayout.LayoutParams.() -> Unit
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(source!!)
        layoutParams.init()
        this@lparams.layoutParams = layoutParams
        return this
    }

    fun <T : View> T.lparams(
            source: ViewGroup.LayoutParams?
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(source!!)
        this@lparams.layoutParams = layoutParams
        return this
    }

    inline fun <T : View> T.lparams(
            source: ViewGroup.MarginLayoutParams?,
            init: SmartRefreshLayout.LayoutParams.() -> Unit
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(source!!)
        layoutParams.init()
        this@lparams.layoutParams = layoutParams
        return this
    }

    fun <T : View> T.lparams(
            source: ViewGroup.MarginLayoutParams?
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(source!!)
        this@lparams.layoutParams = layoutParams
        return this
    }

    inline fun <T : View> T.lparams(
            source: SmartRefreshLayout.LayoutParams?,
            init: SmartRefreshLayout.LayoutParams.() -> Unit
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(source!!)
        layoutParams.init()
        this@lparams.layoutParams = layoutParams
        return this
    }

    fun <T : View> T.lparams(
            source: SmartRefreshLayout.LayoutParams?
    ): T {
        val layoutParams = SmartRefreshLayout.LayoutParams(source!!)
        this@lparams.layoutParams = layoutParams
        return this
    }
}