@file:Suppress("DEPRECATION")

package cn.muchinfo.rma.view.autoWidget

import android.annotation.SuppressLint
import android.content.Context
import android.webkit.CookieSyncManager
import com.tencent.smtt.sdk.CookieManager
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient


class X5WebView(context: Context) : WebView(context) {

    private val client = object : WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }
    }

    init {
        setWebViewClient(client)
        initWebViewSettings()
    }

    fun clearAll() {
        //清空所有Cookie
        CookieSyncManager.createInstance(context)  //Create a singleton CookieSyncManager within a context
        val cookieManager = CookieManager.getInstance() // the singleton CookieManager instance
        cookieManager.removeAllCookie()// Removes all cookies.
        CookieSyncManager.getInstance().sync() // forces sync manager to sync now
        settings.javaScriptEnabled = false
        setWebChromeClient(null)
        setWebViewClient(null)
        clearCache(true)
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        val webSetting = this.settings
        webSetting.javaScriptEnabled = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.builtInZoomControls = true
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(true)
         webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true)
         webSetting.setDatabaseEnabled(true);
        webSetting.domStorageEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(java.lang.Long.MAX_VALUE)
//         webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
         webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.cacheMode = WebSettings.LOAD_NO_CACHE

        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }

//    override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
//        val ret = super.drawChild(canvas, child, drawingTime)
//        canvas.save()
//        val paint = Paint()
//        paint.color = 0x7fff0000
//        paint.textSize = 24f
//        paint.isAntiAlias = true
//        if (x5WebViewExtension != null) {
//            canvas.drawText(this.context.packageName + "-pid:"
//                    + android.os.Process.myPid(), 10f, 50f, paint)
//            canvas.drawText(
//                    "X5  Core:" + QbSdk.getTbsVersion(this.context), 10f,
//                    100f, paint)
//        } else {
//            canvas.drawText(this.context.packageName + "-pid:"
//                    + android.os.Process.myPid(), 10f, 50f, paint)
//            canvas.drawText("Sys Core", 10f, 100f, paint)
//        }
//        canvas.drawText(Build.MANUFACTURER, 10f, 150f, paint)
//        canvas.drawText(Build.MODEL, 10f, 200f, paint)
//        canvas.restore()
//        return ret
//    }


}