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

class MsgA2(MessageBase):
    """
    Message A2: Radar Operation Command
    
    ID: A2
    Direction: BC -> RT (Receive)
    Subaddress: 02
    Rate: 25 Hz
    Document Ref: 7.1.2
    """
    SUBADDRESS = Subaddress.RX_COMMAND
    IS_TRANSMIT = False
    RATE_HZ = 25

    # --- Word 01: Radar Mode Command (Ref 7.1.2.1) ---
    
    # Bits 00-03: Master Mode Selection
    master_mode = EnumField(word_index=0, start_bit=0, width=4, enum_cls=RadarMode)
    
    # Bits 04-07: Designation Control
    designation_ctrl = EnumField(word_index=0, start_bit=4, width=4, enum_cls=DesignationControl)
    
    # Bit 08: INT-BIT Function
    int_bit_req = EnumField(word_index=0, start_bit=8, width=1, enum_cls=IntBitStatus)
    
    # Bit 09: STBY Function
    standby_cmd = EnumField(word_index=0, start_bit=9, width=1, enum_cls=StandbyStatus)
    
    # Bit 10: FREEZE Function
    freeze_cmd = EnumField(word_index=0, start_bit=10, width=1, enum_cls=FreezeStatus)
    
    # Bit 11: Power-Up Stop (Reserved, set to Normal)
    pwr_up_stop_reserved = BitField(word_index=0, start_bit=11, width=1)
    
    # Bit 12: Reserved
    reserved_w1_b12 = BitField(word_index=0, start_bit=12, width=1)
    
    # Bit 13: Silence (Reserved, set to Off)
    silence_reserved = BitField(word_index=0, start_bit=13, width=1)
    
    # Bits 14-15: Spare

    # --- Word 02: Radar Functions and Parameters #1 (Ref 7.1.2.2) ---
    
    # Bit 00: RWS Submode
    rws_submode = EnumField(word_index=1, start_bit=0, width=1, enum_cls=RWSSubmode)
    
    # Bit 01: SPOT Function
    spot_function = EnumField(word_index=1, start_bit=1, width=1, enum_cls=SpotFunction)
    
    # Bits 02-04: ACM Submode
    acm_submode = EnumField(word_index=1, start_bit=2, width=3, enum_cls=ACMSubmode)
    
    # Bit 05: GM Submode
    gm_submode = EnumField(word_index=1, start_bit=5, width=1, enum_cls=GMSubmode)
    
    # Bits 06-07: Expand Function
    expand_function = EnumField(word_index=1, start_bit=6, width=2, enum_cls=ExpandFunction)
    
    # Bits 08-09: Range Scale
    range_scale = EnumField(word_index=1, start_bit=8, width=2, enum_cls=RangeScale)
    
    # Bits 10-11: Number of Bars
    bar_scan = EnumField(word_index=1, start_bit=10, width=2, enum_cls=BarScan)
    
    # Bits 12-13: Azimuth Scan Width
    azimuth_scan = EnumField(word_index=1, start_bit=12, width=2, enum_cls=AzimuthScan)
    
    # Bit 14: Velocity Scale
    velocity_scale = EnumField(word_index=1, start_bit=14, width=1, enum_cls=VelocityScale)
    
    # Bit 15: Spare

    # --- Word 03: Radar Functions and Parameters #2 (Ref 7.1.2.3) ---
    
    # Bits 00-03: Spare (Note 1 says Not Applicable, typically 0)
    
    # Bits 04-05: Zoom Command
    zoom_cmd = EnumField(word_index=2, start_bit=4, width=2, enum_cls=ZoomCommand)
    
    # Bits 06-07: SAR Map Orientation
    map_orientation = EnumField(word_index=2, start_bit=6, width=2, enum_cls=MapOrientation)
    
    # Bits 08-15: Spare