import re
from functions import *

def UI():
    list_of_flights=[
        {
            "code":"0735G",
            "duration":40,
            "dep_city":"Cluj-Napoca",
            "dest_city":"Bucuresti"
        },
        {
            "code":"07378",
            "duration":60,
            "dep_city":"Bucuresti",
            "dest_city":"Istambul"
        },
        {
            "code":"AFNVA",
            "duration":100,
            "dep_city":"Bucuresti",
            "dest_city":"Tokyo"
        },
        {
            "code":"BIV73",
            "duration":20,
            "dep_city":"Bucuresti",
            "dest_city":"Cluj-Napoca"
        },
        {
            "code":"KS23B",
            "duration":60,
            "dep_city":"Istambul",
            "dest_city":"Bucuresti"
        }
    ]
    while True:
        print("""
        1.Add a flight
        2.Modify a flight
        3.Reroute flights
        4.Show flights
        5.Exit
        """)
        i=input(">")
        match i:
            case "1":
                code=input("Please enter a code: ")
                if len(code)<3:
                    print("Invalid code")
                    continue
                duration=input("Please enter a duration: ")
                if not re.match("\d+",duration) or int(duration)<20:
                    print("Invalid duration")
                    continue
                dep_city=input("Please enter a departure city: ")
                if len(dep_city)<3:
                    print("Invalid departure city")
                    continue
                dest_city=input("Please enter a destination city: ")
                if len(dest_city)<3:
                    print("Invalid destination city")
                    continue
                try:
                    add_flight(list_of_flights,code,int(duration),dep_city,dest_city)
                except:
                    print("Flight code already registered")
            case "2":
                code=input("Please enter a code: ")
                if len(code)<3:
                    print("Invalid code")
                    continue
                duration=input("Please enter a duration: ")
                if not re.match("\d+",duration) or int(duration)<20:
                    print("Invalid duration")
                    continue
                try:
                    modify_flight(list_of_flights,code,duration)
                except:
                    print("Invalid flight")
            case "3":
                dest_city=input("Please enter a destination city: ")
                if len(dest_city)<3:
                    print("Invalid destination city")
                    continue
                new_dest_city=input("Please enter a new destination city: ")
                if len(new_dest_city)<3:
                    print("Invalid destination city")
                    continue
                reroute_flight(list_of_flights,dest_city,new_dest_city)
            case "4":
                dep_city=input("Please enter a departure city: ")
                if len(dep_city)<3:
                    print("Invalid departure city")
                    continue
                flights=show_flight(list_of_flights,dep_city)
                for flight in flights:
                    print_nicely(flight)
            case "5":
                break
            case _:
                print("Wrong Command")

