package com.xuqm.base.web

import android.webkit.WebView
import com.xuqm.base.App
import com.xuqm.base.common.GsonImplHelp
import com.xuqm.base.extensions.log

class XWebCallBack(val webView: WebView) {
    fun error(callBackId: String, msg: String) {
        sendCallBack(XWebJsCallModel(callBackId, -1, msg, null))
    }

    fun success(callBackId: String, data: Any?) {
        sendCallBack(XWebJsCallModel(callBackId, 0, null, data))
    }

    private fun sendCallBack(model: XWebJsCallModel) {
        App.getInstance().runOnUiThread {
            GsonImplHelp.get().toJson(model).log()
            webView.loadUrl(
                "javascript:__callbackMessageFromNative("+GsonImplHelp.get().toJson(model)+")"
            )
        }
    }
}