"""
This module contains Enum definitions that mirror the C++ enums,
used for translating numeric data fields into human-readable strings.
"""

from enum import Enum, unique, Flag


def get_enum_name(enum_class, value):
    """Tries to get the name of an enum member, returns the value itself if not found."""
    try:
        if issubclass(enum_class, Flag):
            member = enum_class(value)
            if member.value == 0:
                try:
                    return enum_class(0).name
                except ValueError:
                    return "NONE"
            return "|".join(flag.name for flag in enum_class if flag in member)

        return enum_class(value).name
    except (ValueError, TypeError):
        return value


@unique
class Boolean(Enum):
    """Generic boolean representation (maps to validity_t, active_t etc)."""

    FALSE = 0
    TRUE = 1


@unique
class MasterMode(Enum):
    """Maps to the master_mode_t enum in idd_mode_types.h"""

    IDLE_MASTER_MODE = 0
    INT_BIT_MASTER_MODE = 1
    GM_MASTER_MODE = 2
    DBS_MASTER_MODE = 3
    RWS_MASTER_MODE = 4
    VS_MASTER_MODE = 5
    ACM_MASTER_MODE = 6
    TWS_MASTER_MODE = 7
    SEA_LOW_MASTER_MODE = 8
    SEA_HIGH_MASTER_MODE = 9
    GMTI_MASTER_MODE = 10
    BCN_MASTER_MODE = 11
    SAM_MASTER_MODE = 12
    TA_MASTER_MODE = 13
    WA_MASTER_MODE = 14
    STT_MASTER_MODE = 15
    DTT_MASTER_MODE = 16
    SSTT_MASTER_MODE = 17
    ACQ_MASTER_MODE = 18
    FTT_MASTER_MODE = 19
    AGR_MASTER_MODE = 20
    SAR_MASTER_MODE = 21
    INVALID_MASTER_MODE_ = 22
    XTST_DUMMY_MODE = 23
    XTST_HW_VALIDATION_MODE = 24
    BOOT_MASTER_MODE = 25


@unique
class SubMode(Enum):
    """Maps to the sub_mode_t enum in idd_mode_types.h"""

    NULL_SUBMODE = 0
    RWS_NAM_SUBMODE = 1
    RWS_ASM_SUBMODE = 2
    GM_RBM_SUBMODE = 3
    GM_DBS_1_SUBMODE = 4
    GM_DBS_2_SUBMODE = 5
    ACM_HUD_SUBMODE = 6
    ACM_BORESIGHT_SUBMODE = 7
    ACM_NARROW_SUBMODE = 8
    ACM_WIDE_SUBMODE = 9
    ACM_VERTICAL_SUBMODE = 10
    ACM_SLEWABLE_SUBMODE = 11
    BCN_AA_SUBMODE = 12
    BCN_AS_SUBMODE = 13
    TRK_HR_SUBMODE = 14
    INVALID_SUBMODE = 15
    SUB_ID_CARDINALITY_ = 16


@unique
class Waveform(Enum):
    """Maps to the waveform_t enum in idd_waveform_types.h"""

    WVF_IDLE = 0
    LPRF = 1
    MPRF = 2
    HPRF = 3
    LPRF_NC = 4
    MPRF_SHORT = 5
    AGR_SHORT = 6
    AGR_LONG = 7
    MPRF_LOOK_UP = 8
    MPRF_ADAPTIVE = 9
    AUTO_WITHOUT_LPRF_NC = 10


@unique
class ScanRate(Enum):
    """Maps to scan_rate_t enum in idd_mode_types.h"""

    SCAN_R_IDLE = 0
    NORMAL = 1
    SLOW = 2


@unique
class RangeScale(Enum):
    """Maps to range_scale_t enum in idd_mode_types.h"""

    RANGE_S_IDLE = 0
    NM_10 = 1
    NM_20 = 2
    NM_40 = 3
    NM_80 = 4
    NM_60 = 5
    NM_160 = 6


@unique
class AntennaOpMode(Enum):
    """Maps to op_mode_t enum in idd_antenna_types.h"""

    INVALID_MODE = 0
    HORIZONTAL_SCAN = 1
    VERTICAL_SCAN = 2
    CONICAL = 3
    POSITIONING = 4
    DBS_1 = 5
    DBS_2 = 6


@unique
class AntennaState(Enum):
    """Maps to op_antenna_state_t enum in idd_status_types.h"""

    AOS_NULL = 0
    AOS_READY = 1
    AOS_INITIALIZING = 2
    AOS_INIT_ERROR = 3


