package com.ccwangluo.cc_quickly.ui.quick

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.ui.web.AndroidInterface
import com.ccwangluo.accelerator.utils.http.HttpConfig
import com.ccwangluo.cc_quickly.databinding.FragmentMineBinding
import com.just.agentweb.core.AgentWeb
import com.just.agentweb.core.client.DefaultWebClient
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.utils.TitleBar


class MineFragment : XPageFragment() {
    private var _binding: FragmentMineBinding? = null
    private val binding get() = _binding!!

    override fun initTitleBar(): TitleBar? {
        //不使用@Page标注的一定要注意覆盖这个方法
        return null
    }

    override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View {
        _binding = FragmentMineBinding.inflate(inflater!!, container, false)
        return binding.root
    }

    override fun initViews() {
        val agentWeb =
            createAgentWeb(this, binding.mineWebview, HttpConfig.UI_MAIN_URL)
        agentWeb.getJsInterfaceHolder().addJavaObject("android", AndroidInterface(this, agentWeb))
    }

    override fun initListeners() {

    }

    companion object {
        fun newInstance(): MineFragment {
            val fragment = MineFragment()
            return fragment
        }
    }

    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)
    }
}