import java.util.Scanner;
public class Door extends Doorway {
    
    private boolean open;
   
    public Door(Room room1, Room room2) {
        //call the constructor of Doorway
        super(room1, room2);
        open = false;
   }
    
    protected boolean isOpen() {
        return open;
    }
    
    protected void open() {
        open = true;
    }
    
    protected void close() {
        open = false;
    }
    
    //Make sure that you can't go through a closed door
    @Override
    public void goThrough(Person person) {
        if (isOpen()) {
            super.goThrough(person);
        }else{
            System.out.println("You can't walk through a closed door");
        }
    }
    
    //let a person approach the door and get options to open it/close it
    public void approachDoor(Person person, Scanner scanner) {
        if (isOpen()) {
            System.out.println("Do you want to close this door? (yes/no)");
            if (scanner.next().toLowerCase().equals("yes")) {
                close();
            }
        }else{
            System.out.println("Do you want to open this door? (yes/no)");
            if (scanner.next().toLowerCase().equals("yes")) {
                open();
            }           
        }
        
        if (isOpen()) {
            System.out.println("Do you want to go through the open doorway? (yes/no)");
            
            if (scanner.next().toLowerCase().equals("yes")) {
                goThrough(person);
                
                System.out.println("Do you want to close the door behind you? (yes/no)");
                if (scanner.next().toLowerCase().equals("yes")) {
                    close();
                }                   
            }             
        }
    }
    
    //add extra information to the label
    @Override
    public String getLabel(Room room) {
        return room.getName() + (isOpen() ? " [opened]" : " [closed]");
    }
}