from controller import Controller
from datetime import date
from prettytable import PrettyTable


class UI:
    def __init__(self, controller):
        self.__controller = controller
        self.months=["January","February","March","April","May","June","July","August","September","October","November","December"]
    
    def start(self):
        while True:
            print("""
            1. Display reservations
            2. Create reservation
            3. Delete reservation
            4. Monthly report 
            5. Exit
            """)
            command = input("Enter command: ")
            match command:
                case "1":
                    try:
                        start ="2023-" + input("Enter start date: ")
                        end = "2023-" + input("Enter end date: ")
                        reservations = self.__controller.display(date.fromisoformat(start), date.fromisoformat(end))
                        i=0
                        while i<12:
                            table = PrettyTable((self.months[i],"Name","Guests"))
                            for reservation in reservations:
                                if reservation.start_date.month<=i+1 and reservation.end_date.month>=i+1:
                                    table.add_row((str(reservation.start_date)+"|-| "+str(reservation.end_date),reservation.last_name+" "+reservation.first_name,reservation.guests))
                            if len(table.rows)>0:
                                print(table)
                            i+=1
                    except Exception as e:
                        raise e
                        
                case "2":
                    try:
                        arrival ="2023-"+ input("Enter arrival date: ")
                        departure ="2023-"+ input("Enter departure date: ")
                        rooms = self.__controller.get_empty_rooms(date.fromisoformat(arrival), date.fromisoformat(departure))
                        if len(rooms) == 0:
                            print("No rooms available")
                            break
                        print("Available rooms: ")
                        for room in rooms:
                            print(room)
                        room_type = input("Enter room type: ")
                        if room_type == "exit":
                            break
                        if room_type not in rooms:
                            print("Invalid room type")
                            break
                        guests = input("Enter number of guests: ")
                        if guests == "exit":
                            break
                        if guests == "":
                            print("Invalid number of guests")
                            break
                        last_name = input("Enter last name: ")
                        if last_name == "exit":
                            break
                        if last_name == "":
                            print("Invalid last name")
                            break
                        first_name = input("Enter first name: ")
                        if first_name == "exit":
                            break
                        if first_name == "":
                            print("Invalid first name")
                            break
                        self.__controller.create_reservation(last_name, first_name, room_type, guests, date.fromisoformat(arrival), date.fromisoformat(departure))
                    except Exception as e:
                        print("Error")
                case "3":
                    try:
                        print("""   
    1. Delete by id
    2. Delete by date
    """)
                        command2 = input("Enter command: ")
                        match command2:
                            case "1":
                                id = input("Enter id: ")
                                self.__controller.delete_reservation(id)
                            case "2":
                                start ="2023-" + input("Enter start date: ")
                                end ="2023-" + input("Enter end date: ")
                                room=input("Enter room type: ")
                                reservations=self.__controller.delete_reservation_by_date(date.fromisoformat(start), date.fromisoformat(end),room)
                                if len(reservations)==0:
                                    print("No reservations deleted")
                                else:
                                    i=0
                                    while i<12:
                                        table = PrettyTable((self.months[i],"Name","Guests"))
                                        for reservation in reservations:
                                            if reservation.start_date.month<=i+1 and reservation.end_date.month>=i+1:
                                                table.add_row((str(reservation.start_date)+"|-| "+str(reservation.end_date),reservation.last_name+" "+reservation.first_name,reservation.guests))
                                        if len(table.rows)>0:
                                            print(table)
                                        i+=1

                            case _:
                                print("Invalid command")
                    except Exception as e:
                        print("Error")
                case "4":
                    try:
                        month = int(input("Enter month: "))
                        if month < 1 or month > 12:
                            raise Exception("Invalid month")
                        report = self.__controller.monthly_report(month)
                        table = PrettyTable(("M", "Tu", "W", "Th", "F", "Sa", "Su"))
                        for week in report:
                            if len(week) < 7:
                                continue
                            table.add_row(week)
                        print(table)
                    except Exception as e:
                        raise e
                case "5":
                    break
                case _:
                    print("Invalid command")

