from ..message_base import MessageBase
from ..constants import Subaddress, BitReportLabel
from ..fields import BitField, EnumField, ScaledField

class MsgB8(MessageBase):
    """
    Message B8: BIT Report Message
    
    ID: B8
    Direction: RT -> BC (Transmit)
    Subaddress: 18
    Rate: 1.5625 Hz
    Document Ref: 7.2.8
    
    Contains detailed Built-In Test results, failure locations, 
    and degradation flags.
    """
    SUBADDRESS = Subaddress.TX_BIT_REPORT
    IS_TRANSMIT = True

    # --- Word 01: BIT Report Label (Ref 7.2.8.1) ---
    report_label = EnumField(word_index=0, start_bit=0, width=3, enum_cls=BitReportLabel)

    # --- Word 02: BIT Report Date (Ref 7.2.8.2) ---
    # Format matches A1/06
    report_year = BitField(word_index=1, start_bit=0, width=6)
    report_month = BitField(word_index=1, start_bit=6, width=4)
    report_day = BitField(word_index=1, start_bit=10, width=5)

    # --- Word 03: BIT Report Time Tag (Ref 7.2.8.3) ---
    # Format matches A1/07. LSB = 2 sec
    report_time = ScaledField(word_index=2, start_bit=0, width=15, lsb_value=2.0)

    # --- Word 04: Degradation Conditions 1 (Ref 7.2.8.4) ---
    deg_msg_01 = BitField(word_index=3, start_bit=0, width=1)
    deg_msg_02 = BitField(word_index=3, start_bit=1, width=1)
    deg_msg_03 = BitField(word_index=3, start_bit=2, width=1)
    # ... mapping all 16 bits as individual flags
    deg_msg_16 = BitField(word_index=3, start_bit=15, width=1)

    # --- Word 05: Degradation Conditions 2 (Ref 7.2.8.5) ---
    # Currently spares/reserved in standard mapping, but placeholder structure
    deg_word_2_raw = BitField(word_index=4, start_bit=0, width=16)

    # --- Word 06: Failure Location - Pedestal (Ref 7.2.8.6) ---
    fail_sru_gimbal = BitField(word_index=5, start_bit=0, width=1)
    fail_sru_waveguide = BitField(word_index=5, start_bit=1, width=1)
    # ... (other bits are spares or specific waveguides)

    # --- Word 07: Failure Location - Servoloop (Ref 7.2.8.7) ---
    fail_sru_chassis = BitField(word_index=6, start_bit=0, width=1)
    fail_sru_pwr_supply = BitField(word_index=6, start_bit=1, width=1)
    fail_sru_dig_ctrl = BitField(word_index=6, start_bit=2, width=1)

    # --- Word 08: Failure Location - RX Front End (Ref 7.2.8.8) ---
    fail_rxfe_chassis = BitField(word_index=7, start_bit=0, width=1)
    fail_rxfe_lna = BitField(word_index=7, start_bit=1, width=1)
    fail_rxfe_sum_prot = BitField(word_index=7, start_bit=2, width=1)
    # ...

    # --- Word 09: Failure Location - Receiver (Ref 7.2.8.9) ---
    fail_rx_chassis = BitField(word_index=8, start_bit=0, width=1)
    fail_rx_uhf_assy = BitField(word_index=8, start_bit=1, width=1)
    fail_rx_synth = BitField(word_index=8, start_bit=2, width=1)
    # ...

    # --- Word 10: Failure Location - Transmitter (Ref 7.2.8.10) ---
    fail_tx_chassis = BitField(word_index=9, start_bit=0, width=1)
    fail_tx_rex = BitField(word_index=9, start_bit=1, width=1)
    fail_tx_pwr_supply = BitField(word_index=9, start_bit=2, width=1)
    fail_tx_twt = BitField(word_index=9, start_bit=3, width=1)
    # ...

    # --- Word 11: Failure Location - Processor (Ref 7.2.8.11) ---
    fail_proc_motherboard = BitField(word_index=10, start_bit=0, width=1)
    fail_proc_pulse_comp = BitField(word_index=10, start_bit=1, width=1)
    fail_proc_mti_fft = BitField(word_index=10, start_bit=2, width=1)
    # ...

    # --- Word 12: Signal Processor Test Results (Ref 7.2.8.12) ---
    test_sp1_timer1 = BitField(word_index=11, start_bit=0, width=1)
    test_sp2_timer_timing = BitField(word_index=11, start_bit=1, width=1)
    # ... mapping specific tests ...
    test_sp16_bcn = BitField(word_index=11, start_bit=15, width=1)

    # --- Word 13-22: Other Test Results ---
    # Providing raw access for detailed analysis if needed
    test_res_rx_module = BitField(word_index=12, start_bit=0, width=16)
    test_res_data_proc = BitField(word_index=13, start_bit=0, width=16)
    test_res_post_proc = BitField(word_index=14, start_bit=0, width=16)
    test_res_agc = BitField(word_index=15, start_bit=0, width=16)
    test_res_pwr_supply = BitField(word_index=16, start_bit=0, width=16)
    test_res_servoloop = BitField(word_index=17, start_bit=0, width=16)
    test_res_tx_1 = BitField(word_index=18, start_bit=0, width=16)
    test_res_tx_2 = BitField(word_index=19, start_bit=0, width=16)
    test_res_rx_fe = BitField(word_index=20, start_bit=0, width=16)
    test_res_integrated = BitField(word_index=21, start_bit=0, width=16)