package com.jld.vod.view

import android.graphics.Bitmap
import android.os.Build
import android.view.KeyEvent
import android.webkit.WebView
import android.widget.LinearLayout
import com.jld.vod.R
import com.jld.vod.base.BaseActivity
import com.jld.vod.utils.LogUtils
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient
import kotlinx.android.synthetic.main.activity_web.*
import java.lang.reflect.Constructor
import java.lang.reflect.Field
import java.lang.reflect.Method


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/7/16 14:46
 */
class WebActivity : BaseActivity() {
    private var mAgentWeb: AgentWeb? = null
    private var mLinearLayout : LinearLayout? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_web
    }

    override fun initView() {
        super.initView()
        val intnet = intent
        val url = intnet.getStringExtra("url")
        hookWebView()

        mLinearLayout = findViewById(R.id.ll_webview)

        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(mLinearLayout!!, LinearLayout.LayoutParams(-1, -1))
            .useDefaultIndicator(-1,6)
            .setWebChromeClient(mWebChromeClient)
            .setWebViewClient(mWebViewClient)
            .createAgentWeb()
            .ready()
            .go(url)

        //后退
        iv_web_back.setOnClickListener {
            if ( mAgentWeb!!.getWebCreator().getWebView().canGoBack()) { // 表示按返回键时的操作
                mAgentWeb!!.getWebCreator().getWebView().goBack() // 后退
            }
        }

        //前进
        iv_web_go_forward.setOnClickListener {
            val webview = mAgentWeb!!.getWebCreator().getWebView()
            webview.goForward()//前进
        }

        //退出
        iv_web_exit.setOnClickListener {
            this.onBackPressed()
        }

        //回到主界面
        iv_web_home.setOnClickListener {
            go<HomeActivity>()
            finish()
        }

        //刷新
        iv_web_refresh.setOnClickListener {
            val webview = mAgentWeb!!.getWebCreator().getWebView()
            webview.reload() //刷新
        }
    }

    val mWebViewClient: WebViewClient = object : WebViewClient() {
        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            LogUtils.logD("url=="+url)
        }
    }

    val mWebChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            //LogUtils.logD("newProgress=="+newProgress)
        }
    }

    override fun onPause() {
        mAgentWeb!!.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb!!.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        mAgentWeb!!.webLifeCycle.onDestroy()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (mAgentWeb!!.handleKeyEvent(keyCode, event)) { true }
        else super.onKeyDown(keyCode, event)
    }

    private fun hookWebView() {
        var factoryClass: Class<*>? = null
        try {
            factoryClass = Class.forName("android.webkit.WebViewFactory")
            var getProviderClassMethod: Method? = null
            var sProviderInstance: Any? = null
            if (Build.VERSION.SDK_INT === 23) {
                getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass")
                getProviderClassMethod.setAccessible(true)
                val providerClass =
                    getProviderClassMethod.invoke(factoryClass) as Class<*>
                val delegateClass =
                    Class.forName("android.webkit.WebViewDelegate")
                val constructor: Constructor<*> = providerClass.getConstructor(delegateClass)
                if (constructor != null) {
                    constructor.setAccessible(true)
                    val constructor2: Constructor<*> = delegateClass.getDeclaredConstructor()
                    constructor2.setAccessible(true)
                    sProviderInstance = constructor.newInstance(constructor2.newInstance())
                }
            } else if (Build.VERSION.SDK_INT === 22) {
                getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass")
                getProviderClassMethod.setAccessible(true)
                val providerClass =
                    getProviderClassMethod.invoke(factoryClass) as Class<*>
                val delegateClass =
                    Class.forName("android.webkit.WebViewDelegate")
                val constructor: Constructor<*> = providerClass.getConstructor(delegateClass)
                if (constructor != null) {
                    constructor.setAccessible(true)
                    val constructor2: Constructor<*> = delegateClass.getDeclaredConstructor()
                    constructor2.setAccessible(true)
                    sProviderInstance = constructor.newInstance(constructor2.newInstance())
                }
            } else if (Build.VERSION.SDK_INT === 21) { // Android 21无WebView安全限制
                getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass")
                getProviderClassMethod.setAccessible(true)
                val providerClass =
                    getProviderClassMethod.invoke(factoryClass) as Class<*>
                sProviderInstance = providerClass.newInstance()
            }
            if (sProviderInstance != null) {
                val field: Field = factoryClass.getDeclaredField("sProviderInstance")
                field.setAccessible(true)
                field.set("sProviderInstance", sProviderInstance)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}