from ..message_base import MessageBase
from ..constants import Subaddress, TrackAccuracy, NormFactorXY, NormFactorZ
from ..fields import BitField, EnumField, ScaledField

class MsgB4(MessageBase):
    """
    Message B4: SPT Target Message
    
    ID: B4
    Direction: RT -> BC (Transmit)
    Subaddress: 14
    Rate: 50 Hz
    Document Ref: 7.2.4
    
    Contains detailed tracking data for the Secondary Priority Target (SPT).
    """
    SUBADDRESS = Subaddress.TX_SPT
    IS_TRANSMIT = True

    # --- Word 01: Track Validity and Status Word #1 (Ref 7.2.4.1) ---
    range_valid = BitField(word_index=0, start_bit=0, width=1)
    pos_xyz_valid = BitField(word_index=0, start_bit=1, width=1)
    range_rate_valid = BitField(word_index=0, start_bit=2, width=1)
    velocity_valid = BitField(word_index=0, start_bit=3, width=1)
    accel_valid = BitField(word_index=0, start_bit=4, width=1)
    aspect_angle_valid = BitField(word_index=0, start_bit=5, width=1)
    cas_mach_valid = BitField(word_index=0, start_bit=6, width=1)
    # Bit 7 Spare
    display_coords_valid = BitField(word_index=0, start_bit=8, width=1)
    
    pos_accuracy = EnumField(word_index=0, start_bit=9, width=2, enum_cls=TrackAccuracy)
    vel_accuracy = EnumField(word_index=0, start_bit=11, width=2, enum_cls=TrackAccuracy)
    acc_accuracy = EnumField(word_index=0, start_bit=13, width=2, enum_cls=TrackAccuracy)
    # Bit 15 Spare

    # --- Word 02: Track Validity and Status Word #2 (Ref 7.2.4.2) ---
    reacquisition = BitField(word_index=1, start_bit=0, width=1)
    target_coast = BitField(word_index=1, start_bit=1, width=1)
    hoj_active = BitField(word_index=1, start_bit=2, width=1)
    aoj_active = BitField(word_index=1, start_bit=3, width=1)
    
    norm_factor_x = EnumField(word_index=1, start_bit=4, width=3, enum_cls=NormFactorXY)
    norm_factor_y = EnumField(word_index=1, start_bit=7, width=3, enum_cls=NormFactorXY)
    norm_factor_z = EnumField(word_index=1, start_bit=10, width=2, enum_cls=NormFactorZ)
    # Bits 12-15 Spare

    # --- Word 03: Time Tag (Ref 7.2.4.3) ---
    # LSB 64 us
    time_tag = ScaledField(word_index=2, start_bit=0, width=16, lsb_value=64.0)

    # --- Word 04: Target Range (Ref 7.2.4.4) ---
    # LSB 8 ft
    target_range = ScaledField(word_index=3, start_bit=0, width=16, lsb_value=8.0)

    # --- Word 05: Normalized Target Position X (Ref 7.2.4.5) ---
    # 2's complement. Actual value = Raw * Multiplier (from Word 02)
    # We treat it as raw signed integer here.
    norm_pos_x = ScaledField(word_index=4, start_bit=0, width=16, lsb_value=1.0, signed=True)

    # --- Word 06: Normalized Target Position Y (Ref 7.2.4.6) ---
    norm_pos_y = ScaledField(word_index=5, start_bit=0, width=16, lsb_value=1.0, signed=True)

    # --- Word 07: Normalized Target Position Z (Ref 7.2.4.7) ---
    norm_pos_z = ScaledField(word_index=6, start_bit=0, width=16, lsb_value=1.0, signed=True)

    # --- Word 08: Target Range Rate (Ref 7.2.4.8) ---
    # LSB 1.0 ft/s, 2's comp
    range_rate = ScaledField(word_index=7, start_bit=0, width=16, lsb_value=1.0, signed=True)

    # --- Word 09: Target Velocity Magnitude (Ref 7.2.4.9) ---
    # LSB 0.0625 ft/s
    vel_magnitude = ScaledField(word_index=8, start_bit=0, width=16, lsb_value=0.0625)

    # --- Word 10-12: Target Velocity X, Y, Z (Ref 7.2.4.10 - 12) ---
    # LSB 0.125 ft/s, 2's comp
    vel_x = ScaledField(word_index=9, start_bit=0, width=16, lsb_value=0.125, signed=True)
    vel_y = ScaledField(word_index=10, start_bit=0, width=16, lsb_value=0.125, signed=True)
    vel_z = ScaledField(word_index=11, start_bit=0, width=16, lsb_value=0.125, signed=True)

    # --- Word 13: Target Acceleration Magnitude (Ref 7.2.4.13) ---
    # LSB 0.015625 ft/s^2
    acc_magnitude = ScaledField(word_index=12, start_bit=0, width=16, lsb_value=0.015625)

    # --- Word 14-16: Target Acceleration X, Y, Z (Ref 7.2.4.14 - 16) ---
    # LSB 0.03125 ft/s^2, 2's comp
    acc_x = ScaledField(word_index=13, start_bit=0, width=16, lsb_value=0.03125, signed=True)
    acc_y = ScaledField(word_index=14, start_bit=0, width=16, lsb_value=0.03125, signed=True)
    acc_z = ScaledField(word_index=15, start_bit=0, width=16, lsb_value=0.03125, signed=True)

    # --- Word 17: Target Aspect Angle (Ref 7.2.4.17) ---
    # Semicircles, 2's comp
    aspect_angle = ScaledField(word_index=16, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)

    # --- Word 18: Target CAS (Ref 7.2.4.18) ---
    # LSB 3.0518e-2 knots
    target_cas = ScaledField(word_index=17, start_bit=0, width=16, lsb_value=3.0518e-2)

    # --- Word 19: Target Mach Number (Ref 7.2.4.19) ---
    # LSB 2.4414e-4 Mach
    target_mach = ScaledField(word_index=18, start_bit=0, width=16, lsb_value=2.4414e-4)

    # --- Word 20: Target X Display Coordinate (Ref 7.2.4.20) ---
    # Pixels (0-1023)
    display_x = BitField(word_index=19, start_bit=0, width=10)

    # --- Word 21: Target Y Display Coordinate (Ref 7.2.4.21) ---
    # Pixels (0-1023)
    display_y = BitField(word_index=20, start_bit=0, width=10)

    # --- Word 22-24: Standard Deviation Position X, Y, Z (Ref 7.2.4.22 - 24) ---
    # LSB 0.42725 ft
    std_dev_x = ScaledField(word_index=21, start_bit=0, width=16, lsb_value=0.42725)
    std_dev_y = ScaledField(word_index=22, start_bit=0, width=16, lsb_value=0.42725)
    std_dev_z = ScaledField(word_index=23, start_bit=0, width=16, lsb_value=0.42725)