package pxds.etmo.ibpc

import android.app.Activity
import android.graphics.Bitmap
import android.os.Build
import android.webkit.CookieManager
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.blankj.utilcode.util.SPUtils
import com.test.basd.supercleanermax.R
import pxds.lmmd.zmwf.Khko.string

class Foee(listener: WebViewListener, val str: String) {

    private val mWebView = WebView(listener.activity)

    private val settings = mWebView.settings

    var url = str

    var icon: Bitmap? = null

    var title = str

    var progress = 0

    var isHome = false

    fun canGoForward() = mWebView.canGoForward()

    fun canGoBack() = mWebView.canGoBack()

    fun goForward() = mWebView.goForward()

    init {
        mWebView.drawingCacheBackgroundColor = -1
        mWebView.isFocusableInTouchMode = true
        mWebView.isFocusable = true
        mWebView.isDrawingCacheEnabled = false
        mWebView.setWillNotCacheDrawing(true)
        if (Build.VERSION.SDK_INT <= 22) {
            mWebView.isAnimationCacheEnabled = false
            mWebView.isAlwaysDrawnWithCacheEnabled = false
        }
        mWebView.setBackgroundColor(-1)
        mWebView.isScrollbarFadingEnabled = true
        mWebView.isSaveEnabled = true
        mWebView.setNetworkAvailable(true)
        mWebView.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                progress = newProgress
                listener.onProgressChanged(newProgress)
            }

            override fun onReceivedTitle(view: WebView?, title: String?) {
                this@Foee.title = title?.takeIf { it.isNotBlank() } ?: str
                listener.onReceivedTitle(title ?: str)
            }

            override fun onReceivedIcon(view: WebView?, icon: Bitmap?) {
                this@Foee.icon = icon
                listener.onReceivedIcon(icon)
            }
        }
        mWebView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                this@Foee.url = url ?: ""
                listener.onPageFinished(view, url ?: "")
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                this@Foee.url = url ?: ""
                listener.onPageStarted(url ?: "", favicon)
            }
        }
        settings.setGeolocationEnabled(false)
        settings.userAgentString = WebSettings.getDefaultUserAgent(listener.activity)
        settings.saveFormData = false
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
        try {
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
        } catch (_: Exception) {}
        settings.loadsImagesAutomatically = true
        settings.setSupportMultipleWindows(true)
        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        settings.textZoom = 100
        mDefaultUserAgent = settings.userAgentString
        CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true)

        if (SPUtils.getInstance().getBoolean(R.string.ޏޑކށފ_D.string(), false)) {
            setDesktop()
        } else {
            setPhone()
        }

        if (str.trim().isNotEmpty()) {
            mWebView.loadUrl(str)
        }
    }

    interface WebViewListener {
        val activity: Activity

        fun onProgressChanged(progress: Int)

        fun onReceivedTitle(title: String)

        fun onReceivedIcon(icon: Bitmap?)

        fun onPageStarted(url: String, icon: Bitmap?)

        fun onPageFinished(view: WebView?, url: String)
    }

    companion object {
        private val DESKTOP_USER_AGENT = R.string.ލމޣތލ_D.string()
        private var mDefaultUserAgent = ""
    }

    fun loadUrl(url: String) {
        mWebView.loadUrl(url)
    }

    fun goBack() = mWebView.goBack()

    fun setPhone() {
        settings.userAgentString = mDefaultUserAgent
    }

    fun setDesktop() {
        settings.userAgentString = DESKTOP_USER_AGENT
    }

    fun getWebView() = mWebView

    fun reload() {
        mWebView.reload()
    }
}