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

class MsgA7(MessageBase):
    """
    Message A7: Data Link Targets (Message #1)
    
    ID: A7
    Direction: BC -> RT (Receive)
    Subaddress: 07
    Rate: 6.25 Hz
    Document Ref: 7.1.7
    
    Contains:
    - Validity flags for Targets 01-10
    - Data for Targets 01-05
    """
    SUBADDRESS = Subaddress.RX_DATALINK_1
    IS_TRANSMIT = False
    RATE_HZ = 6.25

    # --- Word 01: DL Targets Validity (Ref 7.1.7.1) ---
    validity_tgt_01 = BitField(word_index=0, start_bit=0, width=1)
    validity_tgt_02 = BitField(word_index=0, start_bit=1, width=1)
    validity_tgt_03 = BitField(word_index=0, start_bit=2, width=1)
    validity_tgt_04 = BitField(word_index=0, start_bit=3, width=1)
    validity_tgt_05 = BitField(word_index=0, start_bit=4, width=1)
    validity_tgt_06 = BitField(word_index=0, start_bit=5, width=1)
    validity_tgt_07 = BitField(word_index=0, start_bit=6, width=1)
    validity_tgt_08 = BitField(word_index=0, start_bit=7, width=1)
    validity_tgt_09 = BitField(word_index=0, start_bit=8, width=1)
    validity_tgt_10 = BitField(word_index=0, start_bit=9, width=1)
    # Bits 10-15 Spare

    # ========================== TARGET 01 ==========================
    # Pos X: 16-bit, 2's comp, LSB=16 ft
    tgt_01_pos_x = ScaledField(word_index=1, start_bit=0, width=16, lsb_value=16.0, signed=True)
    # Pos Y: 16-bit, 2's comp, LSB=16 ft
    tgt_01_pos_y = ScaledField(word_index=2, start_bit=0, width=16, lsb_value=16.0, signed=True)
    # Track Angle: Semicircles, 2's comp
    tgt_01_track_angle = ScaledField(word_index=3, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)
    
    # Presentation Data
    tgt_01_type = EnumField(word_index=4, start_bit=0, width=2, enum_cls=DLTargetType)
    tgt_01_velocity = ScaledField(word_index=4, start_bit=2, width=3, lsb_value=143.0) # Knots
    tgt_01_altitude = ScaledField(word_index=4, start_bit=5, width=7, lsb_value=1000.0) # Feet (LSB 1 kft)
    tgt_01_intercept_zone = BitField(word_index=4, start_bit=12, width=1)
    
    # Call Sign (4 Chars)
    tgt_01_callsign_ab = ASCIIField(word_index=5)
    tgt_01_callsign_cd = ASCIIField(word_index=6)

    # ========================== TARGET 02 ==========================
    tgt_02_pos_x = ScaledField(word_index=7, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_02_pos_y = ScaledField(word_index=8, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_02_track_angle = ScaledField(word_index=9, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)
    
    tgt_02_type = EnumField(word_index=10, start_bit=0, width=2, enum_cls=DLTargetType)
    tgt_02_velocity = ScaledField(word_index=10, start_bit=2, width=3, lsb_value=143.0)
    tgt_02_altitude = ScaledField(word_index=10, start_bit=5, width=7, lsb_value=1000.0)
    tgt_02_intercept_zone = BitField(word_index=10, start_bit=12, width=1)
    
    tgt_02_callsign_ab = ASCIIField(word_index=11)
    tgt_02_callsign_cd = ASCIIField(word_index=12)

    # ========================== TARGET 03 ==========================
    tgt_03_pos_x = ScaledField(word_index=13, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_03_pos_y = ScaledField(word_index=14, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_03_track_angle = ScaledField(word_index=15, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)
    
    tgt_03_type = EnumField(word_index=16, start_bit=0, width=2, enum_cls=DLTargetType)
    tgt_03_velocity = ScaledField(word_index=16, start_bit=2, width=3, lsb_value=143.0)
    tgt_03_altitude = ScaledField(word_index=16, start_bit=5, width=7, lsb_value=1000.0)
    tgt_03_intercept_zone = BitField(word_index=16, start_bit=12, width=1)
    
    tgt_03_callsign_ab = ASCIIField(word_index=17)
    tgt_03_callsign_cd = ASCIIField(word_index=18)

    # ========================== TARGET 04 ==========================
    tgt_04_pos_x = ScaledField(word_index=19, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_04_pos_y = ScaledField(word_index=20, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_04_track_angle = ScaledField(word_index=21, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)
    
    tgt_04_type = EnumField(word_index=22, start_bit=0, width=2, enum_cls=DLTargetType)
    tgt_04_velocity = ScaledField(word_index=22, start_bit=2, width=3, lsb_value=143.0)
    tgt_04_altitude = ScaledField(word_index=22, start_bit=5, width=7, lsb_value=1000.0)
    tgt_04_intercept_zone = BitField(word_index=22, start_bit=12, width=1)
    
    tgt_04_callsign_ab = ASCIIField(word_index=23)
    tgt_04_callsign_cd = ASCIIField(word_index=24)

    # ========================== TARGET 05 ==========================
    tgt_05_pos_x = ScaledField(word_index=25, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_05_pos_y = ScaledField(word_index=26, start_bit=0, width=16, lsb_value=16.0, signed=True)
    tgt_05_track_angle = ScaledField(word_index=27, start_bit=0, width=16, lsb_value=3.05176e-5, signed=True)
    
    tgt_05_type = EnumField(word_index=28, start_bit=0, width=2, enum_cls=DLTargetType)
    tgt_05_velocity = ScaledField(word_index=28, start_bit=2, width=3, lsb_value=143.0)
    tgt_05_altitude = ScaledField(word_index=28, start_bit=5, width=7, lsb_value=1000.0)
    tgt_05_intercept_zone = BitField(word_index=28, start_bit=12, width=1)
    
    tgt_05_callsign_ab = ASCIIField(word_index=29)
    tgt_05_callsign_cd = ASCIIField(word_index=30)