package com.ccwangluo.accelerator.ui.web

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.utils.http.HttpConfig
import com.ccwangluo.cc_quickly.databinding.FragmentWebviewBinding
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


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

    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 agentWeb =
            createAgentWeb(this, binding.commonWebview, HttpConfig.UI_MAIN_URL)
        agentWeb.getJsInterfaceHolder().addJavaObject("android", AndroidInterface(this, agentWeb))
    }

    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(-1, 3)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .createAgentWeb()
            .ready()
            .go(url)
    }

    override fun initListeners() {

    }
}