# 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_operation_command import MsgRdrOperationCommand
from Grifo_E_1553lib.data_types.enums import TargetHistory, FrequencyAgilityType, FrequencyGroupSelector, WaveformInterleave, LPRFThreshold, GroundTargetRejectVel, MinDetectGroundVel, ALEBlanking, AltitudeBlock, PRFLookup, ParameterTransferEnabled, ReceiveTransmitSelector, ParamIdCode, FrequencyGrouping, RdrModes, DesControl, IbitRequest, StbySelection, SarExecSelection, RwsSubmode, SpotSelection, AcmSubmode, GmSubmode, Expand, RangeScale, BarsNum, ScanWidth, VelocityScale, FrequencyGrouping, Zoom, SarMapOrientation, BullsEyeIdentifier, WaypointFytSelection, WaypointQualifierValue, NavDataInvalidField, CrsGhostSelector, CrsDttSelector, CrsSarSelector, DlTargetValidity, DlTargetTypeSelector, FriendFoeSelector, TwsTargetValidity, TwsTargetWhich, TrackPriority, TargetAccurancy

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()

        # 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 A2
        self.assertIsInstance(message, MsgRdrOperationCommand)
        self.assertEqual(message.rdr_mode_command.get_master_mode().value, RdrModes.ACM)

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