package com.rdiot.yx485.ui.web

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.*
import com.gyf.immersionbar.ktx.immersionBar
import com.king.zxing.util.LogUtils
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseActivity
import com.rdiot.yx485.databinding.ActWebBinding

/**
 *
 * @author mR2hao
 * @date 2022/12/28
 */
class WebActivity : BaseActivity<ActWebBinding>() {
    private lateinit var webUrl: String
    override fun getLayoutId() = R.layout.act_web

    companion object {
        private const val WEB_URL = "webUrl"

        fun getNewWebIntent(context: Context, url: String): Intent {
            val intent = Intent(context, WebActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
            intent.putExtra(WEB_URL, url)
            return intent
        }
    }


    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarView(binding.v)
            statusBarColor(R.color.white)
            navigationBarColor(R.color.white)
            statusBarDarkFont(true)
        }

        webUrl = intent.getStringExtra(WEB_URL) ?: ""


        binding.nb.setLeftClickListener {
            if (binding.wv.canGoBack()) {
                binding.wv.goBack()
            } else {
                finish()
            }
        }

        initWebView()
    }

    private fun initWebView() {
        binding.wv.apply {
            webViewClient = WebViewClient()
            webChromeClient = object : WebChromeClient() {
                override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
                    LogUtils.d("consoleMessage:${consoleMessage?.message()}")
                    return super.onConsoleMessage(consoleMessage)
                }

                override fun onReceivedTitle(view: WebView?, title: String?) {
                    super.onReceivedTitle(view, title)
                    binding.nb.setTitle(title)
                }
            }
            val userAgent = settings.userAgentString

            settings.apply {
                javaScriptEnabled = true
                useWideViewPort = true
                loadWithOverviewMode = false
                userAgentString = "$userAgent yongxulvjian/1.0.0"
            }
        }
        binding.wv.loadUrl(webUrl)
//        LogUtils.e(binding.wv.settings.userAgentString)

    }


    /**
     * 设置点击返回按钮，跳转到上一个html页面，而不是退出当前activity
     * @param keyCode
     * @param event
     * @return
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (event.keyCode == KeyEvent.KEYCODE_BACK) {
            if (binding.wv.canGoBack()) {
                //获取webView的浏览记录
                val mWebBackForwardList: WebBackForwardList = binding.wv.copyBackForwardList()
                //这里的判断是为了让页面在有上一个页面的情况下，跳转到上一个html页面，而不是退出当前activity
                if (mWebBackForwardList.currentIndex > 0) {
                    val historyUrl: String =
                        mWebBackForwardList.getItemAtIndex(mWebBackForwardList.currentIndex - 1)
                            .url
                    if (historyUrl != webUrl) {
                        binding.wv.goBack()
                        return true
                    }
                }
            } else {
                return super.onKeyDown(keyCode, event)
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onBackPressed() {
        if (binding.wv.canGoBack()) {
            binding.wv.goBack()
        } else {
            super.onBackPressed()
        }
    }

}