import tkinter as tk
from opcua_transmitter import transmitter_main
from opcua_receiver import receive_main
import threading
import sys
import os
import json
import multiprocessing

cache_data={
    "sender_cache":{"sender_url":"url","sender_namespace":"namespace","hash_server_ip":"","hash_server_port":"","send_to_ip":"","send_to_port":""},
    "receiver_cache":{"receiver_url":"url","receiver_namespace":"namespace","udp_hash_receiver_ip":"","udp_hash_receiver_port":"","localIP":"","localPort":""}
}
platforms = {
        'linux1': 'Linux',
        'linux2': 'Linux',
        'darwin': 'OS X',
        'win32': os.environ.get('Temp')+"\opcua_cloner.txt"
    }

if(os.path.isfile(platforms[sys.platform])):
    file = open(platforms[sys.platform],"r+")
    cache_data = json.loads(file.read())

class App(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.master.geometry("400x300")
        self.master.title("kl opcua cloner")
        self.pack()
        self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
        self.create_widgets()
        self.t1=None

    def on_closing(self):
        file = open(platforms[sys.platform], "w+")
        file.write(json.dumps(cache_data))
        file.close()
        root.destroy()
        if(self.t1):
            self.t1.terminate()

    def create_widgets(self):
        # Label at the top
        self.label = tk.Label(self, text="Choose the right operation:")
        self.label.pack(side="top")

        # Send button
        self.send_button = tk.Button(self, text="Send", command=self.send_window)
        self.send_button.pack(side="left")

        # Receive button
        self.receive_button = tk.Button(self, text="Receive", command=self.receive_window)
        self.receive_button.pack(side="right")

    def send_window(self):
        # Remove existing widgets
        self.label.pack_forget()
        self.send_button.pack_forget()
        self.receive_button.pack_forget()

        # URL label and entry widget
        self.url_label = tk.Label(self, text="Enter Endpoint URL:")
        self.url_label.pack(side="top")
        self.url_entry = tk.Entry(self)
        self.url_entry.insert(0,cache_data["sender_cache"]["sender_url"])
        self.url_entry.pack(side="top")

        self.namespace_label = tk.Label(self, text="Enter Namespace URL:")
        self.namespace_label.pack(side="top")
        self.namespace_entry = tk.Entry(self)
        self.namespace_entry.insert(0,cache_data["sender_cache"]["sender_namespace"])
        self.namespace_entry.pack(side="top")

        self.server_hash_udp_ip_label = tk.Label(self, text="Enter server_hash_udp_ip:")
        self.server_hash_udp_ip_label.pack(side="top")
        self.server_hash_udp_ip_entry = tk.Entry(self)
        self.server_hash_udp_ip_entry.insert(0,cache_data["sender_cache"]["hash_server_ip"])
        self.server_hash_udp_ip_entry.pack(side="top")

        self.server_hash_udp_port_label = tk.Label(self, text="Enter server_hash_port:")
        self.server_hash_udp_port_label.pack(side="top")
        self.server_hash_udp_port_entry = tk.Entry(self)
        self.server_hash_udp_port_entry.insert(0,cache_data["sender_cache"]["hash_server_port"])
        self.server_hash_udp_port_entry.pack(side="top")

        self.server_udp_ip_label = tk.Label(self, text="Enter server_udp_ip:")
        self.server_udp_ip_label.pack(side="top")
        self.server_udp_ip_entry = tk.Entry(self)
        self.server_udp_ip_entry.insert(0,cache_data["sender_cache"]["send_to_ip"])
        self.server_udp_ip_entry.pack(side="top")

        self.server_udp_port_label = tk.Label(self, text="Enter server_port_ip:")
        self.server_udp_port_label.pack(side="top")
        self.server_udp_port_entry = tk.Entry(self)
        self.server_udp_port_entry.insert(0,cache_data["sender_cache"]["send_to_port"])
        self.server_udp_port_entry.pack(side="top")

        # Submit button
        self.submit_button = tk.Button(self, text="Submit", command=lambda: self.submit_url("Send"))
        self.submit_button.pack(side="top")

        # Back button
        self.back_button = tk.Button(self, text="Back", command=self.back_of_send)
        self.back_button.pack(side="top")

    def receive_window(self):
        # Remove existing widgets
        self.label.pack_forget()
        self.send_button.pack_forget()
        self.receive_button.pack_forget()

        # URL label and entry widget
        self.url_label = tk.Label(self, text="Enter URL:")
        self.url_label.pack(side="top")
        self.url_entry = tk.Entry(self)
        self.url_entry.insert(0,cache_data["receiver_cache"]["receiver_url"])
        self.url_entry.pack(side="top")
        self.namespace_label = tk.Label(self, text="Enter Namespace URL:")
        self.namespace_label.pack(side="top")
        self.namespace_entry = tk.Entry(self)
        self.namespace_entry.insert(0,cache_data["receiver_cache"]["receiver_namespace"])
        self.namespace_entry.pack(side="top")

        self.udp_hash_receiver_ip_label = tk.Label(self, text="Enter udp_hash_receiver_ip:")
        self.udp_hash_receiver_ip_label.pack(side="top")
        self.udp_hash_receiver_ip_entry = tk.Entry(self)
        self.udp_hash_receiver_ip_entry.insert(0,cache_data["receiver_cache"]["udp_hash_receiver_ip"])
        self.udp_hash_receiver_ip_entry.pack(side="top")


        self.udp_hash_receiver_port_label = tk.Label(self, text="Enter udp_hash_receiver_port:")
        self.udp_hash_receiver_port_label.pack(side="top")
        self.udp_hash_receiver_port_entry = tk.Entry(self)
        self.udp_hash_receiver_port_entry.insert(0,cache_data["receiver_cache"]["udp_hash_receiver_port"])
        self.udp_hash_receiver_port_entry.pack(side="top")

        self.udp_ip_label = tk.Label(self, text="Enter udp_ip:")
        self.udp_ip_label.pack(side="top")
        self.udp_ip_entry = tk.Entry(self)
        self.udp_ip_entry.insert(0,cache_data["receiver_cache"]["localIP"])
        self.udp_ip_entry.pack(side="top")

        self.udp_port_label = tk.Label(self, text="Enter server_port:")
        self.udp_port_label.pack(side="top")
        self.udp_port_entry = tk.Entry(self)
        self.udp_port_entry.insert(0,cache_data["receiver_cache"]["localPort"])
        self.udp_port_entry.pack(side="top")

        # Submit button
        self.submit_button = tk.Button(self, text="Submit", command=lambda: self.submit_url("Receive"))
        self.submit_button.pack(side="top")

        # Back button
        self.back_button = tk.Button(self, text="Back", command=self.back_of_receive)
        self.back_button.pack(side="top")

    def submit_url(self, button_label):
        # Get the URL from the entry widget

        # Do something with the URL (e.g. send or receive data)

        if(button_label=="Send"):
            url = self.url_entry.get()
            self.url_entry.configure(state="disabled")
            cache_data["sender_cache"]["sender_url"] = url
            namespace = self.namespace_entry.get()
            cache_data["sender_cache"]["sender_namespace"]=namespace
            self.namespace_entry.configure(state="disabled")
            hash_server_ip = self.server_hash_udp_ip_entry.get()
            cache_data["sender_cache"]["hash_server_ip"] = hash_server_ip
            self.server_hash_udp_ip_entry.configure(state="disabled")
            hash_server_port = self.server_hash_udp_port_entry.get()
            cache_data["sender_cache"]["hash_server_port"] = hash_server_port
            self.server_hash_udp_port_entry.configure(state="disabled")
            send_to_ip = self.server_udp_ip_entry.get()
            cache_data["sender_cache"]["send_to_ip"] = send_to_ip
            self.server_udp_ip_entry.configure(state="disabled")
            send_to_port = self.server_udp_port_entry.get()
            cache_data["sender_cache"]["send_to_port"] = send_to_port
            self.server_udp_port_entry.configure(state="disabled")
            self.t1=multiprocessing.Process(target=transmitter_main,args=(hash_server_ip,int(hash_server_port),send_to_ip,int(send_to_port),url,namespace,))
            self.t1.start()
        else:
            Endpoint_Url = self.url_entry.get()
            cache_data["receiver_cache"]["receiver_url"] = Endpoint_Url
            self.url_entry.configure(state="disabled")
            Namespace_Server = self.namespace_entry.get()
            cache_data["receiver_cache"]["receiver_namespace"] = Namespace_Server
            self.namespace_entry.configure(state="disabled")
            udp_hash_receiver_ip = self.udp_hash_receiver_ip_entry.get()
            cache_data["receiver_cache"]["udp_hash_receiver_ip"] = udp_hash_receiver_ip
            self.udp_hash_receiver_ip_entry.configure(state="disabled")
            udp_hash_receiver_port = self.udp_hash_receiver_port_entry.get()
            cache_data["receiver_cache"]["udp_hash_receiver_port"] = udp_hash_receiver_port
            self.udp_hash_receiver_port_entry.configure(state="disabled")
            localIP = self.udp_ip_entry.get()
            cache_data["receiver_cache"]["localIP"] = localIP
            self.udp_ip_entry.configure(state="disabled")
            localPort = self.udp_port_entry.get()
            cache_data["receiver_cache"]["localPort"] = localPort
            self.udp_port_entry.configure(state="disabled")
            self.t1=multiprocessing.Process(target=receive_main,args=(Endpoint_Url,Namespace_Server,udp_hash_receiver_ip,int(udp_hash_receiver_port),localIP,int(localPort),))
            self.t1.start()

    def back_of_send(self):
        # Remove existing widgets
        self.url_label.pack_forget()
        self.url_entry.pack_forget()
        self.submit_button.pack_forget()
        self.back_button.pack_forget()
        self.namespace_label.pack_forget()
        self.namespace_entry.pack_forget()
        self.server_hash_udp_ip_entry.pack_forget()
        self.server_hash_udp_ip_label.pack_forget()
        self.server_hash_udp_port_entry.pack_forget()
        self.server_hash_udp_port_label.pack_forget()
        self.server_udp_ip_entry.pack_forget()
        self.server_udp_ip_label.pack_forget()
        self.server_udp_port_entry.pack_forget()
        self.server_udp_port_label.pack_forget()
        self.create_widgets()

    def back_of_receive(self):
        self.url_label.pack_forget()
        self.url_entry.pack_forget()
        self.submit_button.pack_forget()
        self.back_button.pack_forget()
        self.namespace_label.pack_forget()
        self.namespace_entry.pack_forget()
        self.udp_hash_receiver_ip_entry.pack_forget()
        self.udp_hash_receiver_ip_label.pack_forget()
        self.udp_hash_receiver_port_entry.pack_forget()
        self.udp_hash_receiver_port_label.pack_forget()
        self.udp_ip_label.pack_forget()
        self.udp_port_label.pack_forget()
        self.udp_ip_entry.pack_forget()
        self.udp_port_entry.pack_forget()
        self.create_widgets()

if __name__=="__main__":
    # Create the main window
    root = tk.Tk()

    # Create the app
    app = App(master=root)
    app.grid()
    # Run the app
    app.mainloop()
