import json

import os
import sys
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir) 

import NASAPacket
import NASAMessage


encode_raw = "[50, 0, 17, 16, 0, 0, 176, 0, 255, 192, 20, 143, 1, 128, 49, 0, 157, 7, 52]"
encode_raw = "[50, 0, 60, 16, 0, 0, 176, 0, 255, 192, 20, 196, 13, 2, 2, 255, 255, 4, 16, 0, 0, 0, 0, 4, 27, 0, 32, 255, 255, 128, 0, 0, 128, 5, 255, 128, 23, 0, 128, 25, 0, 128, 26, 0, 128, 33, 1, 128, 50, 255, 128, 51, 0, 128, 60, 0, 128, 69, 0, 240, 94, 52]"
encode_raw = "[50, 0, 56, 98, 0, 144, 178, 0, 32, 192, 17, 3, 11, 64, 147, 0, 64, 148, 0, 66, 115, 0, 0, 66, 116, 0, 0, 66, 117, 0, 0, 66, 118, 0, 0, 66, 119, 0, 0, 66, 120, 0, 0, 66, 121, 0, 0, 66, 122, 0, 0, 66, 123, 0, 0, 221, 200, 52]"
encode_raw = "[50, 0, 56, 98, 0, 144, 178, 0, 32, 192, 17, 240, 11, 64, 147, 0, 64, 148, 0, 66, 115, 0, 0, 66, 116, 0, 0, 66, 117, 0, 0, 66, 118, 0, 0, 66, 119, 0, 0, 66, 120, 0, 0, 66, 121, 0, 0, 66, 122, 0, 0, 66, 123, 0, 0, 76, 33, 52]"
#encode_raw ="[50, 0, 48, 98, 0, 144, 178, 0, 32, 192, 17, 240, 11, 64, 147, 0, 64, 148, 0, 66, 115, 0, 0, 66, 116, 0, 66, 117, 0, 66, 118, 0, 66, 119, 0, 66, 120, 0, 66, 121, 0, 66, 122, 0, 66, 123, 0, 7, 180, 52]"
encode_raw = "['0x32', '0x00', '0x1A', '0x80', '0xFF', '0x00', '0x20', '0x00', '0x00', '0xC0', '0x11', '0xB0', '0x01', '0x06', '0x07', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0xBD', '0xBC', '0x34']"
try:
    encode_bytearray = json.loads(encode_raw.strip()) # for [12, 234, 456 ,67]
except:
    encode_tmp = encode_raw.strip().replace("'", "").replace("[", "").replace("]", "").split(", ") # for ['0x1', '0x2' ..]
    encode_bytearray = [int(value, 16) for value in encode_tmp]


print(f"encode raw: {bytearray(encode_bytearray)}")
print(f"encode bytearray: {encode_bytearray}")
print(f"encode bytearray length: {len(encode_bytearray)}")

encoded_nasa = NASAPacket.NASAPacket()
encoded_nasa.parse(encode_bytearray)

print(f"encode NASA Object: {encoded_nasa}")
exit()
# time to reverse that thing!
decoded_nasa = NASAPacket.NASAPacket()
decoded_nasa.set_packet_source_address_class(NASAPacket.AddressClassEnum.JIGTester)
decoded_nasa.set_packet_source_channel(0)
decoded_nasa.set_packet_source_address(0)
decoded_nasa.set_packet_dest_address_class(NASAPacket.AddressClassEnum.BroadcastSelfLayer)
decoded_nasa.set_packet_dest_channel(0)
decoded_nasa.set_packet_dest_address(255)
decoded_nasa.set_packet_information(True)
decoded_nasa.set_packet_version(2)
decoded_nasa.set_packet_retry_count(0)
decoded_nasa.set_packet_type(NASAPacket.PacketType.Normal)
decoded_nasa.set_packet_data_type(NASAPacket.DataType.Notification)
decoded_nasa.set_packet_number(143)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8031)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
decoded_nasa.set_packet_messages([tmp_msg])

