from ..message_base import MessageBase
from ..constants import Subaddress
from ..fields import ScaledField

class MsgB2(MessageBase):
    """
    Message B2: TWS Targets 3, 4, 5
    
    ID: B2
    Direction: RT -> BC (Transmit)
    Subaddress: 12
    Rate: 6.25 Hz
    Document Ref: 7.2.2
    
    Contains Kinematic data for Targets 03, 04, 05.
    Structure per target is identical to targets in B1.
    """
    SUBADDRESS = Subaddress.TX_TWS_3_4_5
    IS_TRANSMIT = True

    # ========================== TARGET 03 DATA ==========================
    # Words 01-09 (Indices 0-8)
    
    tgt_03_time_tag = ScaledField(word_index=0, start_bit=0, width=16, lsb_value=64.0)
    tgt_03_range = ScaledField(word_index=1, start_bit=0, width=16, lsb_value=8.0)
    
    # Position: LSB 16 ft (X, Y), 2.5 ft (Z)
    tgt_03_pos_x = ScaledField(word_index=2, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_03_pos_y = ScaledField(word_index=3, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_03_pos_z = ScaledField(word_index=4, start_bit=0, width=16, lsb_value=2.5, signed=True)
    
    # Velocity: LSB 0.0625 (Mag), 0.125 (Components)
    tgt_03_vel_mag = ScaledField(word_index=5, start_bit=0, width=16, lsb_value=0.0625)
    tgt_03_vel_x = ScaledField(word_index=6, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_03_vel_y = ScaledField(word_index=7, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_03_vel_z = ScaledField(word_index=8, start_bit=0, width=16, lsb_value=0.125, signed=True)

    # ========================== TARGET 04 DATA ==========================
    # Words 10-18 (Indices 9-17)

    tgt_04_time_tag = ScaledField(word_index=9, start_bit=0, width=16, lsb_value=64.0)
    tgt_04_range = ScaledField(word_index=10, start_bit=0, width=16, lsb_value=8.0)
    
    tgt_04_pos_x = ScaledField(word_index=11, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_04_pos_y = ScaledField(word_index=12, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_04_pos_z = ScaledField(word_index=13, start_bit=0, width=16, lsb_value=2.5, signed=True)
    
    tgt_04_vel_mag = ScaledField(word_index=14, start_bit=0, width=16, lsb_value=0.0625)
    tgt_04_vel_x = ScaledField(word_index=15, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_04_vel_y = ScaledField(word_index=16, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_04_vel_z = ScaledField(word_index=17, start_bit=0, width=16, lsb_value=0.125, signed=True)

    # ========================== TARGET 05 DATA ==========================
    # Words 19-27 (Indices 18-26)

    tgt_05_time_tag = ScaledField(word_index=18, start_bit=0, width=16, lsb_value=64.0)
    tgt_05_range = ScaledField(word_index=19, start_bit=0, width=16, lsb_value=8.0)
    
    tgt_05_pos_x = ScaledField(word_index=20, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_05_pos_y = ScaledField(word_index=21, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_05_pos_z = ScaledField(word_index=22, start_bit=0, width=16, lsb_value=2.5, signed=True)
    
    tgt_05_vel_mag = ScaledField(word_index=23, start_bit=0, width=16, lsb_value=0.0625)
    tgt_05_vel_x = ScaledField(word_index=24, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_05_vel_y = ScaledField(word_index=25, start_bit=0, width=16, lsb_value=0.125, signed=True)
    tgt_05_vel_z = ScaledField(word_index=26, start_bit=0, width=16, lsb_value=0.125, signed=True)