package com.seven.ikar.kotlin.core.network

object ServerConfig {
    const val MessengerBaseUrl = "https://ikar.kusoft.xyz"
    const val ArgusBaseUrl = "https://argus.kusoft.xyz"
    const val AndroidAppSlug = "ikar-android"
    const val AndroidAppPlatform = "android"
    const val AndroidUpdateChannel = "stable"
    const val AndroidManifestPath = "/api/apps/" + AndroidAppSlug + "/manifest?platform=" + AndroidAppPlatform + "&channel=" + AndroidUpdateChannel
    const val HubPath = "/hubs/messenger"

    fun absoluteMessengerUrl(path: String): String = buildAbsoluteUrl(MessengerBaseUrl, path)

    fun absoluteArgusUrl(path: String): String = buildAbsoluteUrl(ArgusBaseUrl, path)

    fun normalizeMediaUrl(path: String?): String? {
        if (path.isNullOrBlank()) {
            return null
        }

        return when {
            path.startsWith("http://") || path.startsWith("https://") -> path
            path.startsWith("/api/") || path.startsWith("/uploads/") -> buildAbsoluteUrl(MessengerBaseUrl, path)
            else -> path
        }
    }

    private fun buildAbsoluteUrl(baseUrl: String, path: String): String {
        if (path.startsWith("http://") || path.startsWith("https://")) {
            return path
        }

        return if (path.startsWith("/")) {
            baseUrl + path
        } else {
            "$baseUrl/$path"
        }
    }
}
