# 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.messages.msg_rdr_operation_command import MsgRdrOperationCommand
from Grifo_E_1553lib.data_types.enums import TargetHistory, RdrModes

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

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

        try:
            while True:  # Loop infinito per ricevere messaggi
                # Ricevi i dati UDP
                data = receiver.receive()
                
                print(f"Lunghezza dei dati ricevuti: {len(data)}")
                print(f"Dati ricevuti: {data}")

                # Parsa l'header
                header = parser.parse_header(data)
                self.assertEqual(header.marker1553, 0x1553)
                self.assertEqual(header.otype, 0x4342)
                
                #print(f"Dimensione di UDP1553Header: {ctypes.sizeof(UDP1553Header)}")
                
                header_size = ctypes.sizeof(UDP1553Header)
                message_data = data[header_size:]
                message_data_copy = bytearray(message_data)  # Crea una copia dei dati
                #print(f"Lunghezza di message_data_copy: {len(message_data_copy)}")
                #print(f"Contenuto di message_data_copy: {message_data_copy}")              

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

                # Verifica il tipo di messaggio e i valori
                if isinstance(message, MsgRdrSettingsAndParameters):
                    print("Received MsgRdrSettingsAndParameters (A1)")
                    self.assertEqual(message.settings.get_history_level(), TargetHistory.TARGET_HISTORY_LEVEL_03)
                elif isinstance(message, MsgRdrOperationCommand):
                    print("Received MsgRdrOperationCommand (A2)")
                    self.assertEqual(message.rdr_mode_command.get_master_mode().value, RdrModes.ACM)
                elif message is None:
                    print("Received unknown message type")
                    self.fail("Unknown message type")
                else:
                    print("Received unknown message type")
                    self.fail("Unknown message type")

        except KeyboardInterrupt:
            print("Exiting...")
        finally:
            # Chiudi il ricevitore
            receiver.close()
        
if __name__ == '__main__':
    unittest.main()