from ..message_base import MessageBase
from ..constants import (
    Subaddress, RadarMode, RadarDesignationStatus, IntBitStatus, StandbyStatus,
    FreezeStatus, DegradedStatus, RfRadiationStatus, TransitionStatus, AcquisitionResult,
    RWSSubmode, SpotFunction, ACMSubmode, GMSubmode, ExpandFunction, RangeScale,
    BarScan, AzimuthScan, VelocityScale, ZoomCommand, MapOrientation, SarFeasibility
)
from ..fields import BitField, EnumField

class MsgB7(MessageBase):
    """
    Message B7: Radar Status Tell-Back
    
    ID: B7
    Direction: RT -> BC (Transmit)
    Subaddress: 17
    Rate: 25 Hz
    Document Ref: 7.2.7
    
    Contains:
    - Current Operational Mode (Tell-back of A2)
    - Status flags (Radiation, Acquisition result, Degradation)
    - Function feedback
    """
    SUBADDRESS = Subaddress.TX_STATUS_TB
    IS_TRANSMIT = True
    RATE_HZ = 25
    WORD_COUNT = 3 # ICD Spec: 3 Words

    # --- Word 01: Radar Mode Tell-Back (Ref 7.2.7.1) ---

    # Bits 00-03: Master Mode Tell-Back
    master_mode_tb = EnumField(word_index=0, start_bit=0, width=4, enum_cls=RadarMode)

    # Bits 04-07: Radar Designation Status
    designation_status = EnumField(word_index=0, start_bit=4, width=4, enum_cls=RadarDesignationStatus)

    # Bit 08: INT-BIT Status (0=Normal, 1=In Progress)
    int_bit_status = EnumField(word_index=0, start_bit=8, width=1, enum_cls=IntBitStatus)

    # Bit 09: STBY Tell-Back
    standby_status = EnumField(word_index=0, start_bit=9, width=1, enum_cls=StandbyStatus)

    # Bit 10: FREEZE Tell-Back
    freeze_status = EnumField(word_index=0, start_bit=10, width=1, enum_cls=FreezeStatus)

    # Bit 11: Degraded Performance Status
    degraded_status = EnumField(word_index=0, start_bit=11, width=1, enum_cls=DegradedStatus)

    # Bit 12: Reserved (set to 0)
    reserved_w1_b12 = BitField(word_index=0, start_bit=12, width=1)

    # Bit 13: RF Radiation Status (0=ON, 1=OFF)
    rf_radiation_status = EnumField(word_index=0, start_bit=13, width=1, enum_cls=RfRadiationStatus)

    # Bit 14: Transition Status (0=No Trans, 1=In Progress)
    transition_status = EnumField(word_index=0, start_bit=14, width=1, enum_cls=TransitionStatus)

    # Bit 15: Last Acquisition Result (0=Fail, 1=Success)
    last_acq_result = EnumField(word_index=0, start_bit=15, width=1, enum_cls=AcquisitionResult)

    # --- Word 02: Radar Functions and Parameters Tell-Back #1 (Ref 7.2.7.2) ---

    # Bit 00: RWS Submode TB
    rws_submode_tb = EnumField(word_index=1, start_bit=0, width=1, enum_cls=RWSSubmode)

    # Bit 01: SPOT Function TB
    spot_function_tb = EnumField(word_index=1, start_bit=1, width=1, enum_cls=SpotFunction)

    # Bits 02-04: ACM Submode TB
    acm_submode_tb = EnumField(word_index=1, start_bit=2, width=3, enum_cls=ACMSubmode)

    # Bit 05: GM Submode TB
    gm_submode_tb = EnumField(word_index=1, start_bit=5, width=1, enum_cls=GMSubmode)

    # Bits 06-07: Expand Function TB
    expand_function_tb = EnumField(word_index=1, start_bit=6, width=2, enum_cls=ExpandFunction)

    # Bits 08-09: Range Scale TB
    range_scale_tb = EnumField(word_index=1, start_bit=8, width=2, enum_cls=RangeScale)

    # Bits 10-11: Number of Bars TB
    bar_scan_tb = EnumField(word_index=1, start_bit=10, width=2, enum_cls=BarScan)

    # Bits 12-13: Azimuth Scan Width TB
    azimuth_scan_tb = EnumField(word_index=1, start_bit=12, width=2, enum_cls=AzimuthScan)

    # Bit 14: Velocity Scale TB
    velocity_scale_tb = EnumField(word_index=1, start_bit=14, width=1, enum_cls=VelocityScale)

    # Bit 15: Spare

    # --- Word 03: Radar Functions and Parameters Tell-Back #2 (Ref 7.2.7.3) ---

    # Bits 00-03: Not Applicable

    # Bits 04-05: Zoom Command Tell-Back
    zoom_cmd_tb = EnumField(word_index=2, start_bit=4, width=2, enum_cls=ZoomCommand)

    # Bits 06-07: SAR Map Orientation Tell-Back
    map_orientation_tb = EnumField(word_index=2, start_bit=6, width=2, enum_cls=MapOrientation)

    # Bit 08: SAR Feasibility for Cursor
    sar_feasible_cursor = EnumField(word_index=2, start_bit=8, width=1, enum_cls=SarFeasibility)

    # Bit 09: SAR Feasibility for SPOI
    sar_feasible_spoi = EnumField(word_index=2, start_bit=9, width=1, enum_cls=SarFeasibility)