package com.develop.base.ext

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


/**
 * 注意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)
}

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