#  NOT USED
import unittest
import socket
import os
import sys
import ctypes

# Add the project root to the Python path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

from Grifo_E_1553lib.udp_receiver import UDPReceiver
from Grifo_E_1553lib.udp_parser import UDPParser
from Grifo_E_1553lib.mil1553udp import UDP1553Header
from Grifo_E_1553lib.messages.msg_rdr_settings_and_parameters import MsgRdrSettingsAndParameters
from Grifo_E_1553lib.data_types.enums import TargetHistory

UDP_IP = "127.0.0.1"
UDP_PORT = 61553

class TestUDPCommunicationA1(unittest.TestCase):
    def test_udp_communication_a1(self):
        # Inizializza il ricevitore UDP
        receiver = UDPReceiver(UDP_IP, UDP_PORT)
        parser = UDPParser()

        # Ricevi i dati UDP
        data = receiver.receive()

        # Parsa l'header
        header = parser.parse_header(data)
        self.assertEqual(header.marker1553, 0x1553)
        self.assertEqual(header.otype, 0x4342)

        # Parsa il messaggio
        message_data = data[ctypes.sizeof(UDP1553Header):]
        message = parser.parse_message(message_data, header)

        # Verifica i valori del messaggio A1
        self.assertIsInstance(message, MsgRdrSettingsAndParameters)
        self.assertEqual(message.settings.get_history_level().value, TargetHistory.TARGET_HISTORY_LEVEL_03)

        # Chiudi il ricevitore
        receiver.close()
        
if __name__ == '__main__':
    unittest.main()