import socket
import threading
import tkinter as tk
import subprocess
from PIL import ImageTk, Image
from tkinter import Tk, Label
import sys
from tkinter import messagebox
from tkinter import ttk
import random
import string
import socket
from tkinter import simpledialog

root = tk.Tk()
root.title("Client")
root.configure(bg="#63bdea")

image = Image.open("vit.png")
image = image.resize((490, 150))

# Create an instance of the ImageTk class to display the image in the window
image_tk = ImageTk.PhotoImage(image)

# Create a Label widget to display the image
image_label = Label(root, image=image_tk)

# Place the label in the window
image_label.pack(pady=50)

frame = tk.Frame(root)
frame.pack()

text_label = tk.Label(root, text="Computer Network Project 2023 (BITE203P)", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=25, y=50)

text_label = tk.Label(root, text="Fall Semester 2023-2024", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=25, y=75)

text_label = tk.Label(root, text="Faculty: Shunmuga P Sir", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=25, y=100)

text_label = tk.Label(root, text="Team Members:", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=1125, y=50)

text_label = tk.Label(root, text="Omkar (21BIT0530)", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=1125, y=75)

text_label = tk.Label(root, text="Aditaya K (21BIT0583)", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=1125, y=100)

text_label = tk.Label(root, text="Malay Shau (21BIT0594)", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=1125, y=125)

text_label = tk.Label(root, text="Diti Jain (21BIT0311)", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=1125, y=150)

text_label = tk.Label(root, text="Tarunika Agarwal (21BIT0442)", bg="#63bdea")
text_label.config(font=("Arial", 18))
text_label.place(x=1125, y=175)


output_box = tk.Text(root)
output_box.place(x=800,y=300)


root.attributes('-fullscreen', True)

def display_message(message):
    output_box.insert(tk.END, message + '\n')
    output_box.see(tk.END)

def createAcc():
    CAGUI()

def Login():
    LNGUI()

def LNGUI():
    window1 = tk.Tk()
    window1.configure(bg="#63bdea")

    def create_profile():
        email = email_entry.get()
        password = password_entry.get()

        if not email:
            messagebox.showwarning("Warning", "Email can't be Empty")
            window1.destroy()
            return
        elif not password:
            messagebox.showwarning("Warning", "Please Enter the Password")
            window1.destroy()
            return

        message = "LI"+" email: "+email+" password: "+password
        client_socket.sendall(message.encode('utf-8'))
        window1.destroy()

    window1.title("Blockchain Crypto Login!")
    window1.geometry("400x200")
    
    email_label = tk.Label(window1, text="Email:",bg="#63bdea", font=("Arial", 12))
    email_label.pack()
    email_entry = tk.Entry(window1)
    email_entry.pack()
    password_label = tk.Label(window1, text="Password:",bg="#63bdea", font=("Arial", 12))
    password_label.pack()
    password_entry = tk.Entry(window1, show="*")
    password_entry.pack()
    create_profile_button = tk.Button(window1, text="Login", command=create_profile,bg="#57ffd1", font=("Arial", 12))
    create_profile_button.pack(pady=20)
    window1.mainloop()


def CAGUI():
    def create_profile(): 
        name = name_entry.get()
        email = email_entry.get()
        password = password_entry.get()

        if not name:
            messagebox.showwarning("Warning", "Filed(s) can't be Empty")
            window.destroy()
            return
        if not email:
            messagebox.showwarning("Warning", "Filed(s) can't be Empty")
            window.destroy()
            return
        if not password:
            messagebox.showwarning("Warning", "Filed(s) can't be Empty")
            window.destroy()
            return

        DATA = "CA"+" name: "+name+" email: "+email+" password: "+password
        client_socket.sendall(DATA.encode('utf-8'))
        window.destroy() 

    window = tk.Tk()
    window.configure(bg="#63bdea")
    window.title("Blockchain Crypto Profile Creation")
    window.geometry("400x200")
    name_label = tk.Label(window, text="Name:",bg="#63bdea", font=("Arial", 12))
    name_label.pack()
    name_entry = tk.Entry(window)
    name_entry.pack()
    
    email_label = tk.Label(window, text="Email:",bg="#63bdea", font=("Arial", 12))
    email_label.pack()
    email_entry = tk.Entry(window)
    email_entry.pack()
    password_label = tk.Label(window, text="Password:",bg="#63bdea", font=("Arial", 12))
    password_label.pack()
    password_entry = tk.Entry(window, show="*")
    password_entry.pack()
    create_profile_button = tk.Button(window, text="Create Profile", command=create_profile,bg="#57ffd1", font=("Arial", 12))
    create_profile_button.pack(pady=20)
    window.mainloop()

