package com.abbidot.tracker.ui.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.webkit.WebView
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.PayResultBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityWebViewBinding
import com.abbidot.tracker.deprecated.ui.activity.profile.recharge.PaymentResultActivity
import com.abbidot.tracker.ui.activity.subscribe.PaymentSuccessActivity
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.widget.BrowserView

class WebViewActivity : BaseActivity<ActivityWebViewBinding>(ActivityWebViewBinding::inflate) {

    //支付成功后的结果
    private var mPayResult: PayResultBean? = null

    override fun getTopBar() = mViewBinding.webViewBar.titleTopBar

    companion object {
        fun startWebViewActivity(activity: Activity, title: String, url: String) {
            val intent = Intent(activity, WebViewActivity::class.java)
            intent.putExtra("url", url)
            intent.putExtra("title", title)
            activity.startActivity(intent)
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initData() {
        super.initData()

        intent.extras?.apply {
            setTopBarTitle(this.getString(ConstantString.Title).toString())
            val url = this.getString(ConstantString.Url).toString()
            LogUtil.e(url)

            mPayResult = Util.getParcelableAdaptive(
                intent, ConstantString.Amount, PayResultBean::class.java
            )

            mViewBinding.webViewQmuiWebView.apply {
                setBrowserViewClient(MyBrowserViewClient(this@WebViewActivity, mPayResult))
                setBrowserChromeClient(MyBrowserChromeClient(mViewBinding.webViewQmuiWebView))
                setDownloadListener { url1, userAgent, contentDisposition, mimetype, contentLength ->
                    val uri = Uri.parse(url1)
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    startActivity(intent)
                }
                loadUrl(url)
            }

        }
    }

    override fun onResume() {
        mViewBinding.webViewQmuiWebView.onResume()
        super.onResume()
    }

    override fun onPause() {
        mViewBinding.webViewQmuiWebView.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        mViewBinding.webViewQmuiWebView.onDestroy()
        super.onDestroy()
    }


    private class MyBrowserViewClient(activity: BaseActivity<*>, payResult: PayResultBean? = null) :
        BrowserView.BrowserViewClient() {

        private val mActivity = activity
        private val mPayResult = payResult

        /**
         * 网页加载错误时回调，这个方法会在 onPageFinished 之前调用
         */
        override fun onReceivedError(
            view: WebView?, errorCode: Int, description: String?, failingUrl: String?
        ) {
            // 这里为什么要用延迟呢？因为加载出错之后会先调用 onReceivedError 再调用 onPageFinished
        }

        /**
         * 开始加载网页
         */
        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            LogUtil.e("开始拦截:$url")
            url?.apply {
                //支付成功和失败
                if (url.startsWith("https://www.baidu.com")) {
                    val intent = Intent(mActivity, PaymentSuccessActivity::class.java)
                    intent.putExtra(ConstantString.Amount, mPayResult)
                    mActivity.startActivityFinish(intent)
                } else if (url.startsWith("https://www.qq.com")) {
                    val intent = Intent(mActivity, PaymentResultActivity::class.java)
                    intent.putExtra(ConstantString.Type, ConstantInt.Type2)
                    mActivity.startActivityFinish(intent)
                }
            }
        }

        /**
         * 完成加载网页
         */
        override fun onPageFinished(view: WebView?, url: String?) {
        }
    }

    private class MyBrowserChromeClient(view: BrowserView) : BrowserView.BrowserChromeClient(view) {
        /**
         * 收到网页标题
         */
        override fun onReceivedTitle(view: WebView?, title: String?) {
        }

        /**
         * 收到加载进度变化
         */
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
        }
    }

    override fun leftBackOnClick() {
        if (mViewBinding.webViewQmuiWebView.canGoBack()) {
            // 后退网页并且拦截该事件
            mViewBinding.webViewQmuiWebView.goBack()
        } else super.leftBackOnClick()
    }
}