package com.zj365.health.act

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxNetTool
import com.tamsiree.rxkit.RxTool
import com.xty.base.act.BaseVmAct
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonUtils
import com.zj365.health.BuildConfig
import com.zj365.health.R
import com.zj365.health.databinding.ActQuestionShowBinding
import com.zj365.health.vm.PayCancerVm
import com.zj365.health.weight.QuestionDialog

/**
 * 已填问卷列表
 */
@Route(path = ARouterUrl.QUESTION_SHOW_ACT)
class QuestionAct : BaseVmAct<PayCancerVm>() {
    val binding by lazy { ActQuestionShowBinding.inflate(layoutInflater) }


    var orderNo: String? = ""
    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        orderNo = intent.getStringExtra("orderNo")
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "调查问卷 "
        binding.title.mIvBack.setOnClickListener { finish() }
        initWebSettings()
        var userId = CommonUtils.getMyUserId()
        binding.wvContent.loadUrl(BuildConfig.url+"hmApi/h5AiQuestionnaire/${userId}/${orderNo}/android")

    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebSettings() {
        binding.wvContent.isHorizontalScrollBarEnabled = false //水平不显示
        binding.wvContent.isVerticalScrollBarEnabled = false //垂直不显示
        var webViewClient = MyClient()

        binding.wvContent.apply {
            isHorizontalScrollBarEnabled = false //水平不显示
            isVerticalScrollBarEnabled = false //垂直不显示

            val webSetting: WebSettings = settings
            webSetting.apply {
                setRenderPriority(WebSettings.RenderPriority.HIGH)
                cacheMode = if (RxNetTool.isConnected(RxTool.getContext())) {
                    WebSettings.LOAD_DEFAULT // 设置缓存模式
                } else {
                    WebSettings.LOAD_CACHE_ELSE_NETWORK // 设置缓存模式
                }
                loadWithOverviewMode = true
                domStorageEnabled = true//不添加可能加载H5的页面时候出现一片空白
                databaseEnabled = true
                setAppCacheEnabled(true)
                allowFileAccess = true
                javaScriptEnabled = true
                textZoom = 110
                useWideViewPort = false//设置此属性，可任意比例缩放
                setLayerType(View.LAYER_TYPE_HARDWARE, null) // 启用硬件加速
                layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
                builtInZoomControls = false //设置内置的缩放控件。若为false，则该WebView不可缩放
                displayZoomControls = false //隐藏原生的缩放控件
                defaultTextEncodingName = "utf-8" //设置编码格式
                blockNetworkImage = false //解决图片不显示
                loadsImagesAutomatically = true //支持自动加载图片
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    setAllowFileAccessFromFileURLs(true)
                    setAllowUniversalAccessFromFileURLs(true)
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//解决视频加载不出来的问题
                    mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW;
                }
            }
            settings.domStorageEnabled = true
            this.webViewClient = webViewClient
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

            }
            webChromeClient = object : WebChromeClient() {
                override fun getDefaultVideoPoster(): Bitmap? {

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

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

                    }

                    return null
                }

            }
        }
    }

    override fun onPause() {
        super.onPause()
        //暂停WebView在后台的所有活动
        binding.wvContent.onPause()
        //暂停WebView在后台的JS活动
        binding.wvContent.pauseTimers()
    }

    override fun onResume() {
        super.onResume()
        binding.wvContent.onResume();
        binding.wvContent.resumeTimers();
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.root.removeView(binding.wvContent)
        binding.wvContent.destroy()
    }
    val tipDialog by lazy {
        QuestionDialog(this, false, "温馨提示", {
            bundle.clear()
            bundle.putString("orderNo", orderNo)
            RouteManager.goAct(ARouterUrl.UPLOAD_CHECK, bundle)
        })
    }
    inner class MyClient : WebViewClient() {
        override fun shouldOverrideUrlLoading(p0: WebView?, p1: String): Boolean {
            if (p1.contains("code:0")) {
                showDialog()
            } else {
                p0?.loadUrl(p1)
            }
            return true
        }
    }

    private fun showDialog() {
        tipDialog.show()
    }

    override fun liveObserver() {
    }

}