from ..message_base import MessageBase
from ..constants import Subaddress, TargetHistory, AltitudeBlock, FrequencyAgility
from ..fields import BitField, EnumField, ScaledField

class MsgA1(MessageBase):
    """
    Message A1: Radar Operational Setting and Parameter Transfer
    
    ID: A1
    Direction: BC -> RT (Receive)
    Subaddress: 01
    Rate: 6.25 Hz
    Document Ref: 7.1.1
    """
    SUBADDRESS = Subaddress.RX_SETTINGS
    IS_TRANSMIT = False
    RATE_HZ = 6.25
    WORD_COUNT = 10  # ICD Spec: 10 Words

    # --- Word 01: Radar Setting (Ref 7.1.1.1) ---
    target_history = EnumField(word_index=0, start_bit=0, width=2, enum_cls=TargetHistory)

    # Symbology Intensity: 0-127 (7 bits)
    symbol_intensity = BitField(word_index=0, start_bit=2, width=7)

    # Ground Target Rejected Radial Velocity: 0=Low, 1=High
    ground_target_rej_vel_high = BitField(word_index=0, start_bit=9, width=1)

    # Min Detectable Ground Target Radial Velocity: 0=Low, 1=High
    min_detect_ground_vel_high = BitField(word_index=0, start_bit=10, width=1)

    # ALE Blanking: 0=Enabled, 1=Disabled
    ale_blanking_disabled = BitField(word_index=0, start_bit=11, width=1)

    altitude_block = EnumField(word_index=0, start_bit=12, width=2, enum_cls=AltitudeBlock)

    # Look-Up Selection: 0=LPRF, 1=MPRF
    mprf_lookup_selected = BitField(word_index=0, start_bit=14, width=1)

    # Bit 15 is Spare

    # --- Word 02: Frequency Agility (Ref 7.1.1.2) ---
    freq_agility_mode = EnumField(word_index=1, start_bit=0, width=2, enum_cls=FrequencyAgility)

    # Frequency Groups (0=Not Active, 1=Active)
    freq_group_1_active = BitField(word_index=1, start_bit=2, width=1)
    freq_group_2_active = BitField(word_index=1, start_bit=3, width=1)
    freq_group_3_active = BitField(word_index=1, start_bit=4, width=1)
    freq_group_4_active = BitField(word_index=1, start_bit=5, width=1)
    freq_group_5_active = BitField(word_index=1, start_bit=6, width=1)

    # Frequency Channel: 1-30
    freq_channel = BitField(word_index=1, start_bit=7, width=6)

    # Waveform Interleave: 0=Spare, 1=Int1, 2=Int2, 3=Int3
    waveform_interleave = BitField(word_index=1, start_bit=13, width=2)

    # LPRF Threshold: 0=Low, 1=High
    lprf_threshold_high = BitField(word_index=1, start_bit=15, width=1)

    # --- Word 03: Beacon Delay and Code (Ref 7.1.1.3) ---
    # Delay: LSB = 0.01 us, Range 0.00 - 40.95 us
    beacon_delay = ScaledField(word_index=2, start_bit=0, width=12, lsb_value=0.01)

    # Code: 0-15
    beacon_code = BitField(word_index=2, start_bit=12, width=4)

    # --- Word 04: Radar Gains (Ref 7.1.1.4) ---
    # IF (Map) Gain: 0-127
    if_gain = BitField(word_index=3, start_bit=0, width=7)

    # Moving Target Gain: 0-127
    moving_target_gain = BitField(word_index=3, start_bit=7, width=7)

    # Frequency Grouping Option: 0=Opt1, 1=Opt2
    freq_grouping_opt2 = BitField(word_index=3, start_bit=14, width=1)

    # Bit 15 is Spare

    # --- Word 05: A/C Identifier (Ref 7.1.1.5) ---
    ac_identifier = BitField(word_index=4, start_bit=0, width=16)

    # --- Word 06: Date of Mission (Ref 7.1.1.6) ---
    mission_year = BitField(word_index=5, start_bit=0, width=6) # 0-63 (2000-2063)
    mission_month = BitField(word_index=5, start_bit=6, width=4) # 1-12
    mission_day = BitField(word_index=5, start_bit=10, width=5) # 1-31
    # Bit 15 is Spare

    # --- Word 07: Time of Mission (Ref 7.1.1.7) ---
    # Time of day in seconds from midnight. LSB = 2 sec. Max 86398
    mission_time = ScaledField(word_index=6, start_bit=0, width=15, lsb_value=2.0)
    # Bit 15 is Spare

    # --- Word 08: Parameter Identifier (Ref 7.1.1.8) ---
    # 0=Not Active, 1=Active
    param_transfer_active = BitField(word_index=7, start_bit=0, width=1)

    # 0=Receive, 1=Transmit
    param_tx_selected = BitField(word_index=7, start_bit=1, width=1)

    # Parameter ID
    param_id = BitField(word_index=7, start_bit=2, width=8)

    # Bits 10-15 are Spare

    # --- Word 09 & 10: Parameter Value (Ref 7.1.1.9) ---
    param_value_word1 = BitField(word_index=8, start_bit=0, width=16)
    param_value_word2 = BitField(word_index=9, start_bit=0, width=16)