package com.swago.baseswago.activity

import android.annotation.SuppressLint
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.swago.baseswago.R
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.databinding.ActivityWebBinding
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.SwagoInfo
import com.swago.baseswago.util.UserInfo
import org.json.JSONObject

/**
 *@date 2021/8/30 10:19
 *description:
 */
@Route(path = ARouteConstant.Base.webView)
class WebViewActivity : BaseXActivity<ActivityWebBinding>() {

    @Autowired
    @JvmField
    var url = ""

    private var webView: WebView? = null

    private val jsLoad by lazy {
        JSLoad()
    }

    override fun loadData() {
    }

    override fun initOther() {
        ARouter.getInstance().inject(this)
        setBackView(binding.toolbar.ivBack)

        webView = WebView(this)
        webView?.apply {
            try {
                val jsonObject = JSONObject()
                jsonObject.put("TokenParam", UserInfo.getLoginModel()?.token?:"")
                jsonObject.put("UserIdParam", UserInfo.getUserInfo()?.id?:"")
                jsonObject.put("LocaleParam", SwagoInfo.getLanguageCode())
                jsonObject.put("AppVersionParam", SwagoInfo.getVersionCode())
                jsonObject.put("ClientParam", SwagoInfo.getAppClient())
                jsonObject.put("ChannelParam", SwagoInfo.getChannel())
                settings.userAgentString = settings.userAgentString + jsonObject.toString()
            } catch (e: Exception) {
                e.printStackTrace()
            }

            settings.javaScriptEnabled = true
            addJavascriptInterface(jsLoad, "jsLoad")
            settings.allowUniversalAccessFromFileURLs = true
            settings.allowFileAccess = true
            settings.allowFileAccessFromFileURLs = true
            settings.useWideViewPort = true
            settings.loadWithOverviewMode = true
            settings.javaScriptCanOpenWindowsAutomatically = true//设置允许JS弹窗
            settings.domStorageEnabled = true
            isHorizontalScrollBarEnabled = true
            isVerticalScrollBarEnabled = true
            clearCache(true)
            webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return super.shouldOverrideUrlLoading(view, request)
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    view?.title?.let {
                        binding.toolbar.tvTitle.text = it
                    }
                }
            }

            //设置大小
            val params = ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                0
            ).apply {
                startToStart = R.id.root
                endToEnd = R.id.root
                topToBottom = R.id.toolbar
                bottomToBottom =R.id.root
            }
            this.layoutParams = params
            binding.root.addView(this)
        }
        webView?.loadUrl(url)
    }

    inner class JSLoad {
        @JavascriptInterface
        fun closeGame(){
        }
    }

    override fun initLiveData() {}
}