#Problem - 2
# This is the program's UI module. The user interface and all interaction with the user (print and input statements) are found here
#
import re
from functions import *
from copy import deepcopy

def parse_command(command:str)->list[str]:
    command=command.strip()
    command=re.split("\s+",command)
    return command

def UI()->None:
    contestants_list=[]
    changes_stack=[]
    generate_list(contestants_list,10)
    while True:
        command = input("> ")
        command = parse_command(command)
        match command[0]:
            case "add":
                try:
                    changes_stack.append(deepcopy(contestants_list))
                    add_contestant(contestants_list,command[1:])
                except Exception as exception:
                    print(exception)
                    changes_stack.pop()
            case "insert":
                try:
                    changes_stack.append(deepcopy(contestants_list))
                    insert_contestant(contestants_list,command[1:])
                except Exception as exception:
                    print(exception)
                    changes_stack.pop()
            case "remove":
                try:
                    changes_stack.append(deepcopy(contestants_list))
                    remove_contestants(contestants_list,command[1:])
                except Exception as exception:
                    print(exception)
                    changes_stack.pop()
            case "replace":
                try:
                    changes_stack.append(deepcopy(contestants_list))
                    replace_contestant(contestants_list,command[1:])
                except Exception as exception:
                    print(exception)
                    changes_stack.pop()
            case "list":
                try:
                    print(list_contestants(contestants_list,command[1:]))
                except Exception as exception:
                    print(exception)
            case "top":
                try:
                    print(top_contestants(contestants_list,command[1:]))
                except Exception as exception:
                    print(exception)
            case "undo":
                if changes_stack:
                    contestants_list=changes_stack.pop()
                else:
                    print("No undo available")
            case "exit":
                break
            case "":
                continue
            case _:
                print("{} command does not exist".format(command[0]))

