package com.dahe.gldriver.base

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.TextView
import com.dahe.gldriver.databinding.ActivityWebBinding
import com.dahe.gldriver.ui.HomeActivity
import com.dahe.mylibrary.R
import com.dahe.mylibrary.base.BaseActivity
import com.just.agentweb.AgentWeb
import com.just.agentweb.AgentWebUIControllerImplBase
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient

/**
 * @ClassName WebActivity
 * @Author john
 * @Date 2024/2/1 15:08
 * @Description TODO
 */
class WebActivity : BaseActivity<ActivityWebBinding>() {

    var mTextRight: TextView? = null
    private var mAgentWeb: AgentWeb? = null

    private var mTitle = ""
    private var mUrl = ""

    override fun initView(savedInstanceState: Bundle?) {
        mTitle = intent.extras?.getString("title").toString()
        mUrl = intent.extras?.getString("url").toString()

        setStatusBarColor(R.color.colorWhite)
        setTitleBar(mTitle) { v: View? -> if (!mAgentWeb!!.back()) finish() }

        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(binding.WebViewLayout, FrameLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .setWebChromeClient(mWebChromeClient)
            .setSecurityType(AgentWeb.SecurityType.DEFAULT_CHECK)
            .setAgentWebUIController(AgentWebUIControllerImplBase())
            .createAgentWeb()
            .ready()
            .go(mUrl)


//        mAgentWeb?.jsInterfaceHolder?.addJavaObject("phone", AndroidInterfaceWeb())
    }

    override fun initDate() {

    }

    private val mWebChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
        }

        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
            setTitleBar(
                if (TextUtils.isEmpty(mTitle)) title else mTitle
            ) { if (!mAgentWeb!!.back()) finish() }
        }
    }

//    private val mWebViewClient: WebViewClient = object : WebViewClient() {
//        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
//            super.onPageStarted(view, url, favicon)
//        }
//
//        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
//            return super.shouldOverrideUrlLoading(view, request)
//        } //        @Override
//        //        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//        //            //该方法在Build.VERSION_CODES.LOLLIPOP以后有效
//        //            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//        //                String url = request.getUrl().toString();
//        //                Log.i(TAG, "shouldOverrideUrlLoading: " + url);
//        //            }
//        //            return false;
//        //        }
//    }


    //    @OnClick({R.id.common_toolBar_text_right, R.id.common_toolBar_image_right})
    //    public void onClick(View view) {
    //        if (view.getId() == R.id.common_toolBar_text_right) {
    //        } else if (view.getId() == R.id.common_toolBar_image_right) {
    //        }
    //    }
    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

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

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


//    internal class AndroidInterfaceWeb {
//        @JavascriptInterface
//        fun gohome() {
//            val intent = Intent(mContext, HomeActivity::class.java)
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
//            mContext.startActivity(intent)
//        }
//    }

}