package com.zj365.health.fragment

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.blankj.utilcode.util.GsonUtils
import com.tamsiree.rxkit.RxNetTool
import com.tamsiree.rxkit.RxTool
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.LogUtils
import com.xty.common.R
import com.zj365.health.BuildConfig
import com.zj365.health.DownloadFile
import com.zj365.health.databinding.FragQuestionBinding
import com.zj365.health.vm.PayCancerVm
import com.xty.network.model.CancerGoodsBean

/**
 * 全国排行榜
 */
class QuestionIdFrag : BaseVmFrag<PayCancerVm>() {
    //val binding by lazy{FragWholeRankBinding.inflate(layoutInflater)}
    val binding by lazy { FragQuestionBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    var result = mutableListOf<String>()
    override fun initView() {
        super.initView()
        binding.llDelete.setOnClickListener {
            order?.orderNo?.let {
                mViewModel.deleteQuestion(it)
            }
        }
    }

    var order: CancerGoodsBean? = null
    var status: Int? = null
    val uploadManager by lazy { DownloadFile(context as Activity) }
    private var filePath: String? = null
    override fun initData() {
        super.initData()
        var result = arguments?.getString("order")
        result?.let {
            order = GsonUtils.fromJson(it, CancerGoodsBean::class.java)
        }
        initWebSettings()
        order?.questionnaireStatus?.let {
            if (it == 1) {
                //已删除
                showData(false)
                binding.wvContent.visibility = View.GONE
                binding.llDelete.visibility = View.VISIBLE
                binding.viewNone.rlRoot.visibility = View.VISIBLE
            } else {
                showData(true)
                order?.questionnaireId?.let {
                    binding.wvContent.loadUrl(BuildConfig.url + "hmApi/h5CheckAiQuestionnaire/${it}")
                }
            }
        }
        order?.status?.let {
            if (it == 5) {
                binding.llDelete.visibility = View.VISIBLE
            } else {
                binding.llDelete.visibility = View.GONE
            }
        }


    }

    @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
                }

            }
        }
    }

    fun newInstance(data: CancerGoodsBean?): QuestionIdFrag {

        val fragment = QuestionIdFrag()
        var bundle = Bundle()
        data?.let {
            bundle.putString("order", GsonUtils.toJson(it))
        }
        fragment.arguments = bundle
        return fragment
    }

    inner class MyClient : WebViewClient() {
        override fun shouldOverrideUrlLoading(p0: WebView?, p1: String): Boolean {
            p0?.loadUrl(p1)
            return true
        }
    }

    override fun setViewModel() = PayCancerVm()
    override fun observer() {
        mViewModel.deleteOb.observe(this) {
            showData(false)
        }
    }

    fun showData(isShow: Boolean) {
        if (isShow) {
            binding.wvContent.visibility = View.VISIBLE
            binding.llDelete.visibility = View.VISIBLE
            binding.viewNone.rlRoot.visibility = View.GONE
        } else {
            binding.wvContent.visibility = View.GONE
            binding.llDelete.visibility = View.GONE
            binding.viewNone.rlRoot.visibility = View.VISIBLE
        }
    }
}