package cash.z.ecc.android.sdk.ext

import java.util.Locale

/**
 * Helper class for converting/displaying consensus branch ids. Activation height is intentionally
 * omitted since this is not the source of truth for branch information but rather a tool for
 * printing that information to users.
 */
enum class ConsensusBranchId(val displayName: String, val id: Long, val hexId: String) {
    // TODO: see if we can find a way to not rely on this separate source of truth (either stop converting from hex to display name in the apps or use Rust to get this info)
    SAPLING("Sapling", 0x76b8_09bb, "76b809bb");

    override fun toString(): String = displayName

    companion object {
        fun fromName(name: String): ConsensusBranchId? = values().firstOrNull { it.displayName.equals(name, true) }

        fun fromId(id: Long): ConsensusBranchId? = values().firstOrNull { it.id == id }

        fun fromHex(hex: String): ConsensusBranchId? = values().firstOrNull { branch ->
            hex.toLowerCase(Locale.US).replace("_", "").replaceFirst("0x", "").let { sanitized ->
                branch.hexId.equals(sanitized, true)
            }
        }
    }
}
