import java.util.Scanner;

public class RoomExample {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        
        //create rooms
        Room hallway = new Room("Hallway");
        Room livingRoom = new Room("Living Room");
        Room kitchen = new Room("Kitchen");
        Room diningRoom = new Room("Dining Room");
        Room bathroom = new Room("Bath room");     
        Room outside = new Room("Outside");
        Room elevator = new Room("Elevator");
        
        //create doorways
        new Doorway(hallway, livingRoom);
        new Doorway(hallway, kitchen);
        new Doorway(kitchen, diningRoom);
        new Doorway(livingRoom, diningRoom);
        new Door(hallway, bathroom);   
        new LockedDoor(hallway, outside);
        
        //using a anonymous class to create a blocked doorway
        new Doorway(hallway, elevator) {
            @Override
            public void goThrough(Person person) {
                System.out.println("You can't get through here");
            }
            
            @Override
            public String getLabel(Room room) {
                return room.getName() + " [out of order]";
            }
        };

        
        Person user = new Person("Steve", hallway);
        
        while(true) {
            System.out.println(user.getName() + "'s current location: " + user.getRoom().getName());
            System.out.println("Where would you like to go?");
            
            //print out the different rooms adjacent to the current room
            int i = 1;
            for (; i <= user.getRoom().getDoorwayCount(); i++) {
                Doorway doorway = user.getRoom().getDoorway(i);
                Room target = doorway.getOppositeRoom(user.getRoom());
                
                System.out.println(i + ". " + doorway.getLabel(target));
            }
            System.out.println(i + ". Exit");

            int option = myScanner.nextInt();
            if (option >= 1 && option <= user.getRoom().getDoorwayCount()) {
                Doorway selectedDoorway = user.getRoom().getDoorway(option);
                
                
                if (selectedDoorway instanceof Door) {
                    //let the person approach a door
                    Door selectedDoor = (Door)selectedDoorway;
                    selectedDoor.approachDoor(user, myScanner);
                }else{
                    //let the person go through the doorway
                    selectedDoorway.goThrough(user);
                }
            }else if(option == i) {
                break;
            }else{
                System.out.println("Invalid output");
            }
        }
    }
    
}