package com.example.base.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.load
import com.example.base.R
import com.example.base.extensions.gone
import com.example.base.extensions.onClick
import com.example.base.extensions.visible
import org.jetbrains.anko.textColor

/**
 * 加载view  错误view 无数据view
 */

enum class PageStatus {
    ERROR,       // 加载错误
    NO_DATA,     // 没有数据
    LOADING,     // 加载中
    GONG         // 不显示(默认)
}


class EmptyView : FrameLayout {

    private var status: PageStatus = PageStatus.LOADING

    constructor(context: Context, status: PageStatus) : super(context, null) {
        this.status = status
        initLayout(context, null)
    }

    constructor(context: Context) : super(context, null) {
        initLayout(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initLayout(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initLayout(context, attrs)
    }

    private var onReloadListener: (() -> Unit)? = null

    private val mLayoutParams by lazy {
        ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
        )
    }

    private val mLoadView: View by lazy {
        View.inflate(
            context,
            R.layout.mvvm_emptyview_loading,
            null
        )
    }
    private val mNotDataView: View by lazy {
        View.inflate(
            context,
            R.layout.mvvm_emptyview_nodata,
            null
        )
    }
    private val mErrorView: View by lazy {
        View.inflate(
            context,
            R.layout.mvvm_emptyview_error,
            null
        )
    }


    private fun initLayout(context: Context, attrs: AttributeSet?) {

        isFocusable = true
        isClickable = true

        val gifImageLoader = ImageLoader.Builder(context)
        gifImageLoader.components {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                add(ImageDecoderDecoder.Factory())
            } else {
                add(GifDecoder.Factory())
            }
        }

        mErrorView.findViewById<ImageView>(R.id.ivLogo)
            .load(R.drawable.brvah_sample_footer_loading, gifImageLoader.build())

        addView(mLoadView, mLayoutParams)
        addView(mNotDataView, mLayoutParams)
        addView(mErrorView, mLayoutParams)

        // 错误点击 重新加载
        mErrorView.setOnClickListener {
            onReloadListener?.invoke()
            setStatus(PageStatus.LOADING)
        }

        // 默认loading 状态
        setStatus(status)
    }

    fun setStatus(status: PageStatus) {
        this.status = status
        this.removeAllViews()
        this.visibility = View.VISIBLE
        when (status) {
            PageStatus.LOADING -> addView(mLoadView, mLayoutParams)
            PageStatus.NO_DATA -> addView(mNotDataView, mLayoutParams)
            PageStatus.ERROR -> addView(mErrorView, mLayoutParams)
            else -> this.visibility = View.GONE
        }

    }

    fun getStatus(): PageStatus {
        return status
    }

    fun setOnReloadListener(listener: (() -> Unit)?) {
        onReloadListener = listener
    }

    fun setNoDataText(text: String) {
        mNotDataView.findViewById<TextView>(R.id.tvNoData).text = text
    }

    fun setNoDataLogo(logo: Int) {
        mNotDataView.findViewById<ImageView>(R.id.ivLogo).setBackgroundResource(logo)
    }

    fun setBtnVisible(visible: Boolean) {
        if (visible) {
            mNotDataView.findViewById<TextView>(R.id.reLoadBtn).visible()
        } else {
            mNotDataView.findViewById<TextView>(R.id.reLoadBtn).gone()
        }
    }

    fun setBtnText(str:String){
        mNotDataView.findViewById<TextView>(R.id.reLoadBtn).text = str
    }

    fun setBtnTextColor(color: Int) {
        mNotDataView.findViewById<TextView>(R.id.reLoadBtn).textColor = color
    }

    fun setBtnTextBackground(drawable: Drawable?) {
        mNotDataView.findViewById<TextView>(R.id.reLoadBtn).background = drawable
    }

    fun setBtnTextIcon(resId: Int){
        mNotDataView.findViewById<TextView>(R.id.reLoadBtn).setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0 ,0,0)
    }

    fun noDataBtn(clickFun: () -> Unit) {
        mNotDataView.findViewById<TextView>(R.id.reLoadBtn).onClick { clickFun.invoke() }
    }

}
