import ctypes
from Grifo_E_1553lib.data_types.relative_timetag import RelativeTimetag
from Grifo_E_1553lib.data_types.semicircle import Semicircle
from Grifo_E_1553lib.data_types.acceleration import Acceleration
from Grifo_E_1553lib.data_types.velocity import Velocity
from Grifo_E_1553lib.data_types.attitude_rate import AttitudeRate
from Grifo_E_1553lib.data_types.ang_acc import AngAcc
from Grifo_E_1553lib.data_types.enums import TwsTargetValidity, TwsTargetWhich, TrackPriority, TargetAccurancy

from Grifo_E_1553lib.data_types.valitidy_word_and_hpt_identifier import ValitidyWordAndHptIdentifier
from Grifo_E_1553lib.data_types.track_priority_01_04 import TrackPriority0104
from Grifo_E_1553lib.data_types.track_priority_05_08 import TrackPriority0508
from Grifo_E_1553lib.data_types.track_priority_09_10 import TrackPriority0910
from Grifo_E_1553lib.data_types.track_accurancy_01_04 import TrackAccurancy0104
from Grifo_E_1553lib.data_types.track_accurancy_05_08 import TrackAccurancy0508
from Grifo_E_1553lib.data_types.track_id_of_tws_tracked_target_01_02 import TrackIdOfTwsTrackedTarget0102
from Grifo_E_1553lib.data_types.track_id_of_tws_tracked_target_03_04 import TrackIdOfTwsTrackedTarget0304
from Grifo_E_1553lib.data_types.track_id_of_tws_tracked_target_05_06 import TrackIdOfTwsTrackedTarget0506
from Grifo_E_1553lib.data_types.track_id_of_tws_tracked_target_07_08 import TrackIdOfTwsTrackedTarget0708
from Grifo_E_1553lib.data_types.x_display_coord_of_the_hpt_target import XDisplayCoordOfTheHptTarget
from Grifo_E_1553lib.data_types.y_display_coord_of_the_hpt_target import YDisplayCoordOfTheHptTarget
from Grifo_E_1553lib.data_types.target_velocity_magnitude import TargetVelocityMagnitude
from Grifo_E_1553lib.data_types.tws_velocity import TwsVelocity

class TwsStatusAndTargets0102(ctypes.Structure):
    _fields_ = [
        ("valitidy_word_and_hpt_identifier", ValitidyWordAndHptIdentifier),
        ("track_priority_01_04", TrackPriority0104),
        ("track_priority_05_08", TrackPriority0508),
        ("track_accurancy_01_04", TrackAccurancy0104),
        ("track_accurancy_05_08", TrackAccurancy0508),
        ("track_id_of_tws_tracked_target_01_02", TrackIdOfTwsTrackedTarget0102),
        ("track_id_of_tws_tracked_target_03_04", TrackIdOfTwsTrackedTarget0304),
        ("track_id_of_tws_tracked_target_05_06", TrackIdOfTwsTrackedTarget0506),
        ("track_id_of_tws_tracked_target_07_08", TrackIdOfTwsTrackedTarget0708),
        ("x_display_coord_of_the_hpt_target", XDisplayCoordOfTheHptTarget),
        ("y_display_coord_of_the_hpt_target", YDisplayCoordOfTheHptTarget),
        ("time_tag_of_tracked_target_01_data", RelativeTimetag),
        ("range_of_tracked_01_target", TargetVelocityMagnitude),
        ("target_01_position_x", TwsVelocity),
        ("target_01_position_y", TwsVelocity),
        ("target_01_position_z", TwsVelocity),
        ("target_01_velocity_magnitude", TargetVelocityMagnitude),
        ("target_01_velocity_x", TwsVelocity),
        ("target_01_velocity_y", TwsVelocity),
        ("target_01_velocity_z", TwsVelocity),
        ("time_tag_of_tracked_target_02_data", RelativeTimetag),
        ("range_of_tracked_02_target", TargetVelocityMagnitude),
        ("target_02_position_x", TwsVelocity),
        ("target_02_position_y", TwsVelocity),
        ("target_02_position_z", TwsVelocity),
        ("target_02_velocity_magnitude", TargetVelocityMagnitude),
        ("target_02_velocity_x", TwsVelocity),
        ("target_02_velocity_y", TwsVelocity),
        ("target_02_velocity_z", TwsVelocity)
    ]


def _clone_fields_with_reindex(fields, src1='01', src2='02', dst1='03', dst2='04'):
    """Utility: clone _fields_ list replacing numeric suffixes in names.

    This is a pragmatic helper to produce identical ctypes Structures
    for different TWS page pairs (03/04, 05/06, ...). It performs a
    textual replace on known suffixes '01'->dst1 and '02'->dst2 where
    present in the field names.
    """
    new = []
    for name, typ in fields:
        new_name = name.replace(f"_{src1}_", f"_{dst1}_").replace(f"_{src2}_", f"_{dst2}_")
        # Also handle patterns like 'target_01_position_x' -> 'target_03_position_x'
        new_name = new_name.replace(src1, dst1, 1) if src1 in new_name and dst1 not in new_name else new_name
        new_name = new_name.replace(src2, dst2, 1) if src2 in new_name and dst2 not in new_name else new_name
        new.append((new_name, typ))
    return new


# Create cloned structures for subsequent B-pages (03/04, 05/06, 07/08, 09/10)
class TwsStatusAndTargets0304(ctypes.Structure):
    _fields_ = _clone_fields_with_reindex(TwsStatusAndTargets0102._fields_, '01', '02', '03', '04')


class TwsStatusAndTargets0506(ctypes.Structure):
    _fields_ = _clone_fields_with_reindex(TwsStatusAndTargets0102._fields_, '01', '02', '05', '06')


class TwsStatusAndTargets0708(ctypes.Structure):
    _fields_ = _clone_fields_with_reindex(TwsStatusAndTargets0102._fields_, '01', '02', '07', '08')


class TwsStatusAndTargets0910(ctypes.Structure):
    _fields_ = _clone_fields_with_reindex(TwsStatusAndTargets0102._fields_, '01', '02', '09', '10')


# Additional page pairs for higher-numbered B-pages (11/12 .. 31/32)
_pairs = [
    ('11', '12'), ('13', '14'), ('15', '16'), ('17', '18'), ('19', '20'),
    ('21', '22'), ('23', '24'), ('25', '26'), ('27', '28'), ('29', '30'),
    ('31', '32')
]

for a, b in _pairs:
    cls_name = f"TwsStatusAndTargets{a}{b}"
    globals()[cls_name] = type(cls_name, (ctypes.Structure,), {
        '_fields_': _clone_fields_with_reindex(TwsStatusAndTargets0102._fields_, '01', '02', a, b)
    })