decoded_nasa = NASAPacket.NASAPacket()
decoded_nasa.set_packet_source_address_class(NASAPacket.AddressClassEnum.Outdoor)
decoded_nasa.set_packet_source_channel(0)
decoded_nasa.set_packet_source_address(0)
decoded_nasa.set_packet_dest_address_class(NASAPacket.AddressClassEnum.BroadcastSelfLayer)
decoded_nasa.set_packet_dest_channel(0)
decoded_nasa.set_packet_dest_address(255)
decoded_nasa.set_packet_information(True)
decoded_nasa.set_packet_version(2)
decoded_nasa.set_packet_retry_count(0)
decoded_nasa.set_packet_type(NASAPacket.PacketType.Normal)
decoded_nasa.set_packet_data_type(NASAPacket.DataType.Notification)
decoded_nasa.set_packet_number(196)
lst = []
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x0202)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([255, 255])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x0410)
tmp_msg.set_packet_message_type(2)
tmp_msg.set_packet_payload([0, 0, 0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x41b)
tmp_msg.set_packet_message_type(2)
tmp_msg.set_packet_payload([0, 32, 255, 255])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8000)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8005)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([255])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8017)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8019)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x801a)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8021)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([1])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8032)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([255])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8033)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x803c)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x8045)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
decoded_nasa.set_packet_messages(lst)

#rasw = decoded_nasa.to_raw()

decoded_nasa = NASAPacket.NASAPacket()
decoded_nasa.set_packet_source_address_class(NASAPacket.AddressClassEnum.WiFiKit)
decoded_nasa.set_packet_source_channel(0)
decoded_nasa.set_packet_source_address(144)
decoded_nasa.set_packet_dest_address_class(NASAPacket.AddressClassEnum.BroadcastSetLayer)
decoded_nasa.set_packet_dest_channel(0)
decoded_nasa.set_packet_dest_address(32)
decoded_nasa.set_packet_information(True)
decoded_nasa.set_packet_version(2)
decoded_nasa.set_packet_retry_count(0)
decoded_nasa.set_packet_type(NASAPacket.PacketType.Normal)
decoded_nasa.set_packet_data_type(NASAPacket.DataType.Read)
decoded_nasa.set_packet_number(240)
lst = []
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4093)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4094)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4273)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4274)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4275)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4276)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4277)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4278)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x4279)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x427a)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x427b)
tmp_msg.set_packet_message_type(1)
tmp_msg.set_packet_payload([0, 0])
lst.append(tmp_msg)
decoded_nasa.set_packet_messages(lst)

decoded_nasa = NASAPacket.NASAPacket()
decoded_nasa.set_packet_source_address_class(NASAPacket.AddressClassEnum.WiFiKit)
decoded_nasa.set_packet_source_channel(0)
decoded_nasa.set_packet_source_address(144)
decoded_nasa.set_packet_dest_address_class(NASAPacket.AddressClassEnum.BroadcastSelfLayer)
decoded_nasa.set_packet_dest_channel(255)
decoded_nasa.set_packet_dest_address(255)
decoded_nasa.set_packet_information(True)
decoded_nasa.set_packet_version(2)
decoded_nasa.set_packet_retry_count(0)
decoded_nasa.set_packet_type(NASAPacket.PacketType.Normal)
decoded_nasa.set_packet_data_type(NASAPacket.DataType.Notification)
decoded_nasa.set_packet_number(168)
tmp_msg = NASAMessage.NASAMessage()
tmp_msg.set_packet_message(0x0000)
tmp_msg.set_packet_message_type(0)
tmp_msg.set_packet_payload([2])
decoded_nasa.set_packet_messages([tmp_msg])


rasw = decoded_nasa.to_raw()

print(f"decoded bytearray: {rasw}")
print(f"decoded NASA Object: {decoded_nasa}")
print(f"decoded bytearray: {[int(value) for value in rasw]}")

print("Reverse Check:")
checkback = NASAPacket.NASAPacket()
checkback.parse(rasw)
print(checkback)