from ..message_base import MessageBase
from ..constants import Subaddress, TwsValidity, HptIdentifier, TrackPriority, TrackAccuracy
from ..fields import BitField, EnumField, ScaledField

class MsgB1(MessageBase):
    """
    Message B1: TWS Status and Targets 1 and 2
    
    ID: B1
    Direction: RT -> BC (Transmit)
    Subaddress: 11
    Rate: 6.25 Hz
    Document Ref: 7.2.1
    
    Contains:
    - Global TWS status (Validity, Priority, IDs for all 8 tracks)
    - Kinematic data for Target 01 and Target 02
    """
    SUBADDRESS = Subaddress.TX_TWS_1_2
    IS_TRANSMIT = True

    # --- Word 01: Validity Word and HPT Identifier (Ref 7.2.1.1) ---
    validity_tgt_01 = EnumField(word_index=0, start_bit=0, width=1, enum_cls=TwsValidity)
    validity_tgt_02 = EnumField(word_index=0, start_bit=1, width=1, enum_cls=TwsValidity)
    validity_tgt_03 = EnumField(word_index=0, start_bit=2, width=1, enum_cls=TwsValidity)
    validity_tgt_04 = EnumField(word_index=0, start_bit=3, width=1, enum_cls=TwsValidity)
    validity_tgt_05 = EnumField(word_index=0, start_bit=4, width=1, enum_cls=TwsValidity)
    validity_tgt_06 = EnumField(word_index=0, start_bit=5, width=1, enum_cls=TwsValidity)
    validity_tgt_07 = EnumField(word_index=0, start_bit=6, width=1, enum_cls=TwsValidity)
    validity_tgt_08 = EnumField(word_index=0, start_bit=7, width=1, enum_cls=TwsValidity)
    
    hpt_identifier = EnumField(word_index=0, start_bit=8, width=4, enum_cls=HptIdentifier)
    # Bits 12-15 Spare

    # --- Word 02: Track Priority Targets 01-04 (Ref 7.2.1.2) ---
    priority_tgt_01 = EnumField(word_index=1, start_bit=0, width=3, enum_cls=TrackPriority)
    priority_tgt_02 = EnumField(word_index=1, start_bit=3, width=3, enum_cls=TrackPriority)
    priority_tgt_03 = EnumField(word_index=1, start_bit=6, width=3, enum_cls=TrackPriority)
    priority_tgt_04 = EnumField(word_index=1, start_bit=9, width=3, enum_cls=TrackPriority)
    # Bits 12-15 Spare

    # --- Word 03: Track Priority Targets 05-08 (Ref 7.2.1.3) ---
    priority_tgt_05 = EnumField(word_index=2, start_bit=0, width=3, enum_cls=TrackPriority)
    priority_tgt_06 = EnumField(word_index=2, start_bit=3, width=3, enum_cls=TrackPriority)
    priority_tgt_07 = EnumField(word_index=2, start_bit=6, width=3, enum_cls=TrackPriority)
    priority_tgt_08 = EnumField(word_index=2, start_bit=9, width=3, enum_cls=TrackPriority)
    # Bits 12-15 Spare

    # --- Word 04: Track Accuracy Targets 01-04 (Ref 7.2.1.4) ---
    # Note: Gaps between fields as per Table B1/04-A/C/E/G
    accuracy_tgt_01 = EnumField(word_index=3, start_bit=0, width=2, enum_cls=TrackAccuracy)
    accuracy_tgt_02 = EnumField(word_index=3, start_bit=4, width=2, enum_cls=TrackAccuracy)
    accuracy_tgt_03 = EnumField(word_index=3, start_bit=8, width=2, enum_cls=TrackAccuracy)
    accuracy_tgt_04 = EnumField(word_index=3, start_bit=12, width=2, enum_cls=TrackAccuracy)

    # --- Word 05: Track Accuracy Targets 05-08 (Ref 7.2.1.5) ---
    accuracy_tgt_05 = EnumField(word_index=4, start_bit=0, width=2, enum_cls=TrackAccuracy)
    accuracy_tgt_06 = EnumField(word_index=4, start_bit=4, width=2, enum_cls=TrackAccuracy)
    accuracy_tgt_07 = EnumField(word_index=4, start_bit=8, width=2, enum_cls=TrackAccuracy)
    accuracy_tgt_08 = EnumField(word_index=4, start_bit=12, width=2, enum_cls=TrackAccuracy)

    # --- Word 06-09: Track IDs (Ref 7.2.1.6 - 7.2.1.9) ---
    track_id_tgt_01 = BitField(word_index=5, start_bit=0, width=8)
    track_id_tgt_02 = BitField(word_index=5, start_bit=8, width=8)
    
    track_id_tgt_03 = BitField(word_index=6, start_bit=0, width=8)
    track_id_tgt_04 = BitField(word_index=6, start_bit=8, width=8)
    
    track_id_tgt_05 = BitField(word_index=7, start_bit=0, width=8)
    track_id_tgt_06 = BitField(word_index=7, start_bit=8, width=8)
    
    track_id_tgt_07 = BitField(word_index=8, start_bit=0, width=8)
    track_id_tgt_08 = BitField(word_index=8, start_bit=8, width=8)

    # --- Word 10-11: HPT Display Coordinates (Ref 7.2.1.10 - 7.2.1.11) ---
    hpt_display_x = BitField(word_index=9, start_bit=0, width=10) # Pixels
    hpt_display_y = BitField(word_index=10, start_bit=0, width=10) # Pixels

    # ========================== TARGET 01 DATA ==========================
    # Ref 7.2.1.12 to 7.2.1.20
    
    # Word 12: Time Tag (LSB 64us)
    tgt_01_time_tag = ScaledField(word_index=11, start_bit=0, width=16, lsb_value=64.0)
    
    # Word 13: Range (LSB 8 ft, Unsigned)
    tgt_01_range = ScaledField(word_index=12, start_bit=0, width=16, lsb_value=8.0)
    
    # Word 14-16: Position (LSB 16 ft, 2's comp)
    tgt_01_pos_x = ScaledField(word_index=13, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_01_pos_y = ScaledField(word_index=14, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_01_pos_z = ScaledField(word_index=15, start_bit=0, width=16, lsb_value=2.5, signed=True) # NOTE: Z LSB is 2.5 ft per 7.2.1.16
    
    # Word 17: Velocity Magnitude (LSB 0.0625 ft/s, Unsigned)
    tgt_01_vel_mag = ScaledField(word_index=16, start_bit=0, width=16, lsb_value=0.0625)
    
    # Word 18-20: Velocity XYZ (LSB 0.125 ft/s, 2's comp)
    tgt_01_vel_x = ScaledField(word_index=17, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_01_vel_y = ScaledField(word_index=18, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_01_vel_z = ScaledField(word_index=19, start_bit=0, width=16, lsb_value=0.125, signed=True)


    # ========================== TARGET 02 DATA ==========================
    
    # Word 21: Time Tag
    tgt_02_time_tag = ScaledField(word_index=20, start_bit=0, width=16, lsb_value=64.0)
    
    # Word 22: Range
    tgt_02_range = ScaledField(word_index=21, start_bit=0, width=16, lsb_value=8.0)
    
    # Word 23-25: Position
    tgt_02_pos_x = ScaledField(word_index=22, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_02_pos_y = ScaledField(word_index=23, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_02_pos_z = ScaledField(word_index=24, start_bit=0, width=16, lsb_value=2.5, signed=True)
    
    # Word 26: Velocity Magnitude
    tgt_02_vel_mag = ScaledField(word_index=25, start_bit=0, width=16, lsb_value=0.0625)
    
    # Word 27-29: Velocity XYZ
    tgt_02_vel_x = ScaledField(word_index=26, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_02_vel_y = ScaledField(word_index=27, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_02_vel_z = ScaledField(word_index=28, start_bit=0, width=16, lsb_value=0.125, signed=True)