package com.tangem.features.disclaimer.impl.ui

import android.graphics.Bitmap
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import com.google.accompanist.web.AccompanistWebViewClient
import com.tangem.utils.logging.TangemLogger

/**
 * [AccompanistWebViewClient] for Disclaimer [WebView]
 *
 * @property onLoadingFinished lambda be invoked when client will get a result of loading (content or error)
 *
[REDACTED_AUTHOR]
 */
@Suppress("NullableToStringCall")
internal class DisclaimerWebViewClient(
    private val onLoadingFinished: (isWithError: Boolean) -> Unit,
) : AccompanistWebViewClient() {

    private val loadedUrls: MutableMap<String, Boolean> = mutableMapOf()

    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)

        TangemLogger.d("onPageStarted: $url")
        if (url != null) {
            loadedUrls[url] = false
        }
    }

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

        TangemLogger.d("onPageFinished: $url")
        if (url != null && loadedUrls.containsKey(url)) {
            loadedUrls[url] = true
            onLoadingFinished(false)
        }
    }

    override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
        super.onReceivedError(view, request, error)

        val url = request?.url?.toString()

        TangemLogger.d("onReceivedError: $url")

        if (url != null && loadedUrls.containsKey(url)) {
            loadedUrls[url] = true
            onLoadingFinished(true)
        }
    }
}