import java.util.Scanner;
public class LockedDoor extends Door {
    
    private boolean locked;
    
    public LockedDoor(Room room1, Room room2) {
        //call the constructor of Door
        super(room1, room2);
        locked = true;
    }
    
    protected boolean isLocked() {
        return locked;
    }
    
    protected void lock() {
        locked = true;
    }
    
    protected void unlock() {
        locked = false;
    }

    //make sure that the user can't open a locked door
    @Override
    protected void open() {
        if (!isLocked()) {
            super.open();
        }
    }
    
    //make sure that the user can't close a locked door
    @Override
    protected void close() {
        if (!isLocked()) {
            super.close();
        }
    }
    
    //allow the person to approach the door to unlock/lock it. If the door is unlocked the user
    //may open it, using the code in the Door class
    @Override
    public void approachDoor(Person person, Scanner scanner) {
        if (isLocked()) {
            System.out.println("Do you want to unlock the door? (yes/ no)");
            if (scanner.next().toLowerCase().equals("yes")) {
                unlock();
            }
        }else if(!isOpen()){
            System.out.println("Do you want to lock the door? (yes/ no)");
            if (scanner.next().toLowerCase().equals("yes")) {
                lock();
            }           
        }
        
        if (!isLocked()) {
            //get the options to open it/go through
            super.approachDoor(person, scanner);
            
            if (!isOpen()) {
                System.out.println("Do you want to lock the door before you leave? (yes/ no)");
                if (scanner.next().toLowerCase().equals("yes")) {
                    lock();
                }                  
            }
        }
    }
    
    @Override
    public String getLabel(Room room) {
        if (isLocked()) {
            return room.getName() + " [locked]";
        }else{
            return super.getLabel(room);
        }
    }
    
}