package com.zswc.creativity.activity

import android.os.Bundle
import android.webkit.WebView
import com.qmuiteam.qmui.widget.webview.QMUIWebViewClient
import com.zswc.creativity.base.MBActivity
import com.zswc.creativity.databinding.ActivityWebviewBinding
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel

open class WebViewActivity : MBActivity<BViewModel<BRepository>, ActivityWebviewBinding>() {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        binding.webView.webViewClient = MyWebViewClient()
        val settings = binding.webView.settings
        settings.javaScriptEnabled = true


        intent?.extras?.getString("url", "http://www.baidu.com")?.let { binding.webView.loadUrl(it) }
    }

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

    override fun onDestroy() {
        binding.webView.destroy()
        super.onDestroy()
    }


    private class MyWebViewClient : QMUIWebViewClient(true, false) {

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
//            if (Uri.parse(url).host == "www.example.com") {
            // This is my web site, so do not override; let my WebView load the page
            return false
//            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
//            Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
//                startActivity(this)
//            }
//            return true
        }
    }

}