def start_client():
    button1.pack_forget()
    global client_socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('192.168.219.219', 8000)  # Replace with the server IP address
    client_socket.connect(server_address)
    print("Connected to the server.")

    receive_thread = threading.Thread(target=receive_data, args=(client_socket,))
    receive_thread.start()

    button1.destroy()
    display_message("Successfully Connected to Server!")
    display_message("Please Choose Anyone from the Given Options")

    button_create = tk.Button(root, text="Create Account",width=20, height=2, font=("Arial", 12), command=createAcc,bg="#00a38e")
    button_create.place(x=300,y=300)

    # Login button
    button_login = tk.Button(root, text="Login into Existing Account",width=20, height=2, font=("Arial", 12), command=Login,bg="#aac6ff")
    button_login.place(x=300,y=380)

button1 = tk.Button(root, text="Click To Connect to Server",width=20, height=2, font=("Arial", 12), command=start_client,bg="#57ffd1")
button1.place(x=250,y=250)

def call_another_gui_program():
    window = tk.Tk()
    window.configure(bg="#63bdea")
    label1_transfer = tk.Label(window, text="Waiting For Conformation",font=("Arial", 20), fg="blue", bg="yellow")
    label2_transfer = tk.Label(window, text="Transfer Successful",font=("Arial", 20), fg="white", bg="green")
    label3_transfer = tk.Label(window, text="Transfer Unsuccessful",font=("Arial", 20), fg="white", bg="red")
    
    def remove_items():
        label1_transfer.place_forget()
        label2_transfer.place_forget()
        label3_transfer.place_forget()

    def generate_random_number():
        number = random.uniform(0, 10)
        increment = random.choice([0.1, 0.2, 0.3])
        decrement = random.choice([-0.1, -0.2, -0.3])
        number += increment
        number += decrement
        return round(number, 2)
        
    number_label = tk.Label(window, text="",font=("Arial", 20))

    def update_number_label():
        number = generate_random_number()
        number_label.place(x=1100,y=10)
        number_label.config(text="Current Bitcoin: "+str((3+number*0.001)),bg="#63bdea")
        window.after(2000, update_number_label)
        
    token_label = tk.Label(window,bg="#63bdea")

    def generate_token():
        update_number_label()
        generate_button.pack_forget()
        token = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
        token_label.config(text="Token: " + token)
        transfer_button.place(x=500,y=250)
        deposit_button.place(x=700,y=250)
        check_currency_button.place(x=900,y=250)
        
    def transfer():
        remove_items()
        label1_transfer.place(x=500,y=350)
                 
        def transfer_amount():
            token_number = token_entry.get()
            amount = amount_entry.get()
            root.destroy()

            client_socket.sendall(f"TF {token_number} {amount}".encode('utf-8'))
            result = client_socket.recv(1024).decode('utf-8')

            # label2_transfer.configure(text=result)

            if(result=='Approved'):
                label1_transfer.place_forget()
                label3_transfer.place_forget()
                label2_transfer.configure(text="Transfer Successful")
                label2_transfer.place(x=500,y=350)
            elif(result[0:6]=='Denied'):
                label1_transfer.place_forget()
                label2_transfer.place_forget()
                label3_transfer.configure(text=f"Transfer Unsuccessful with reason: "+result[7:])
                label3_transfer.place(x=500,y=350)

        
        root = tk.Tk()
        root.title("Token Transfer")
        root.configure(bg="#63bdea")
        # Set window size
        window_width = 400
        window_height = 200
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        x_coordinate = int((screen_width/2) - (window_width/2))
        y_coordinate = int((screen_height/2) - (window_height/2))
        root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}")

        token_label = tk.Label(root, text="Desired Token Number:", font=("Arial", 12),bg="#63bdea")
        token_label.pack(pady=10)
        token_entry = tk.Entry(root)
        token_entry.pack()
        
        amount_label = tk.Label(root, text="Amount:", font=("Arial", 12),bg="#63bdea")
        amount_label.pack()
        amount_entry = tk.Entry(root)
        amount_entry.pack()
        
        transfer_button = tk.Button(root, text="Transfer", command=transfer_amount, font=("Arial", 13),bg="#57ddf1")
        transfer_button.pack(pady=10)
        
        root.mainloop()

    def deposit():
        remove_items()

        # Create a label for displaying the selected option
        label = tk.Label(window, text="Selected Option: ",font=("Arial", 20),bg="#63bdea")
        label.place(x=650,y=350)
        options = ["--Click Here--", "Via Bitcoin", "Via Bank Account"]

        selected_option = tk.StringVar(window)
        selected_option.set(options[0])  # Set the initial selected option
        
        def on_option_selected(*args):
            global select
            selected = selected_option.get()
            select = selected
            label.config(text="Selected Option: " + selected,bg="#63bdea")
            proceed_button.config(state=tk.NORMAL)

        def run():
            label1_transfer.place(x=500,y=350)
            if(select == 'Via Bitcoin'):
                label.place_forget()
                option_menu.place_forget()
                proceed_button.place_forget()
                def deposit_button_click():
                    
                    
                    token_id = token_id_entry.get()
                    token_amount = token_amount_entry.get()
                    token_id_entry.delete(0, tk.END)
                    token_amount_entry.delete(0, tk.END)
                    window.destroy()

                    Data = "DP "+token_id+" "+token_amount
                    client_socket.sendall(Data.encode('utf-8'))
                    result = client_socket.recv(1024).decode('utf-8')
                    label2_transfer.configure(text=result)
                    
                    if(result=='Approved'):
                        label1_transfer.place_forget()
                        label3_transfer.place_forget()
                        label2_transfer.configure(text="Deposit Successful")
                        label2_transfer.place(x=500,y=350)
                    elif(result[0:6]=='Denied'):
                        label1_transfer.place_forget()
                        label2_transfer.place_forget()
                        label3_transfer.configure(text=f"Deposit Unsuccessful with reason: "+result[7:])
                        label3_transfer.place(x=500,y=350)
                        
                    
                window = tk.Tk()
                window.configure(bg="#63bdea")
                window.title("Deposit")

                window.geometry("400x200")
                
                token_id_label = tk.Label(window, text="Token ID:", font=("Arial", 12),bg="#63bdea")
                token_id_label.pack()
                token_id_entry = tk.Entry(window)
                token_id_entry.pack()
                
                token_amount_label = tk.Label(window, text="Token Amount:", font=("Arial", 12),bg="#63bdea")
                token_amount_label.pack()
                token_amount_entry = tk.Entry(window)
                token_amount_entry.pack()
                
                deposit_button = tk.Button(window, text="Deposit", command=deposit_button_click, font=("Arial", 12),bg="#57ffd1")
                deposit_button.pack(pady=10)
                
                window.mainloop()
            elif(select == 'Via Bank Account'):
                label.place_forget()
                option_menu.place_forget()
                proceed_button.place_forget()

                def deposit_button_click():
                    token_id_entry.delete(0, tk.END)
                    token_amount_entry.delete(0, tk.END)
                    label.place_forget()
                    option_menu.place_forget()
                    proceed_button.place_forget()
                    
                    token_id = token_id_entry.get()
                    token_amount = token_amount_entry.get()

                    window.destroy()
                    

                    Data = "DP "+token_id+" "+token_amount
                    client_socket.sendall(Data.encode('utf-8'))
                    
                window = tk.Tk()
                window.configure(bg="#63bdea")
                window.title("Deposit")

                window.geometry("400x200")
                
                token_id_label = tk.Label(window, text="Bank ID:", font=("Arial", 12),bg="#63bdea")
                token_id_label.pack()
                token_id_entry = tk.Entry(window)
                token_id_entry.pack()
                
                token_amount_label = tk.Label(window, text="Token Amount:", font=("Arial", 12),bg="#63bdea")
                token_amount_label.pack()
                token_amount_entry = tk.Entry(window)
                token_amount_entry.pack()
                
                deposit_button = tk.Button(window, text="Deposit", command=deposit_button_click, font=("Arial", 12),bg="#57ffd1")
                deposit_button.pack(pady=10)
                
                window.mainloop()
            
        option_menu = tk.OptionMenu(window, selected_option, *options, command=on_option_selected)
        option_menu.place(x=650,y=400)

        proceed_button = tk.Button(window, text="Proceed", state=tk.DISABLED,width=12, font=("Arial", 12), command=run,bg="#57ffd1")
        proceed_button.place(x=800,y=400)
        
    def check_currency():
        pass
        

    window.title("Blockchain Transaction")
    window.geometry("430x400")  # Set the window size

    token_label.configure(font=("TkDefaultFont", 24))
    token_label.place(x=550,y=10)
    token_label.pack_forget()

    
    generate_button = tk.Button(window, text="Generate Token",width=30, height=2, font=("Arial", 15), command=generate_token,bg="#57ffd1")
    generate_button.pack(padx=700,pady=300)
    
    transfer_button = tk.Button(window, text="Transfer",width=13, height=2, font=("Arial", 15), command=transfer,bg="#ffdead")
    deposit_button = tk.Button(window, text="Deposit",width=13, height=2, font=("Arial", 15), command=deposit,bg="#ffdead") 
    check_currency_button = tk.Button(window, text="Check Currency",width=13, height=2, font=("Arial", 15), command=check_currency,bg="#ffdead")

    
    style = ttk.Style()
    style.configure('TButton', padding=5, relief="flat", background="#ccc")
    window.attributes('-fullscreen', True)
    
    def close_program():
        window.destroy()
        
    exit_button = tk.Button(window, text="Log Out",width=13, height=2, font=("Arial", 8), command=close_program,bg="#ffad9f")
    exit_button.place(x=window.winfo_screenwidth() - 100, y=50)
    
    window.mainloop()


def receive_data(sock):
    while True:
        data = "Message from server --> "+sock.recv(1024).decode('utf-8')
        if not data:
            break
        display_message(data)

        if(data == "Message from server --> Login Successful!"):
            call_another_gui_program()

def send_data(event=None):
    print("Sending Data")

def close_program():
    root.destroy()

exit_button = tk.Button(root, text="Exit",width=10, height=2, font=("Arial", 8), command=close_program,bg="#ffad9f")
exit_button.place(x=root.winfo_screenwidth() - 80, y=50)

root.bind('<Return>', send_data)
root.mainloop()




optimize the code