# UI section
# Write all functions that have input or print statements here
# Ideally, this section should not contain any calculations relevant to program functionalities

import os
import re

# from list_repr import create_number,stringify
from dict_repr import create_number,stringify
from backend import *

def clear_terminal()->None:
    os.system('cls' if os.name=='nt' else 'clear')

def read_numbers(numbers:list)->list:
    clear_terminal()
    print("Please enter a complex number in the format a+bi or type: \"exit\" to exit.")
    while True:
        inp=input("> ")
        if inp =="exit":
            return numbers
        elif re.match("[-]?\d+[+-]\d+i",inp)!=None:
            parsed_list=re.findall("[-]?\d+",inp)
            number=create_number(int(parsed_list[0]),int(parsed_list[1]))
            numbers.append(number)
        else:
            print("Invalid Number")

def display_numbers(numbers:list)->None:
    if numbers == []:
        print("The list is empty")
        return
    for number in numbers:
        print(stringify(number),end="\t")
    
def display_longest_subarray_of_distinct_numbers(numbers:list)->None:
    lenght,array=get_longest_subarray_of_distinct_numbers(numbers)
    print("The longest subarray of distinct numbers is of lenght {} and its elements are: ".format(lenght))
    display_numbers(array)

def display_lenght_of_longest_alternatig_subsequence(numbers:list)->None:
    print("The length of the longest alternating subsequence is: {}".format(get_lenght_of_longest_alternatig_subsequence(numbers)))

def UI()->None:
    numbers=generate_numbers(10)
    while True:
        print("""
        Please select a number:
        1. Read a list of number
        2. Display the list
        3. Display the length and elements of a longest subarray of distinct numbers
        4. Display the length of a longest alternating subsequence, when considering each number's modulus
        9. Clear the terminal
        0. Exit
        """)
        selected_command=input("> ")
        match selected_command:
            case "1":
                numbers=read_numbers(numbers)
            case "2":
                display_numbers(numbers)
            case "3":
                display_longest_subarray_of_distinct_numbers(numbers)
            case "4":
                display_lenght_of_longest_alternatig_subsequence(numbers)
            case "9":
                clear_terminal()
            case "0":
                return
            case _:
                print("Invalid Command")