package com.ccwangluo.accelerator.ui.home

import android.graphics.Color
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.FragmentMineBinding
import com.ccwangluo.accelerator.ui.web.AndroidInterface
import com.ccwangluo.accelerator.manager.LoginUtils
import com.ccwangluo.accelerator.utils.SysUtils
import com.github.shadowsocks.http.HttpConfig
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!!
    private var agentWeb: AgentWeb? = null

    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() {
        var weburl = HttpConfig.UI_MAIN_URL + "?channel=${SysUtils.getChannel()}"
        LoginUtils.token?.let {
            weburl = weburl + "&token=${it}"
        }
        agentWeb =
            createAgentWeb(this, binding.mineWebview, weburl)
        agentWeb?.getJsInterfaceHolder()
            ?.addJavaObject("android", AndroidInterface(this, agentWeb!!))
        refreshLogin()
    }

    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(Color.parseColor("#3CA4FD"), 3)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .createAgentWeb()
            .ready()
            .go(url)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        refreshLogin()
    }

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

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

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

    override fun onResume() {
        refreshLogin()
        agentWeb?.getWebLifeCycle()?.onResume()
        super.onResume()
    }

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


}