package taxi;
import java.util.*;

public class House extends Building {
	
	private List residents;
	
	public House(Map map) throws IllegalArgumentException {
		super(map);
		type = Type.HOUSE;
		residents = new ArrayList();
				
		// list of resident(s) is optional & not limited to String elements
		if (map.containsKey("resident")) {
			Object o = map.get("resident");
			
			if (o instanceof List) {
				residents.addAll((List) o);
				
			} else if (o instanceof String) {
				residents.add((String) o);
				
			} else {
				throw new IllegalArgumentException();
			}
		}
	}
	
	public House(String name, Address address, List residents) {
		super(name, address);
		type = Type.HOUSE;
		this.residents = new ArrayList();
		try {
			this.residents.addAll(residents);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	protected boolean altNameMatches(String name) {
		for (int i = 0; i < residents.size(); i++) {
			if (((String) residents.get(i)).toLowerCase().startsWith(name.toLowerCase())) {
				return true;
			}
		}
		return false;
	}
	
	public List getResidents() {
		return residents;
	}
	
	public void setResidents(List residents) {
		List backupResidents = new ArrayList();
		backupResidents.addAll(this.residents);
		this.residents.clear();
		
		try {
			this.residents.addAll(residents);
		} catch (Exception e) {
			this.residents.addAll(backupResidents);
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

	public boolean equals(Object o) {
		return 	o instanceof House
			?	((Building) o).equals((Building) this)
			?	((House) o).residents.equals(((House) this).residents)
			?	true : false : false : false;
	}
	
	public String toString() {
		String rtn = super.toString();
		
		rtn += "\nResidents:";
		
		for (int i = 0; i < residents.size(); i++) {
			rtn += "\n" + (String) residents.get(i);
		}
		
		return rtn;
	}
		

}		