package com.zxhl.cms.common

import android.content.Intent
import android.os.Build
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.utils.FileUtils
import kotlinx.android.synthetic.main.activity_web.*


class WebActivity : BaseActivity() {
    private var chromeClient: PaxWebChromeClient? = null

    override fun onClick(p0: View?) {

    }

    override fun layoutID(): Int = R.layout.activity_web

    override fun init() {
        id_activity_web_toolbar?.setNavigationOnClickListener { finish() }
        val title = intent?.data?.getQueryParameter("title") ?: ""
        val url = intent?.data?.getQueryParameter("url") ?: ""


        initWebView()
        id_activity_web_title?.text = title

        id_activity_web_view?.loadUrl(url)

    }


    private fun initWebView() {
        val settings = id_activity_web_view?.settings
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            settings?.mediaPlaybackRequiresUserGesture = true//设置视频不自动播放
        }
        chromeClient= PaxWebChromeClient(this)
        settings?.javaScriptCanOpenWindowsAutomatically = true//支持js自动弹框
        settings?.setSupportZoom(true) //支持缩放
        settings?.setNeedInitialFocus(false)
        settings?.allowFileAccess = true
        settings?.setRenderPriority(WebSettings.RenderPriority.HIGH)
        settings?.builtInZoomControls = true//支持手势缩放
        settings?.displayZoomControls = false //是否显示缩放按钮
        // >= 19(SDK4.4)启动硬件加速，否则启动软件加速
        settings?.loadsImagesAutomatically = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
        settings?.useWideViewPort = true //设置内容自适应屏幕大小
        settings?.loadWithOverviewMode = true
        settings?.saveFormData = true
        settings?.cacheMode = WebSettings.LOAD_NO_CACHE
        settings?.setEnableSmoothTransition(true)
        settings?.setSupportMultipleWindows(false)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings?.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            settings?.mediaPlaybackRequiresUserGesture = false
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true)
        }
        // support android API 7-
        try {
            // API 7, LocalStorage/SessionStorage
            settings?.domStorageEnabled = true
            settings?.databaseEnabled = true
            settings?.databasePath = FileUtils.getWebViewCacheDiectory()
            // API 7， Web SQL Database, 需要重载方法（WebChromeClient）才能生效，无法只通过反射实现
        } catch (e: Exception) {
        }


        try {
            // API 7， Application Storage
            settings?.setAppCacheEnabled(true)
            settings?.setAppCachePath(FileUtils.getWebViewCacheDiectory())
            settings?.setAppCacheMaxSize(5 * 1024 * 1024)
        } catch (e: Exception) {
        }
        try {
            // API 5， Geolocation
            settings?.setGeolocationEnabled(true)
            settings?.setGeolocationDatabasePath(FileUtils.getWebViewCacheDiectory())
        } catch (e: Exception) {
        }



        id_activity_web_view?.isHorizontalScrollBarEnabled = false
        id_activity_web_view?.overScrollMode = View.OVER_SCROLL_NEVER // 取消WebView中滚动或拖动到顶部、底部时的阴影
        id_activity_web_view?.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
//        settings.userAgentString = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; zh-tw) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16"
        //注意：以下两项是必须设置，其他设置选项根据自身项目需求进行配置
        settings?.javaScriptEnabled = true


      id_activity_web_view?.webChromeClient = chromeClient


        id_activity_web_view?.webViewClient = object : WebViewClient() {


        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        chromeClient?.onActivityResult(requestCode,resultCode,data);
        super.onActivityResult(requestCode, resultCode, data)
    }



}
