package com.casic.br.view

import android.graphics.Bitmap
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.LinearLayout
import com.casic.br.R
import com.gyf.immersionbar.ImmersionBar
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebViewClient
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.convertColor
import com.pengxh.kt.lite.utils.Constant
import com.pengxh.kt.lite.utils.ImmerseStatusBarUtil
import kotlinx.android.synthetic.main.activity_web.*

class WebsiteActivity : KotlinBaseActivity() {

    private lateinit var agentWeb: AgentWeb
    private lateinit var url: String

    override fun initLayoutView(): Int = R.layout.activity_web

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(false).init()
        ImmerseStatusBarUtil.setColor(this, R.color.mainThemeColor.convertColor(this))

        leftBackView.setOnClickListener { finish() }
        titleView.text = "北京市燃气集团有限责任公司"
    }

    override fun initData() {
        url = intent.getStringExtra(Constant.INTENT_PARAM)!!
        agentWeb = AgentWeb.with(this)
            .setAgentWebParent(
                containerView,
                LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            .useDefaultIndicator()
            .setWebViewClient(object : WebViewClient() {
                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {

                }

                override fun onPageFinished(view: WebView?, url: String?) {

                }
            })
            .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
            .interceptUnkownUrl()
            .createAgentWeb()
            .ready()
            .go(url)
        val webView = agentWeb.webCreator.webView
        webView.settings.useWideViewPort = true
        webView.settings.builtInZoomControls = true
        webView.settings.setSupportZoom(true)
        webView.settings.displayZoomControls = false
        webView.setInitialScale(4)
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        rightOptionView.setOnClickListener {
            agentWeb.webCreator.webView.loadUrl(url)
        }
    }

    override fun onResume() {
        agentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onPause() {
        agentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        agentWeb.webLifeCycle.onDestroy()
        super.onDestroy()
    }
}