from ..message_base import MessageBase
from ..constants import (
    Subaddress, TargetHistory, AltitudeBlock, FrequencyAgility,
    CursorMode, CursorSnowplough, SAREnable, RadarMode
)
from ..fields import BitField, EnumField, ScaledField

class MsgB6(MessageBase):
    """
    Message B6: Radar Operational Setting and Parameter Transfer Tell-Back
    
    ID: B6
    Direction: RT -> BC (Transmit)
    Subaddress: 16
    Rate: 6.25 Hz
    Document Ref: 7.2.6
    
    Contains:
    - Confirmation of settings (from A1)
    - Radar Health Status / BIT results
    - Software Release Versions
    - Scan Center positions
    - Cursor position feedback
    """
    SUBADDRESS = Subaddress.TX_SETTING_TB
    IS_TRANSMIT = True
    RATE_HZ = 6.25

    # --- Word 01: Radar Setting Tell-Back (Ref 7.2.6.1) ---
    target_history_tb = EnumField(word_index=0, start_bit=0, width=2, enum_cls=TargetHistory)
    symbol_intensity_tb = BitField(word_index=0, start_bit=2, width=7)
    gnd_tgt_rej_vel_tb = BitField(word_index=0, start_bit=9, width=1)
    min_det_gnd_vel_tb = BitField(word_index=0, start_bit=10, width=1)
    ale_blanking_tb = BitField(word_index=0, start_bit=11, width=1)
    altitude_block_tb = EnumField(word_index=0, start_bit=12, width=2, enum_cls=AltitudeBlock)
    mprf_lookup_tb = BitField(word_index=0, start_bit=14, width=1)

    # --- Word 02: Freq Agility & Interleave Tell-Back (Ref 7.2.6.2) ---
    freq_agility_mode_tb = EnumField(word_index=1, start_bit=0, width=2, enum_cls=FrequencyAgility)
    freq_group_1_tb = BitField(word_index=1, start_bit=2, width=1)
    freq_group_2_tb = BitField(word_index=1, start_bit=3, width=1)
    freq_group_3_tb = BitField(word_index=1, start_bit=4, width=1)
    freq_group_4_tb = BitField(word_index=1, start_bit=5, width=1)
    freq_group_5_tb = BitField(word_index=1, start_bit=6, width=1)
    freq_channel_tb = BitField(word_index=1, start_bit=7, width=6)
    waveform_int_tb = BitField(word_index=1, start_bit=13, width=2)
    lprf_thresh_tb = BitField(word_index=1, start_bit=15, width=1)

    # --- Word 03: Beacon Tell-Back (Ref 7.2.6.3) ---
    beacon_delay_tb = ScaledField(word_index=2, start_bit=0, width=12, lsb_value=0.01)
    beacon_code_tb = BitField(word_index=2, start_bit=12, width=4)

    # --- Word 04: Gains Tell-Back (Ref 7.2.6.4) ---
    if_gain_tb = BitField(word_index=3, start_bit=0, width=7)
    mtg_gain_tb = BitField(word_index=3, start_bit=7, width=7)
    freq_grouping_tb = BitField(word_index=3, start_bit=14, width=1)

    # --- Word 05: Radar Health Status (Ref 7.2.6.5) ---
    radar_failed = BitField(word_index=4, start_bit=0, width=1) # 0=OK, 1=Fail
    array_failed = BitField(word_index=4, start_bit=1, width=1)
    pedestal_failed = BitField(word_index=4, start_bit=2, width=1)
    servoloop_failed = BitField(word_index=4, start_bit=3, width=1)
    rx_frontend_failed = BitField(word_index=4, start_bit=4, width=1)
    receiver_failed = BitField(word_index=4, start_bit=5, width=1)
    transmitter_failed = BitField(word_index=4, start_bit=6, width=1)
    processor_failed = BitField(word_index=4, start_bit=7, width=1)
    
    tx_overtemp = BitField(word_index=4, start_bit=8, width=1)
    proc_overtemp = BitField(word_index=4, start_bit=9, width=1)
    servo_overtemp = BitField(word_index=4, start_bit=10, width=1)
    pressurization_fail = BitField(word_index=4, start_bit=11, width=1)
    bit_report_available = BitField(word_index=4, start_bit=12, width=1)

    # --- Word 06: SW Release 1 (Ref 7.2.6.6) ---
    main_sw_release = BitField(word_index=5, start_bit=0, width=4)
    main_sw_version = BitField(word_index=5, start_bit=4, width=4)
    graph_sw_release = BitField(word_index=5, start_bit=8, width=4)
    graph_sw_version = BitField(word_index=5, start_bit=12, width=4)

    # --- Word 07: SW Release 2 (Ref 7.2.6.7) ---
    ant_sw_release = BitField(word_index=6, start_bit=0, width=4)
    ant_sw_version = BitField(word_index=6, start_bit=4, width=4)

    # --- Word 08: AZ Scan Center (Ref 7.2.6.8) ---
    # Semicircles, 2's comp
    az_scan_center = ScaledField(word_index=7, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)

    # --- Word 09: EL Scan Center (Ref 7.2.6.9) ---
    # Semicircles, 2's comp
    el_scan_center = ScaledField(word_index=8, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)

    # --- Word 10: Clearance Plane Distance TB (Ref 7.2.6.10) ---
    clearance_plane_tb = ScaledField(word_index=9, start_bit=0, width=16, lsb_value=1.0)

    # --- Word 11: Terrain Avoidance Data #1 (Ref 7.2.6.11) ---
    # LSB = 4.68426 meters (approx from PDF logic)
    ta_min_range_protrusion_high = ScaledField(word_index=10, start_bit=0, width=12, lsb_value=4.68426)
    ta_no_signal = BitField(word_index=10, start_bit=12, width=1)
    ta_jammer_in_corridor = BitField(word_index=10, start_bit=13, width=1)

    # --- Word 12: Terrain Avoidance Data #2 (Ref 7.2.6.12) ---
    ta_min_range_protrusion_low = ScaledField(word_index=11, start_bit=0, width=12, lsb_value=4.68426)

    # --- Word 13: Parameter ID TB (Ref 7.2.6.13) ---
    param_transfer_active_tb = BitField(word_index=12, start_bit=0, width=1)
    param_tx_selected_tb = BitField(word_index=12, start_bit=1, width=1)
    param_id_tb = BitField(word_index=12, start_bit=2, width=8)

    # --- Word 14-15: Parameter Value TB (Ref 7.2.6.14) ---
    param_val_1_tb = BitField(word_index=13, start_bit=0, width=16)