package com.remax.visualnovel.app.widget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.drake.brv.PageRefreshLayout
import com.remax.visualnovel.R
import com.remax.visualnovel.configs.NovelApplication
import com.remax.visualnovel.databinding.LayoutEmptyBinding
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.utils.spannablex.utils.dp


fun PageRefreshLayout.setEmptyText(
    @StringRes emptyTextResId: Int = 0,
    @DrawableRes emptyIcon: Int = R.mipmap.icon_new_empty,
    topMargin: Int? = null,
) {
    emptyLayout = R.layout.layout_empty
    stateEnabled = true
    onEmpty {
        LayoutEmptyBinding.bind(this).apply {
            if (emptyIcon != 0) {
                ivEmpty.setImageResource(emptyIcon)
                ivEmpty.isVisible = true
            }
            if (emptyTextResId != 0) {
                tvEmpty.setText(emptyTextResId)
                tvEmpty.isVisible = true
            }
            topMargin?.let {
                root.run {
                    setPaddingRelative(paddingStart, topMargin.dp, paddingEnd, paddingBottom)
                }
            }
        }
    }
}

fun BaseQuickAdapter<*, *>.setMyEmptyView(
    @StringRes emptyTextResId: Int = 0,
    @DrawableRes emptyIcon: Int = R.mipmap.icon_new_empty,
    topMargin: Int? = null,
    @StringRes btnText: Int = 0,
    btnInvoke: (() -> Unit)? = null
) {
    NovelApplication.getCurrentActivity()?.let { activity ->
        setEmptyView(
            EmptyView(activity).createEmptyView(emptyTextResId, emptyIcon, topMargin, btnText, btnInvoke)
        )
    }
}

class EmptyView(private val context: Context) {

    private val viewBind: LayoutEmptyBinding = LayoutEmptyBinding.inflate(LayoutInflater.from(context))

    fun createEmptyView(
        @StringRes emptyTextResId: Int = 0,
        @DrawableRes emptyIcon: Int = R.mipmap.icon_new_empty,
        topMargin: Int? = null,
        @StringRes btnText: Int = 0,
        btnInvoke: (() -> Unit)? = null
    ): View {
        setEmptyView(emptyIcon, emptyTextResId)
        topMargin?.let {
            setMarginTop(it.dp)
        }
        if (btnText != 0) {
            setBtn(btnText, btnInvoke)
        }
        return viewBind.root
    }

    private fun setEmptyView(@DrawableRes emptyIcon: Int = R.mipmap.icon_new_empty, @StringRes emptyTextResId: Int = 0): EmptyView {
        with(viewBind) {
            ivEmpty.isVisible = false
            emptyIcon.takeIf { it != 0 }?.let {
                ivEmpty.setImageResource(it)
                ivEmpty.isVisible = true
            }
            tvEmpty.isVisible = false
            emptyTextResId.takeIf { it != 0 }?.let {
                tvEmpty.text = context.getString(emptyTextResId)
                tvEmpty.isVisible = true
            }
        }
        return this
    }

    private fun setBtn(@StringRes btnText: Int, btnInvoke: (() -> Unit)? = null): EmptyView {
        with(viewBind.btnEmptyMessage) {
            isVisible = true
            text = context.getString(btnText)
            setOnClick(this) {
                btnInvoke?.invoke()
            }
        }
        return this
    }

    private fun setMarginTop(topMargin: Int): EmptyView {
        viewBind.root.run {
            setPaddingRelative(paddingStart, topMargin, paddingEnd, paddingBottom)
        }
        return this
    }

}