import Backend
from getpass import getpass
from customtkinter import *
from tkinter import filedialog, simpledialog, messagebox

global version
global modpack
global modpack_name
global modpack_id
global username
global password
global modpacks

def get_credentials():
    global username
    global password
    username = simpledialog.askstring("username", "Username: ")
    password = simpledialog.askstring("password", "Password: ", show="*")

def select_file():
    global fileName
    global file_path_entry
    file = filedialog.askopenfilename()
    if file:
        fileName = file
        file_path_entry.insert(0, fileName)

def update_pack():
    global modpacks
    global fileName
    global username
    global password
    global modpack
    global version
    get_credentials()
    pack_id = None
    for pack in modpacks:
        if pack["name"] == modpack.get():
            pack_id = pack["id"]
    Backend.upload_pack(username, password, version.get(), fileName, pack_id)
    messagebox.Message(title="complete", message="Modpack updated successfuly").show()

def create_pack():
    global username
    global password
    global modpacks
    global modpack_id
    global modpack_name
    global version
    global fileName
    for pack in modpacks:
        if pack["id"] == modpack_id.get():
            messagebox.Message(title="Error", message="Pack ID already exists").show()
            return
        if pack["name"] == modpack_name.get():
            messagebox.Message(title="Error", message="Pack name already exists").show()
            return
    get_credentials()
    Backend.createModpack(modpack_id.get(), modpack_name.get(), username, password)
    Backend.upload_pack(username, password, version.get(), fileName, modpack_id.get())
    messagebox.Message(title="complete", message="Modpack created successfully").show()

def delete_pack():
    global username
    global password
    global modpacks
    global modpack_delete
    get_credentials()
    pack_id = None
    for pack in modpacks:
        if pack["name"] == modpack_delete.get():
            pack_id = pack["id"]

    Backend.deleteModpack(username, password, pack_id)

def main():
    global fileName
    global modpacks
    global modpack_id
    global version
    global modpack
    global modpack_name
    global modpack_delete
    fileName = "modpack.mrpack"

    app = CTk()
    if os.name == 'posix':
        app.attributes('-type', 'dialog')
    app.geometry('500x400')
    app.title("FamilyCraft Launcher Upload Utility")
    set_appearance_mode("dark")
    set_default_color_theme("blue")
    
    version = StringVar()
    modpack = StringVar()
    modpack_name = StringVar()
    modpack_id = StringVar()
    modpack_delete = StringVar()



    
    file_label = CTkLabel(master=app, text="Select mrpack file:", font=("Impact", 15))
    file_label.pack()
    global file_path_entry
    file_path_entry = CTkEntry(master=app, width=350, font=("Arial", 14))
    file_path_entry.pack(pady=5)
    file_path_entry.insert(0, fileName)
    browse_button = CTkButton(master=app, text="Browse...", command=select_file)
    browse_button.pack(pady=5)

    version_label = CTkLabel(master=app, text="Version Number:", font=("Impact", 15))
    version_label.pack()
    version_entry = CTkEntry(master=app, width=350, font=("Arial", 14), placeholder_text="0.0.0", textvariable=version)
    version_entry.pack(pady=5)

    guitabs = CTkTabview(master=app, bg_color="transparent")
    guitabs.pack(expand=True, fill='x', side="bottom", padx=0, pady=(10,0))
    guitabs.add("Update Pack")
    guitabs.add("Create Pack")
    guitabs.add("Delete Pack")

    modpacks = Backend.getModpacks()
    options = []
    for pack in modpacks:
        options.append(pack["name"])
    modpack_selector = CTkOptionMenu(master=guitabs.tab("Update Pack"), values=options, variable=modpack)
    modpack_selector.set(options[0])
    modpack_selector.pack()

    update_button = CTkButton(master=guitabs.tab("Update Pack"), text="Update", command=update_pack)
    update_button.pack(pady=5)

    modpack_id_label = CTkLabel(master=guitabs.tab("Create Pack"), text="ID:", font=("Impact", 15))
    modpack_id_label.pack()
    modpack_id_entry = CTkEntry(master=guitabs.tab("Create Pack"), width=350, font=("Arial", 14), textvariable=modpack_id)
    modpack_id_entry.pack(pady=5)

    modpack_name_label = CTkLabel(master=guitabs.tab("Create Pack"), text="Name: ", font=("Impact", 15))
    modpack_name_label.pack()
    modpack_name_entry = CTkEntry(master=guitabs.tab("Create Pack"), width=350, font=("Arial", 14), textvariable=modpack_name)
    modpack_name_entry.pack(pady=5)

    create_button = CTkButton(master=guitabs.tab("Create Pack"), text="Create", command=create_pack)
    create_button.pack(pady=5)

    modpack_selector_delete = CTkOptionMenu(master=guitabs.tab("Delete Pack"), values=options, variable=modpack_delete)
    modpack_selector_delete.set(options[0])
    modpack_selector_delete.pack()

    delete_button = CTkButton(master=guitabs.tab("Delete Pack"), text="Delete", command=delete_pack)
    delete_button.pack(pady=5)







    #username = input("Username: ")
    #password = getpass()
    #version_tag = input("Version Number: ")
    #Backend.upload_pack(username, password, version_tag, fileName, "test")
    app.mainloop()

    
if __name__=="__main__":
    main()
