package com.ccwangluo.accelerator.ui.web

import android.graphics.Color
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.ccwangluo.accelerator.databinding.FragmentWebviewBinding
import com.ccwangluo.accelerator.utils.LoginUtils
import com.just.agentweb.core.AgentWeb
import com.just.agentweb.core.client.DefaultWebClient
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.utils.TitleBar


@Page(name = "webview")
class CommonWebViewFragment : XPageFragment() {

    override fun initTitleBar(): TitleBar? {
        return null
    }

    var agentWeb: AgentWeb? = null
    lateinit var binding: FragmentWebviewBinding
    override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View {
        binding = FragmentWebviewBinding.inflate(inflater!!, container, false)
        return binding.root
    }

    override fun initViews() {
        val url = arguments?.getString(WEBVIEW_URL_KEY)

        agentWeb =
            url?.let {
                var weburl = it
                LoginUtils.token?.let {
                    if (weburl.contains("?")){
                        weburl = weburl + "&token=$it"
                    } else {
                        weburl = weburl + "?token=$it"
                    }
                }
                createAgentWeb(this, binding.commonWebview, weburl)
            }
        agentWeb?.getJsInterfaceHolder()
            ?.addJavaObject("android", AndroidInterface(this, agentWeb!!))
        agentWeb?.webCreator?.webView?.requestFocus(View.FOCUS_DOWN);
    }

    fun createAgentWeb(fragment: Fragment?, viewGroup: ViewGroup, url: String): AgentWeb {
        return AgentWeb.with(fragment!!)
            .setAgentWebParent(
                viewGroup!!,
                -1,
                FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            .useDefaultIndicator(Color.parseColor("#3CA4FD"), 3)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .createAgentWeb()
            .ready()
            .go(url)
    }

    override fun initListeners() {

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
    }

    override fun getUserVisibleHint(): Boolean {
        return super.getUserVisibleHint()
    }

    override fun onPause() {
        agentWeb?.getWebLifeCycle()?.onPause()
        super.onPause()
    }

    override fun onResume() {
        Log.e("====onResume","====")
        agentWeb?.getWebLifeCycle()?.onResume()
        super.onResume()

        refreshLogin()
    }

    private fun refreshLogin() {
        LoginUtils.token?.let {
            Log.e("refreshLogin","===token:"+LoginUtils.token)
            agentWeb?.getJsAccessEntrace()
                ?.quickCallJs("userToken", it)
        } ?: let {
            Log.e("clearToken","===token")
            agentWeb?.getJsAccessEntrace()
                ?.quickCallJs("clearToken")
        }
    }

    override fun onDestroyView() {
        agentWeb?.getWebLifeCycle()?.onDestroy()
        super.onDestroyView()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            agentWeb?.let {
                if (it.back()) {
                    return true
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }


    companion object {
        var WEBVIEW_URL_KEY = "WEBVIEW_URL_KEY"
    }
}