package com.adealink.weparty.webview.scheme

import android.net.Uri
import com.adealink.frame.router.Router
import com.adealink.weparty.commonui.ext.getActivity
import com.adealink.weparty.module.webview.Web
import com.adealink.weparty.webview.IWebView

/**
 * gami://web/page?url=${encodeURIComponent("https://test-web.gami.vip")}&safeArea=true&header=xxx
 *
 * - url: H5链接 urlencode
 * - header: 导航栏标题，设置了后客户端使用原生导航栏 & safeArea参数不生效，不传则展示全屏容器
 * - safeArea：true-客户端设置安全区 false-全屏H5 默认false
 *
 * 调用示例: gami://web/page?url=https%3A%2F%2Ftest-web.gami.vip&safeArea=true
 */
class OpenNewPageScheme(webView: IWebView) :
    WebSchemeHandler(webView, arrayOf(WebScheme.OpenNewPage.PATH)) {
    override fun handle(uri: Uri) {

        val url = uri.getQueryParameter("url")
        if (url.isNullOrEmpty()) {
            return
        }
        var showTopBar = true
        var safeArea = true
        val title = uri.getQueryParameter("header")
        if (title.isNullOrEmpty()) {
            safeArea = uri.getQueryParameter("safeArea")?.toBoolean() ?: false
            showTopBar = false
        } else {
            showTopBar = true
        }

        val activity = webView.getWebView().context?.getActivity() ?: return
        Router.build(activity, Web.FullScreen.PATH)
            .putExtra(Web.Common.EXTRA_URL, url)
            .putExtra(Web.Common.EXTRA_SHOW_TOPBAR, showTopBar)
            .putExtra(Web.Common.EXTRA_SAFE_AREA, safeArea)
            .start()
    }
}