package com.adealink.weparty.webview.util

import com.adealink.frame.aab.util.getCompatString
import com.adealink.weparty.R
import com.adealink.weparty.commonui.widget.CommonDialog

object SSLErrorHandleUtil {

    /**
     * 上一次取消处理的Url
     */
    private var lastNegativeUrl: String? = null

    fun createSSLErrorHandleDialog(url: String? = null, onPositive: () -> Unit, onNegative: () -> Boolean): CommonDialog? {
        url ?: return null
        //如果上一次没有允许访问的Url与这一次相同，则保持取消处理
        if (lastNegativeUrl == url) {
            if (onNegative.invoke()) {
                //可控是否清除
                lastNegativeUrl = null
            }
            return null
        }
        return CommonDialog.Builder()
            .title(getCompatString(R.string.common_error))
            .message(getCompatString(R.string.common_ssl_error_tip))
            .canceledOnTouchOutside(false)
            .negativeText(getCompatString(com.adealink.weparty.R.string.commonui_cancel))
            .positiveText(getCompatString(com.adealink.weparty.R.string.commonui_confirm))
            .onPositive {
                onPositive.invoke()
            }.onNegative {
                lastNegativeUrl = url
                if (onNegative.invoke()) {
                    //可控是否清除
                    lastNegativeUrl = null
                }
            }.build()
    }
}