/*
 *	EasyScout Boy Scouts of America tracking software.
 *	http://www.easyscout.org/
 *
 *	Copyright (C) 2000 Conan C. Albrecht
 *	conan@warp.cx
 *
 *	This program is released under the GNU license so tech-savvy
 *	Boy Scouts and others can enhance the program for the use
 *	of all.  Please e-mail changes to my address above so I can
 *	reintegrate into the main program at http://www.easyscout.org/.
 *	This way, all users mutually benefit one another in the spirit of
 *	scouting.
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version 2
 *	of the License, or (at your option) any later version.
 *
 * 	Any and all changes to the program code must be documented with
 *  date, author name and e-mail, and a short description.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package cx.warp.scout.file;

import cx.warp.scout.*;
import java.util.*;
import java.io.*;


/** Superclass of all people objects */
public class FilePerson implements cx.warp.scout.Identifiable, Serializable {

	public static final long serialVersionUID = 2632689177875333832L;

	protected FileTroop troop = null;
	protected String notes = null;
	protected String id = Utils.getGUID();
	protected String fname = null;
	protected String mname = null;
	protected String lname = null;
	protected Date birthdate = null;
	protected String address = null;
	protected String phone = null;
	protected String email = null;
	protected String password = null;
	
	/** Constructor */
	public FilePerson(FileTroop troop) throws DataException {
		this.troop = troop;
	}//FilePerson

	/** Saves this scout information */
	public void save() throws DataException {
		if (troop == null) {
			throw new DataException("Troop reference is null!  Cannot save changes.");
		}else{
			troop.save();
		}//if
	}//save

	/** Returns the scout's unique id */
	public String getID() throws DataException						{ 
	  return id; 
	}//getID

	/** Sets the scout's first name */
	public void setFirstName(String fname) throws DataException {
		this.fname = fname;
	  save();
	}
	
	/** Returns the scout's first name */
	public String getFirstName() throws DataException  	{ 
		return fname;	
	}
	
	/** Sets the scout's middle name */
	public void setMiddleName(String mname) throws DataException { 
		this.mname = mname; 
	  save();
	}

	/** Returns the scout's middle name */
	public String getMiddleName() throws DataException 	{	
		return mname; 
	}
	
	/** Sets the scout's last name */
	public void setLastName(String lname) throws DataException { 
		this.lname = lname; 
	  save();
	}

	/** Returns the scout's last name */
	public String getLastName() throws DataException   	{
		return lname; 
	}
	
	/** Sets the scout's birthdate */
	public void setBirthDate(Date birth) throws DataException { 
		this.birthdate = birth;
	  save();
	} 
	
	/** Returns the scout's birthdate */
	public Date   getBirthDate() throws DataException  	{	
		return birthdate;
	}
	
	/** Sets the scout's address */
	public void setAddress(String address) throws DataException { 
		this.address = address;
	  save();
	}
	
	/** Returns the scout's address */
	public String getAddress()  throws DataException   	{ 
		return address;
	}
	
	/** Sets the scout's e-mail */
	public void setEmail(String email) throws DataException {
		this.email = email; 
	  save();
	}
	
	/** Returns the scout's e-mail address */
	public String getEmail() throws DataException        { 
		return email;
	}
	
	/** Sets the scout's phone number */
	public void setPhone(String phone) throws DataException { 
		this.phone = phone;
	  save();
	}
	
	/** Returns the scout's phone number */
	public String getPhone() throws DataException      	{ 
		return phone;
	}
	
	/** Sets the notes about the scout */
	public void setNotes(String notes) throws DataException {
		this.notes = notes; 
	  save();
	}
	
	/** Returns notes about the scout */
	public String getNotes()  throws DataException       {
		return notes;
	}
	
	/** Returns the scout's name, in the style: first middle last */
	public String getName() throws DataException {
		if (mname == null || mname.equals("")) {
			return fname + " " + lname;
		}else{
	  	return fname + " " + mname + " " + lname;
	  }//if
	}//getname
	
	/** Returns the scout's name, in the style: last, first middle */
	public String getFullName() throws DataException {
		if (mname == null || mname.equals("")) {
			return lname + ", " + fname;
		}else{
			return lname + ", " + fname + " " + mname;
		}//if
	}//if
	
	/** Returns the person's name in the style: first initial. last name */
	public String getSmallName() throws DataException {
		if (fname == null || fname.length() == 0) {
			return lname;
		}else{
			return fname.substring(0, 1) + " " + lname;
		}//if
	}//getSmallName
	
	
	/** Returns the scout's age as of today */
	public String getAge() throws DataException {
		Calendar now = Calendar.getInstance();
		Calendar birth = Calendar.getInstance();
		birth.setTime(birthdate);
		int years = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		int months = now.get(Calendar.MONTH) - birth.get(Calendar.MONTH);
		int days = now.get(Calendar.DATE) - birth.get(Calendar.DATE);
		if (months < 0 || (months == 0 && days < 0)) { // adjust if the person hasn't had their birthday yet this year 
			years--;
		}//if
		return String.valueOf(years);
	}//getAge
	
	/** Writes the contents of this person to an XML buffer */
	public void writeXML(PrintWriter out) throws DataException {
		out.println("    <FirstName>" + Utils.xml(fname) + "</FirstName>"); 
		out.println("    <MiddleName>" + Utils.xml(mname) + "</MiddleName>"); 
		out.println("    <LastName>" + Utils.xml(lname) + "</LastName>");
		out.println("    <Birthdate>" + Utils.xml(Utils.format(birthdate)) + "</Birthdate>");
		out.println("    <Address>" + Utils.xml(address) + "</Address>");
		out.println("    <Phone>" + Utils.xml(phone) + "</Phone>");
		out.println("    <Email>" + Utils.xml(email) + "</Email>");
		out.println("    <Notes>" + Utils.xml(notes) + "</Notes>"); 
		out.println("    <Password>" + Utils.xml(password) + "</Password>"); 
	}//writeXML
	
	/** Sets the password for the person */
	public void setPassword(String pass) throws DataException {
		this.password = pass;
		save();
	}//setPassword
	
	/** Verifies a password for this person */
	public boolean verifyPassword(String pass) throws DataException {
		return (pass.equals(password));
	}//verifyPassword
	
	/** Returns the current password */
	public String getPassword() throws DataException {
		return password;
	}//getPassword
	
	
}//FilePerson
