import enum

from bitarray import bitarray
from bitarray.util import int2ba, ba2int

from okdmr.dmrlib.utils.bits_interface import BitsInterface


@enum.unique
class ReasonCode(BitsInterface, enum.Enum):
    """
    ETSI TS 102 361-2 V2.4.1 (2017-10) - 7.2.3  Reason Code
    """

    MSDoesNotSupportThisFeatureOrService = 0b00100001

    @classmethod
    def _missing_(cls, value: object) -> "ReasonCode":
        raise ValueError(f"ReasonCode value {value} is undefined")

    def as_bits(self) -> bitarray:
        return int2ba(self.value, length=8)

    @staticmethod
    def from_bits(bits: bitarray) -> "ReasonCode":
        return ReasonCode(ba2int(bits[0:8]))
