package com.xty.common

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.tamsiree.rxkit.RxNetTool
import com.tamsiree.rxkit.RxTool

class WebViewOrigin {
    fun initWebSettings(context: Context, view: WebView) {
        view.isHorizontalScrollBarEnabled = false //水平不显示
        view.isVerticalScrollBarEnabled = false //垂直不显示
        var   webViewClient = WebViewClient()

        val webSetting: WebSettings =  view.settings
        webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH)
        if (RxNetTool.isConnected(RxTool.getContext())) {
            webSetting.cacheMode = WebSettings.LOAD_DEFAULT // 设置缓存模式
        } else {
            webSetting.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK // 设置缓存模式
        }
        webSetting.loadWithOverviewMode = true
        webSetting.domStorageEnabled = true
        webSetting.databaseEnabled = true
        webSetting.displayZoomControls = false
        webSetting.setAppCacheEnabled(true)
        webSetting.allowFileAccess = true
        webSetting.setJavaScriptEnabled(true)
        webSetting.textZoom=110
        webSetting.useWideViewPort = false
        webSetting.builtInZoomControls = false //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSetting.displayZoomControls = false //隐藏原生的缩放控件
        webSetting.defaultTextEncodingName = "utf-8" //设置编码格式
        webSetting.blockNetworkImage = false //解决图片不显示
        webSetting.loadsImagesAutomatically = true //支持自动加载图片
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            webSetting.setAllowFileAccessFromFileURLs(true)
            webSetting.setAllowUniversalAccessFromFileURLs(true)
        }

        view.webViewClient = webViewClient
        view.webChromeClient = object : WebChromeClient() {
            override fun getDefaultVideoPoster(): Bitmap? {

                try{
                    //这个地方是加载h5的视频列表 默认图   点击前的视频图
                    LogUtils.d("   加载视频默认图片");
                    return BitmapFactory.decodeResource( context.getResources(),
                        R.mipmap.ic_chat_picture);

                }catch( e:Exception){
                    LogUtils.e("默认图片加载失败");

                }

                return  null
            }

        }
    }
}