package com.colorful.keyboard.theme

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.view.View
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import com.colorful.keyboard.theme.databinding.ActivityWebPrivacyBinding
import com.gyf.immersionbar.ktx.immersionBar

class LocalPrivacyAct : AppCompatActivity() {

    private lateinit var binding: ActivityWebPrivacyBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityWebPrivacyBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initBar()
        initView()
    }

    private fun initBar() {
        immersionBar {
            statusBarDarkFont(true)
            statusBarView(binding.view)
        }
    }

    private fun initView() {
        binding.backBtn.setOnClickListener {
            finish()
        }
        initWebView()
        loadUrl()
    }

    private fun loadUrl() {
        binding.webView.loadUrl("file:///android_asset/PrivacyPolicy.html")
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebView() {
        // 设置背景色
        binding.webView.setBackgroundColor(0);
        // 设置填充透明度
        binding.webView.background.alpha = 0

        binding.webView.settings.javaScriptEnabled = true
        binding.webView.settings.domStorageEnabled = true

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            binding.webView.settings.mixedContentMode = 0
        }

        binding.webView.settings.apply {
            javaScriptEnabled = true
            loadWithOverviewMode = true
            useWideViewPort = true
            setSupportZoom(true)
            loadWithOverviewMode = true
            javaScriptCanOpenWindowsAutomatically = true
        }
        binding.webView.webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView?, title: String?) {
            }
        }
        binding.webView.webViewClient = object : WebViewClient() {
            private var mLoadError = false
            override fun shouldOverrideUrlLoading(
                view: WebView, request: WebResourceRequest?
            ): Boolean {
                val url = request?.url?.toString()
                return overrideUrlLoading(view, url)
            }

            @Suppress("OverridingDeprecatedMember")
            override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
                return overrideUrlLoading(view, url)
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                binding.progressBar.visibility = View.GONE
                if (mLoadError) {
                }
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                binding.progressBar.visibility = View.VISIBLE
                mLoadError = false
                super.onPageStarted(view, url, favicon)
            }

            @TargetApi(Build.VERSION_CODES.M)
            override fun onReceivedError(
                view: WebView?, request: WebResourceRequest?, error: WebResourceError?
            ) {
                error(error?.errorCode, error?.description, request?.url?.toString())
                super.onReceivedError(view, request, error)
            }

            @Suppress("OverridingDeprecatedMember", "DEPRECATION")
            override fun onReceivedError(
                view: WebView?, errorCode: Int, description: String?, failingUrl: String?
            ) {
                error(errorCode, description, failingUrl)
                super.onReceivedError(view, errorCode, description, failingUrl)
            }

            private fun error(code: Int?, desc: CharSequence?, url: String?) {
            }

            private fun overrideUrlLoading(view: WebView, url: String?): Boolean {
                return false
            }
        }
    }
}
