package com.sshproxy.vpn.util

import com.sshproxy.vpn.ssh.UdpRelayHealthCheck

data class CallsAvailabilityStatus(
    val availability: CallsAvailability,
    val unavailableReason: String? = null
) {
    fun hasSameDisplayAs(other: CallsAvailabilityStatus?): Boolean {
        if (other == null) {
            return false
        }
        return availability == other.availability &&
            unavailableReason == other.unavailableReason
    }

    companion object {
        val NOT_READY = CallsAvailabilityStatus(CallsAvailability.NOT_READY)
        val CHECKING = CallsAvailabilityStatus(CallsAvailability.CHECKING)
        val AVAILABLE = CallsAvailabilityStatus(CallsAvailability.AVAILABLE)

        fun fromCheckResult(
            result: UdpRelayHealthCheck.Result,
            formatReason: (UdpRelayHealthCheck.Result) -> String
        ): CallsAvailabilityStatus {
            return if (result.available) {
                AVAILABLE
            } else {
                CallsAvailabilityStatus(
                    availability = CallsAvailability.UNAVAILABLE,
                    unavailableReason = formatReason(result)
                )
            }
        }
    }
}
