package com.zswc.creativity.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.View.OnLongClickListener
import android.webkit.WebSettings
import android.webkit.WebView.HitTestResult
import androidx.lifecycle.Observer
import com.google.gson.Gson
import com.hwangjr.rxbus.RxBus
import com.zswc.creativity.base.MBActivity
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.databinding.ActivityArticleDetailBinding
import com.zswc.creativity.model.MsgList
import com.zswc.creativity.utils.WebViewUtils
import com.zswc.creativity.vmodel.ArticleDetailVModel
import java.util.*


/**
 *Created by shihuiyun
 *on 2020/9/28
 */
class ArticleDetailActivity: MBActivity<ArticleDetailVModel, ActivityArticleDetailBinding>() {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        initWeb() //初始化

        var type=intent.extras?.getString("type","0")
        vm.chsoe.value=intent.extras?.getString("chose","1")

        titleBar()!!.setBackgroundColor(Color.parseColor("#ffffff"))

        if(type.equals("-1")){
            vm.isShow.value=true

            var data=intent.extras?.getString("content")
            val item: MsgList = Gson().fromJson(data, MsgList::class.java)
            vm.msgGetInfo.value=item
            vm.isShow.value=true

            titleBar()!!.setTitle("消息详情")


            //刷新我的界面-消息数量
            RxBus.get().post(ConfigRx.REFRESH_USER_DATA, "")

        }else {
            vm.getInfo()

            vm.bean.observe(this, Observer {
                if (null == it) {
                    return@Observer
                }

                when(vm.chsoe.value){
                    "1"->{
                        titleBar()!!.setTitle("隐私协议")
                        inData(it.privacy)
                    }//隐私协议
                    "2"->{
                        titleBar()!!.setTitle("使用条款")
                        inData(it.agreement)
                    }//使用条款
                    "3"->{
                        titleBar()!!.setTitle("关于我们")
                        inData(it.about_us)
                    }//关于我们

                    "4"->{
                        titleBar()!!.setTitle("违规处罚")
                        //todo:
                       // inData(it.about_us)
                    }//违规处罚
                }


            })
        }

    }


    fun inData(content:String){

        binding.webContent.loadDataWithBaseURL(null, WebViewUtils.getHtmlData2(content), "text/html", "utf-8", null)

    }

    @SuppressLint("JavascriptInterface")
    private fun initWeb() {
        if (null != binding.webContent) {
            val ws: WebSettings = binding.webContent!!.getSettings()
            ws.javaScriptEnabled = true // 设置支持javascript脚本
            ws.blockNetworkImage = false //解决图片不显示
            ws.allowFileAccess = true // 允许访问文件
            ws.builtInZoomControls = false // 设置显示缩放按钮z
            ws.setSupportZoom(false) // 支持缩放 <span style="color:#337fe5;"> /**
            ws.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            ws.defaultTextEncodingName = "utf-8" // 设置文本编码
            ws.domStorageEnabled = true
            ws.setSupportMultipleWindows(true)
            ws.loadWithOverviewMode = true
            ws.useWideViewPort = true
            ws.setAppCacheEnabled(true)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ws.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            }
            ws.cacheMode = WebSettings.LOAD_DEFAULT // 设置缓存模式</span>
            //添加Javascript调用java对象
//        webContent.addJavascriptInterface(this, "java2js");

            //        webContent.setWebViewClient(new WebViewClientDemo());
//        webContent.setWebChromeClient(new WebViewChromeClientDemo());
            // 设置打开的网页
            // webView.loadUrl("http://orgcent.com");
            // 使用WebView来显示图片
            binding.webContent!!.setLongClickable(true)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                binding.webContent.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
            }
            binding.webContent!!.setOnLongClickListener(OnLongClickListener { v: View? ->
                val result: HitTestResult = binding.webContent!!.getHitTestResult()
                if (result.type == HitTestResult.IMAGE_TYPE) {
                    val url = result.extra
                    val paths: ArrayList<Any?> = ArrayList<Any?>()
                    paths.add(url)
                    val location = IntArray(2)
                    binding.webContent!!.getLocationOnScreen(location)
                    val height: Int = binding.webContent!!.getHeight()
                    val width: Int = binding.webContent!!.getWidth()

                    return@OnLongClickListener true
                }
                false
            })
        }
    }



    override fun binding(): ActivityArticleDetailBinding {
        return ActivityArticleDetailBinding.inflate(layoutInflater)
    }

    override fun vmClass(): Class<ArticleDetailVModel> {
        return ArticleDetailVModel::class.java
    }

    override fun title(): String? {
        return "详情"
    }

    override val isRxbus: Boolean
        get() = true
}
