package com.develop.food.base.router

import android.app.Activity
import android.app.Application
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter


object ARouterUtils {


    fun init(application: Application,isDebug :Boolean) {
        ARouter.openLog() // 打印日志
        if (isDebug) {
            ARouter.openDebug() // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(application) // 尽可能早，推荐在Application中初始化
        ARouter.getInstance().inject(this)
    }

}

/**
 * 注意block内不要再调用navigation
 */
fun Activity.navigateTo(
    path: String,
    params: (Postcard.() -> Unit)? = null
) {
    val postcard = ARouter.getInstance().build(path)
    params?.let { postcard.it() }
    postcard.navigation(this)
}

fun Activity.navigateTo(
    path: String,
    params: (Postcard.() -> Unit)?,
    callback: NavigationCallback?
) {
    val postcard = ARouter.getInstance().build(path)
    params?.let { postcard.it() }
    postcard.navigation(this, callback)
}

fun Application.navigateTo(
    path: String,
    params: (Postcard.() -> Unit)? = null
) {
    val postcard = ARouter.getInstance().build(path)
    params?.let { postcard.it() }
    postcard.navigation(this)
}

fun Application.navigateTo(
    path: String,
    params: (Postcard.() -> Unit)?,
    callback: NavigationCallback?
) {
    val postcard = ARouter.getInstance().build(path)
    params?.let { postcard.it() }
    postcard.navigation(this, callback)
}

/**
 * Fragment navigate to
 */
fun Fragment.navigateTo(
    path: String,
    params: (Postcard.() -> Unit)? = null
) {
    requireActivity().navigateTo(path, params)
}