package inclass01;

import java.util.ArrayList;
import java.lang.IllegalArgumentException;
/*
 * Class to follow family relationships
 */
public class Person {
	private String firstName;
	private String lastName;
	private boolean isMale;
	private Person father;
	private Person mother;
	private Person spouse;
	private ArrayList<Person> children;

	public Person(Person mom,String babiesName,boolean isBoy) {
		firstName=babiesName;
		if (mom.spouse==null) { lastName=mom.lastName; }
		else { lastName = mom.spouse.lastName; }
		isMale=isBoy;
		father=mom.spouse;
		mother=mom;
		children = new ArrayList<Person>();
	}

	public Person(String fname, String lname, boolean isBoy) {
		firstName = fname;
		lastName=lname;
		isMale=isBoy;
		children = new ArrayList<Person>();
	}

	public String getFirstName() { return firstName; }
	public String getLastName() { return lastName; }
	public boolean getIsMale() { return isMale; }
	public Person getMother() { return mother; }
	public Person getFather() { return father; }
	public Person getSpouse() { return spouse; }

	public String getAncestors() {
		String result="";
		if (mother != null) {
			result +=" " + mother.getName();
			result += mother.getAncestors();
		}
		if (father != null) {
			result += " " + father.getName();
			result += father.getAncestors();
		}
		return result;
	}

	public String getName() { return firstName + " " + lastName; }

	public String toString() { return firstName + " " + lastName; }

	public void marry(Person horw) {
		if (spouse != null || horw.spouse != null) {
			System.out.println("Can't marry twice!");
			return;
		}
		spouse=horw;
		horw.spouse = this;
	}

	public Person birth(String babyName, boolean isBoy) throws IllegalArgumentException {
		if (isMale) {
			throw new IllegalArgumentException("Men cant have babies");
		}
		Person baby = new Person(this,babyName,isBoy);
		children.add(baby);
		if (spouse != null) { spouse.children.add(baby); }
		return baby;
	}

	public static void main(String[] args) throws IllegalArgumentException {
		Person mgm = new Person("Isabel","Webster",false);
		Person mgf = new Person("James","Anderson",true);
		mgm.marry(mgf);
		Person mom = mgm.birth("Isabel",false);
		Person pgm = new Person("Anna","Curtis",false);
		Person pgf = new Person("Fred","Bartenstein",true);
		pgm.marry(pgf);
		Person dad = pgm.birth("Fred",true);
		mom.marry(dad);
		Person me = mom.birth("Tom",true);
		System.out.println(me + " ancestry: " +me.getAncestors());
	}

}