import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))
import threading
from pynput.keyboard import Key, Listener
from Grifo_E_1553lib.data_types.enums import TargetHistory, RdrModes
from Grifo_E_1553lib.connection.utils import *
from Grifo_E_1553lib.messages.messages import *
import gui


# b7 = B7()
b7 = MessageNew("B7",MsgRdrStatusTellback,25,17,True)
# print(MessageDB.getAllMessages())
# print(b7.label)
# exit()

# a1 = A1()
a1 = MessageNew("A1",MsgRdrSettingsAndParameters,10,1)
a1.message.settings.set_history_level(TargetHistory.TARGET_HISTORY_LEVEL_04)
a1.message.settings.str.rdr_symbology_intensity = 127
# a2 = A2()
a2 = MessageNew("A2",MsgRdrOperationCommand,25,2)
a2.message.rdr_mode_command.set_master_mode(RdrModes.RWS)
a2.message.param1.set_gm_submode(0)
a2.message.param2.set_spare_0_4(0)
# a3 = A3()
# print("printtttttttttttttttttttttttttt")
print(MessageDB.getMessage("A2"))
# print("printtttttttttttttttttttttttttt")

a3 = MessageNew("A3",MsgGraphicSetting,10,3)

print(a3.head.cw.raw)
print(a3.size)

# a4 = A4()
a4 = MessageNew("A4",MsgNavDataAndCursor,50,4)
a4.message.validity_and_slew.raw = 0

# a5 = A5()
a5 = MessageNew("A5",MsgInuHighSpeed,50,5)
a5.message.timetag.raw = 0


# b1 = B1()


print(MajorFrame.get_majorFrame())

# print(ctypes.sizeof(b7.message))
# print(isA(a2), isA(a4), isA(a5))

# b1 =Message(TwsStatusAndTargets0102,11, True)
# b1 =Message(TwsStatusAndTargets0102,11, True)

# print(vars(RdrModes))
# print(list(RdrModes)[1].name + " = " + str(list(RdrModes)[1].value))

# def send_frame_at_right_time(stop, canSend):
#     print("Sending frames...")
#     while not stop.is_set():
#         for key in MajorFrame.get_majorFrame().keys():
#             if current_milli_time() % key == 0 and canSend.is_set():
#                 A5().update_A5_time()
#                 udp_msg = prepare_udp_message(MajorFrame.get_majorFrame()[key])
#                 send_udp_msg(udp_msg)
###############################################################################

def listen_for_command():
    with Listener(on_press = keypressed) as listener:   
        listener.join()

canSend.set()

guii = True

t1 = threading.Thread(target = MajorFrame.send_frame_at_right_time_new, args=[STOP_ALL,canSend])
t2 = threading.Thread(target=recv_messages)
t3 = threading.Thread(target=gui.main_menu) if guii else None
# t4 = threading.Thread(target=listen_for_command)

t1.start()
t2.start()
t3.start() if guii else None
# t4.start()

# while not STOP_ALL.is_set():
#     # udp_msg = prepare_udp_message([a5])
#     # send_udp_msg(udp_msg)
#     # print(f"Size of A5: {ctypes.sizeof(MsgInuHighSpeed)}")
#     # print("PREPARE")
#     print(isA(b1))
#     udp_msg = prepare_udp_message([b1])
#     print("Send")
    
#     send_udp_msg(udp_msg)
#     time.sleep(3)
# # print(RdrModes.__dict__.items())
# # def stampa_class_constants(cls):
# #     for name, value in cls.__dict__.items():
# #         if not name.startswith("__") and not callable(value):
# #             print(f"{name} = {value}")
# # stampa_class_constants(RdrModes)



t1.join()
t2.join()
t3.join()
# t4.join()

print("Program Terminated.")