class HealthStatus(Flag):
    """Maps to op_health_status_t enum (a bitmask) in idd_status_types.h"""

    HS_ALL_OK = 0
    HS_CORE_FPGA_IF_FAIL = 1 << 1
    HS_BATCH_FAIL = 1 << 2
    HS_COMM_IF_FAIL = 1 << 3
    HS_MODE_FAIL = 1 << 4
    HS_DC_IF_FAIL = 1 << 8
    HS_DC1_FAIL = 1 << 9
    HS_DC2_FAIL = 1 << 10
    HS_DC3_FAIL = 1 << 11
    HS_EXCSYNTH_IF_FAIL = 1 << 16
    HS_EXCITER_FAIL = 1 << 17
    HS_SYNTH_FAIL = 1 << 18
    HS_AESA_IF_FAIL = 1 << 24
    HS_AESA_FAIL = 1 << 25
    HS_AESA_RFIF_FAIL = 1 << 26


class AesaError(Flag):
    """Maps to AesaErrorEnum in aesa_report.h"""

    E_AESA_OK = 0
    E_AESA_SRIO_TIMEOUT = 1 << 0
    E_AESA_FPGA_TIMEOUT = 1 << 1
    E_AESA_FPGA_CRC_ERR = 1 << 2
    E_AESA_FPGA_LEN_ERR = 1 << 3
    E_AESA_UNKNOWN_ERR = 1 << 4
    E_AESA_TOTAL_COMM_FAIL = 1 << 5
    E_AESA_CTI_LINK_ERR = 1 << 8
    E_AESA_CTI_CRC_ERR = 1 << 9
    E_AESA_CTI_CODING_ERR = 1 << 10
    E_AESA_CTI_BEAM_ERR = 1 << 11
    E_AESA_CTI_DUTY_PULSE_ERR = 1 << 12
    E_AESA_CTI_UNKNOWN_ERR = 1 << 16


# --- ENUM REGISTRY ---
ENUM_REGISTRY = {
    # DSPHDRIN -> GeHeader
    "main_header.ge_header.mode.master_mode": MasterMode,
    "main_header.ge_header.mode.operation_mode": MasterMode,
    "main_header.ge_header.mode.standby": Boolean,
    "main_header.ge_header.mode.radiate": Boolean,
    "main_header.ge_header.mode.waveform": Waveform,
    "main_header.ge_header.mode.scan_rate": ScanRate,
    "main_header.ge_header.mode.range_scale": RangeScale,
    # CDPSTS -> ModeStatus
    "cdp_sts_results.payload.data.status_chunk.data.mode": MasterMode,
    "cdp_sts_results.payload.data.status_chunk.data.sub_mode": SubMode,
    "cdp_sts_results.payload.data.status_chunk.data.range_scale": RangeScale,
    "cdp_sts_results.payload.data.status_chunk.data.stby": Boolean,
    "cdp_sts_results.payload.data.status_chunk.data.radiate": Boolean,
    "cdp_sts_results.payload.data.status_chunk.data.freeze": Boolean,
    "cdp_sts_results.payload.data.status_chunk.data.expand": Boolean,
    "cdp_sts_results.payload.data.status_chunk.data.waveform": Waveform,
    "cdp_sts_results.payload.data.status_chunk.data.health_status": HealthStatus,
    "cdp_sts_results.payload.data.status_chunk.data.antenna_state": AntennaState,
    "cdp_sts_results.payload.data.status_chunk.data.wow": Boolean,
    "cdp_sts_results.payload.data.status_chunk.data.ant_radiate": Boolean,
    # CDPSTS -> AntennaFeedback
    "cdp_sts_results.payload.data.antenna_status_chunk.data.bar_in_progress": Boolean,
    "cdp_sts_results.payload.data.antenna_status_chunk.data.frame_in_progress": Boolean,
    "cdp_sts_results.payload.data.antenna_status_chunk.data.op_mode": AntennaOpMode,
    "cdp_sts_results.payload.data.antenna_status_chunk.data.off_limits": Boolean,
    "cdp_sts_results.payload.data.antenna_status_chunk.data.loop_fail": Boolean,
    # CDPSTS -> GenericDetections
    "cdp_sts_results.payload.data.detections_chunk.data.pp_waveform_tellback": Waveform,
    "cdp_sts_results.payload.data.detections_chunk.data.pp_range_scale_tellback": RangeScale,
    "cdp_sts_results.payload.data.detections_chunk.data.pp_scan_rate_tellback": ScanRate,
    "cdp_sts_results.payload.data.detections_chunk.data.detected_in_lookup": Boolean,
    # AESA Block -> Synthetic Report
    "aesa_data.payload.comm": AesaError,
    # D1553
    "d1553_data.raw_mode": MasterMode,
}
