package org.session.libsignal.service.loki.api.opengroups

import org.session.libsignal.utilities.JsonUtil

public data class PublicChat(
    public val channel: Long,
    private val serverURL: String,
    public val displayName: String,
    public val isDeletable: Boolean
) {
    public val server get() = serverURL.toLowerCase()
    public val id get() = getId(channel, server)

    companion object {

        @JvmStatic fun getId(channel: Long, server: String): String {
            return "$server.$channel"
        }

        @JvmStatic fun fromJSON(jsonAsString: String): PublicChat? {
            try {
                val json = JsonUtil.fromJson(jsonAsString)
                val channel = json.get("channel").asLong()
                val server = json.get("server").asText().toLowerCase()
                val displayName = json.get("displayName").asText()
                val isDeletable = json.get("isDeletable").asBoolean()
                return PublicChat(channel, server, displayName, isDeletable)
            } catch (e: Exception) {
                return null
            }
        }
    }

    public fun toJSON(): Map<String, Any> {
        return mapOf( "channel" to channel, "server" to server, "displayName" to displayName, "isDeletable" to isDeletable )
    }
}
