package com.adealink.weparty.webview.component

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.weparty.R
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.widget.CommonEmptyErrorView

/**
 */
class ErrorComp(
    lifecycleOwner: LifecycleOwner,
    val webView: com.adealink.weparty.webview.BaseWebView,
    private val errorView: CommonEmptyErrorView,
    val reload: () -> Unit,
) : ViewComponent(lifecycleOwner) {

    fun showError(msg: String? = null) {
        webView.visibility = View.GONE
        errorView.visibility = View.VISIBLE
        errorView.show(
            R.drawable.common_net_error_ic,
            R.string.web_view_page_load_error_tip,
            null,
            R.string.common_retry,
            {
                reload()
            })
        errorMsg(msg)
    }

    fun hideError() {
        webView.visibility = View.VISIBLE
        errorView.visibility = View.GONE
    }

    private fun errorMsg(msg: String?) {
        if(msg == null) {
            errorView.binding.tvEmptyErrorSubTitle.gone()
        }else {
            errorView.binding.tvEmptyErrorSubTitle.show()
            errorView.binding.tvEmptyErrorSubTitle.text = msg
        }
    }

}