package com.adealink.weparty.webview.payermax

import android.app.Activity
import android.graphics.Bitmap
import android.net.http.SslError
import android.os.Build
import android.webkit.SslErrorHandler
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentActivity
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.webview.CommonWebViewClient
import com.adealink.weparty.webview.IWebView
import com.adealink.weparty.webview.loader.ResourceRequest
import com.adealink.weparty.webview.loader.webResourceLoader
import com.adealink.weparty.webview.util.SSLErrorHandleUtil
import com.ushareit.easysdk.web.view.SPBaseWebViewClient

class SpWebViewClient(
    activity: Activity,
    private val iWebView: IWebView,
    private val webViewClient: CommonWebViewClient?
) : SPBaseWebViewClient(activity, iWebView.getWebView()) {
    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)
        webViewClient?.onPageStarted(view, url, favicon)
    }

    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)
        webViewClient?.onPageFinished(view, url)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onReceivedError(
        view: WebView?,
        request: WebResourceRequest?,
        error: WebResourceError?,
    ) {
        super.onReceivedError(view, request, error)
        webViewClient?.onReceivedError(view, request, error)
    }

    override fun onReceivedError(
        view: WebView?,
        errorCode: Int,
        description: String?,
        failingUrl: String?,
    ) {
        super.onReceivedError(view, errorCode, description, failingUrl)
        webViewClient?.onReceivedError(view, errorCode, description, failingUrl)
    }

    override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
        val activity = AppUtil.currentActivity as? FragmentActivity
        activity?.supportFragmentManager?.let {
            SSLErrorHandleUtil.createSSLErrorHandleDialog(view?.url, onPositive = {
                handler?.proceed()
            }, onNegative = {
                super.onReceivedSslError(view, handler, error)
                webViewClient?.onReceivedSslError(view, handler, error)
                false
            })?.show(it)
        }
    }

    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        return webViewClient?.shouldOverrideUrlLoading(view, request) ?: super.shouldOverrideUrlLoading(
            view,
            request
        )
    }

    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        return webViewClient?.shouldOverrideUrlLoading(view, url) ?: super.shouldOverrideUrlLoading(
            view,
            url
        )
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun shouldInterceptRequest(
        view: WebView,
        request: WebResourceRequest
    ): WebResourceResponse? {
        val response = super.shouldInterceptRequest(view, request)
        if (response != null) {
            return response
        }

        val isRedirect = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            request.isRedirect
        } else {
            false
        }
        return webResourceLoader.load(
            ResourceRequest(
                request.url.toString(),
                request.method,
                request.requestHeaders,
                request.isForMainFrame,
                isRedirect
            )
        )
    }

}


