import ctypes
from Grifo_E_1553lib.data_types.dl_target_validity import DlTargetValidity
from Grifo_E_1553lib.data_types.semicircle import Semicircle
from Grifo_E_1553lib.data_types.target_presentation_data import TargetPresentationData
from Grifo_E_1553lib.data_types.target_half_call_sign import TargetHalfCallSign
from Grifo_E_1553lib.data_types.target_position import TargetPosition

class Msg2DataLinkTarget(ctypes.Structure):
    _fields_ = [
        ("tgt_06_pos_x", TargetPosition),
        ("tgt_06_pos_y", TargetPosition),
        ("tgt_06_ground_track_angle", Semicircle),
        ("tgt_06_presentation_data", TargetPresentationData),
        ("tgt_06_call_sign", TargetHalfCallSign * 2),
        ("tgt_07_pos_x", TargetPosition),
        ("tgt_07_pos_y", TargetPosition),
        ("tgt_07_ground_track_angle", Semicircle),
        ("tgt_07_presentation_data", TargetPresentationData),
        ("tgt_07_call_sign", TargetHalfCallSign * 2),
        ("tgt_08_pos_x", TargetPosition),
        ("tgt_08_pos_y", TargetPosition),
        ("tgt_08_ground_track_angle", Semicircle),
        ("tgt_08_presentation_data", TargetPresentationData),
        ("tgt_08_call_sign", TargetHalfCallSign * 2),
        ("tgt_09_pos_x", TargetPosition),
        ("tgt_09_pos_y", TargetPosition),
        ("tgt_09_ground_track_angle", Semicircle),
        ("tgt_09_presentation_data", TargetPresentationData),
        ("tgt_09_call_sign", TargetHalfCallSign * 2),
        ("tgt_10_pos_x", TargetPosition),
        ("tgt_10_pos_y", TargetPosition),
        ("tgt_10_ground_track_angle", Semicircle),
        ("tgt_10_presentation_data", TargetPresentationData),
        ("tgt_10_call_sign", TargetHalfCallSign * 2),
    ]

    def __init__(self):
        super().__init__()
        self.tgt_06_pos_x = TargetPosition()
        self.tgt_06_pos_y = TargetPosition()
        self.tgt_06_ground_track_angle = Semicircle()
        self.tgt_06_presentation_data = TargetPresentationData()
        self.tgt_06_call_sign = (TargetHalfCallSign * 2)()

    def set_target(self, idx, pos_x, pos_y, ground_track_angle, presentation_raw=None, call_sign_chars=None):
        """Populate target by index 6..10 with numeric values.

                - `idx`: 6..10
                - `ground_track_angle`: float (radians) or integer raw. `Semicircle.set` accepts
                    radians (float) and will convert to raw; passing an `int` stores the raw word.
        """
        if idx < 6 or idx > 10:
            raise ValueError("idx must be 6..10")
        local_idx = idx - 5
        setattr(self, f"tgt_{idx:02d}_pos_x", TargetPosition())
        getattr(self, f"tgt_{idx:02d}_pos_x").set(pos_x)
        setattr(self, f"tgt_{idx:02d}_pos_y", TargetPosition())
        getattr(self, f"tgt_{idx:02d}_pos_y").set(pos_y)
        setattr(self, f"tgt_{idx:02d}_ground_track_angle", Semicircle())
        getattr(self, f"tgt_{idx:02d}_ground_track_angle").set(ground_track_angle)
        if presentation_raw is not None:
            setattr(self, f"tgt_{idx:02d}_presentation_data", TargetPresentationData())
            getattr(self, f"tgt_{idx:02d}_presentation_data").raw = presentation_raw
        if call_sign_chars is not None:
            arr = (TargetHalfCallSign * 2)()
            arr[0].raw = (call_sign_chars[0] << 8) | (call_sign_chars[1] & 0xFF)
            arr[1].raw = 0
            setattr(self, f"tgt_{idx:02d}_call_sign", arr)

    def get_target_raw(self, idx):
        if idx < 6 or idx > 10:
            raise ValueError("idx must be 6..10")
        px = getattr(self, f"tgt_{idx:02d}_pos_x").raw
        py = getattr(self, f"tgt_{idx:02d}_pos_y").raw
        ang = getattr(self, f"tgt_{idx:02d}_ground_track_angle").raw
        pres = getattr(self, f"tgt_{idx:02d}_presentation_data").raw
        call0 = getattr(self, f"tgt_{idx:02d}_call_sign")[0].raw
        call1 = getattr(self, f"tgt_{idx:02d}_call_sign")[1].raw
        return (px, py, ang, pres, call0, call1)
        self.tgt_07_pos_x = TargetPosition()
        self.tgt_07_pos_y = TargetPosition()
        self.tgt_07_ground_track_angle = Semicircle()
        self.tgt_07_presentation_data = TargetPresentationData()
        self.tgt_07_call_sign = (TargetHalfCallSign * 2)()
        self.tgt_08_pos_x = TargetPosition()
        self.tgt_08_pos_y = TargetPosition()
        self.tgt_08_ground_track_angle = Semicircle()
        self.tgt_08_presentation_data = TargetPresentationData()
        self.tgt_08_call_sign = (TargetHalfCallSign * 2)()
        self.tgt_09_pos_x = TargetPosition()
        self.tgt_09_pos_y = TargetPosition()
        self.tgt_09_ground_track_angle = Semicircle()
        self.tgt_09_presentation_data = TargetPresentationData()
        self.tgt_09_call_sign = (TargetHalfCallSign * 2)()
        self.tgt_10_pos_x = TargetPosition()
        self.tgt_10_pos_y = TargetPosition()
        self.tgt_10_ground_track_angle = Semicircle()
        self.tgt_10_presentation_data = TargetPresentationData()
        self.tgt_10_call_sign = (TargetHalfCallSign * 2)()