package cn.yyxx.eyuangame.core.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.webkit.CookieSyncManager
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.ImageView
import cn.yyxx.support.DensityUtils
import cn.yyxx.support.ResUtils
import cn.yyxx.support.device.DeviceInfoUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/19
 */
class AgreementDialog constructor(context: Context, url: String, val isLandscape: Boolean) : Dialog(context) {

    lateinit var ivReturn: ImageView
        private set

    init {
        initView(context)
        initWebView(url)
    }

    private fun initView(context: Context) {
        setCanceledOnTouchOutside(false)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)

        val view = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_agreement_dialog", "layout"), null)
        setContentView(view)

        val attr = window?.attributes as WindowManager.LayoutParams
        attr.height = ViewGroup.LayoutParams.WRAP_CONTENT
        if (isLandscape) {
            attr.width = DensityUtils.getHeightAndWidth(context)[0] / 2
        } else {
            attr.width = DensityUtils.getHeightAndWidth(context)[0] - 80
        }
        attr.width = ViewGroup.LayoutParams.WRAP_CONTENT
        attr.gravity = Gravity.CENTER

        ivReturn = findViewById(ResUtils.getResId(context, "yyxx_iv_return", "id"))
    }

    private fun initWebView(url: String) {
        val webView = WebView(context)
        webView.isVerticalScrollBarEnabled = true
        webView.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
        val webViewContainer = findViewById<FrameLayout>(ResUtils.getResId(context, "yyxx_webview_container", "id"))
        webViewContainer.addView(webView)

        val webSetting = webView.settings
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.saveFormData = true
        webSetting.savePassword = true
        webSetting.builtInZoomControls = false
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(false)
        webSetting.setAppCacheEnabled(true)
        webSetting.domStorageEnabled = true
        webSetting.javaScriptEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
        webSetting.setAppCachePath(context.getDir("appcache", 0).path)
        webSetting.databasePath = context.getDir("databases", 0).path
        webSetting.setGeolocationDatabasePath(context.getDir("geolocation", 0).path)
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        CookieSyncManager.createInstance(context)
        CookieSyncManager.getInstance().sync()

        if (DeviceInfoUtils.isNetworkConnected(context)) {
            webView.loadUrl(url)
        } else {
            //这种写法可以正确解码
            webView.loadData("网络异常,请检查重试", "text/html; charset=UTF-8", null)
        }

        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    if (!view.canGoBack()) {
                        return false
                    }
                    if (view.hitTestResult.type == WebView.HitTestResult.UNKNOWN_TYPE) {
                        return false
                    }
                    view.loadUrl(url)
                }
                return false
            }
        }
    }

}