def stop():
    print('\n\n\nThank you for using ESP and goodbye! ')
#   exit(0)

def init(): #initialise the menu dictionary with item: price values...
    menu = {'Firewall Services':1.2,'Security Ops Center':4.2,'Hot Site':8.5,'Data Protection':10.0}
    

def main(s='no services'): #display menu and prompt user for choice
    choice='0'
    while choice!='' and choice!='5':
        print(f"\n\n===========================================\n"+
            "Welcome to Electronics Services & Protection:\n"+
            "===============================================\n")
        choice = input('''
    1. Display Our list of Services
    2. Search for service
    3. Display added services
    4. Payment
    5. Exit ESP
    6. Display Services currently subscribed



    Please input your choice of action(ENTER to exit):''')
        if choice == '1':
            #displayMenu()
            addOrder(list(menu.keys()))
        elif choice == '2':
            search()
        elif choice == '3':
            displayCart()
        elif choice == '4':
            checkout()
        elif choice == '6':
            input(f"You have currently subscribed to: \n{s}\nPress Enter key to continue... ")
        elif (choice ==''or choice =='5'):
            stop()

def checkout(): #list the cart and total the amount to pay
    if (len(order)>0):
        print("please check your services added:\n")
        total=0
        for i,item in enumerate(order):
            print(f"{i+1}. {item:20}:\t${menu[item]}k/year")
            total+=menu[item]
        order.clear()
        input(f"Your subscripton will be a total of : ${total}k/year. \nThank you for using ESP.")
    else:
        print('You have not added any service!')

def displayCart(): #display menu for the day
    if len(order) == 0:
        input('You have not added any service! Press enter to continue...')
        return
    print(f"Services you have added today: ")
    for i, item in enumerate(order,1):
        print(f"{i}. {item:20}:\t${menu[item]}k")
    choice = 1
    while (choice)!=0:
        choice=eval(input(f"Enter the service 1-{i} that you would like to remove, or 0 to stop: "))
        if choice>=0 and choice<=i:
            if choice>0:
                print(f"{order.pop(choice-1)} removed")
                return
            print(order)
        else:
            print(f"Invalid choice! Please enter 1-{i} or 0 to stop")

    print()
    input("Press any key to continue...")

def search():
    choice= " "

    while len(choice)>0:
        choice = input("\n\nPlease input service to search: ")
        if len(choice)==0:
            break

    matches=[]

    for service in menu.keys():
        if service.lower().find(choice.lower())>=0:
            matches.append(service.strip())

    if len(matches)==0:
        print(f"Sorry we don't have services: {choice}!\n")
        choice=input("Please press ENTER key to quit or 'N' enter new search: ")
    else:
        addOrder(matches)
        

def addOrder(matches):
    submenu=""
    for i,service in enumerate(matches,1):
        submenu+=f"\n{i}. {service:20}:\t${menu[service]}k/year"

    print(f"yes, we have the following services(s):{submenu}")
    choice=-1
    while (choice)!=0:
        choice=0 #indicate exit case
        choice=eval(input(f"Enter the service 1-{i} that you would like to add, or 0 to stop: "))
        if choice>=0 and choice<=i:
            if choice>0:
                order.append(matches[choice-1])
                print(f"{choice} added")
            #print(order)
        else:
            print(f"Invalid choice! Please enter 1-{i} or 0 to stop")
            
    print(f"You have added: {order}")

order=[] #cart
menu={} #menu
if __name__ == '__main__':
    init() #populate menu
    main() #display welcome screen