from services.services import Services
import re

class ui():
    def __init__(self,repo) -> None:
        self.repo=repo
        self.services=Services(self.repo)
    def __call__(self) -> None:
        while True:
            print("""
    Please select a command:
    1. Add an expense
    2. Display the list of expenses
    3. Filter the list
    4. Undo
    0. Exit     
            """)
            inp=input(">")
            match inp:
                case "1":
                    day=input("Please enter the day: ")
                    if not re.fullmatch("\d+",day) or int(day)<1 or int(day)>30:
                        print("Invalid day. Value must be an integer between 1 and 30")
                        continue
                    money=input("Please enter the amount of money: ")
                    if not re.fullmatch("\d+",money):
                        print("Invalid amount of money. Value must be a positive integer")
                        continue
                    type_of_expense=input("Please enter the type of expense: ")
                    self.services.add_expense(int(day),int(money),type_of_expense)
                case "2":
                    for expense in self.services.display_list():
                        print(expense)
                case "3":
                    value=input("Please enter a value: ")
                    if not re.fullmatch("\d+",value):
                        print("Invalid number. Value must be a positive integer")
                        continue
                    self.services.filter_list(int(value))
                    print("List filtered")
                case "4":
                    try:
                        self.services.undo()
                    except Exception as e:
                        print(e)
                case "0":
                    return
                case _:
                    print("Invalid Command")
