/*
 *	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.*;

/** A specific rank, such as Scout, Tenderfoot, 2nd class, etc. */
public class FileRankObject extends Vector implements Identifiable, Rank, Serializable {
	
	public static final long serialVersionUID = 7267897963623266033L;

	String id = Utils.getGUID();
	String name = null;
	Date awarded = null;
	int numBadges = 0;
	int numRequiredBadges = 0;

	/** Constructor */
	public FileRankObject(String name) {
		this.name = name;
	}//FileRankObject
	
	/** Returns the unique id for this object */
	public String getID() {
		return id;
	}//getID
	
	/** Returns the name of this rank */
	public String getName() throws DataException {
		return name;
	}//getName
	
	/** Returns the name of this rank */
	public String toString() {
		return name;
	}//toString
	
	
	
	/** Returns whether all requirements are passed off for this rank */
	public boolean isCompleted() throws DataException {
		FileRequirement requirement = null;
		for (int i = 0; i < size(); i++) {
			requirement = (FileRequirement)elementAt(i);
			if (requirement.getPassed() == null) {
				return false;
			}//if
		}//for 
		return true;
	}//isCompleted
	
	/** Returns the number of requirements completed for this rank */
	public int getCompletedCount() throws DataException {
		FileRequirement requirement = null;
		int count = 0;
		for (int i = 0; i < size(); i++) {
			requirement = (FileRequirement)elementAt(i);
			if (requirement.getPassed() != null) {
				count++;
			}//if
		}//for 
		return count;
	}//getCOmpletedCount
	
	/** Returns the date the last item was passed off for this rank (which should always be the board of review),
			or null if the rank is not yet completed. */
	public Date getCompletedDate() throws DataException  {
		if (size() > 0) {
			FileRequirement requirement = (FileRequirement)lastElement();
			return requirement.getPassed();
		}//if
		return null;
	}//getCompletedDate
	
	/** Returns the date this rank was actually awarded to the scout, or null if the rank has not been awarded yet */
	public Date getAwarded() throws DataException  {
		return awarded;
	}//getAwarded
	
	/** Sets the date this rank was actually awarded */
	public void setAwarded(Date awarded)throws DataException  {
		this.awarded = awarded;
	}//steAwarded
	
	/** Returns the number of requirements */
	public int getRequirementCount() throws DataException {
		return size();
	}//getRequirementCount
	
	/** Gets the given requirement */
	public Requirement getRequirement(int index) throws DataException {
		if (index >= 0 && index < size()) {
			return (Requirement)elementAt(index);
		}//if
		return null;
	}//getRequirement
	
	/** Returns all of the requirements for this rank */
	public Enumeration getRequirements() throws DataException{
		return elements();
	}//getRequirements

	/** Writes my information to an XML buffer */
	public void writeXML(PrintWriter out) throws DataException {
		out.println("    <Rank id=\"" + id + "\">");
		out.println("      <Name>" + Utils.xml(name) + "</Name>");
		out.println("      <Awarded>" + Utils.xml(awarded) + "</Awarded>");
		for (Enumeration enum = elements(); enum.hasMoreElements(); ) {
			FileRequirement requirement = (FileRequirement)enum.nextElement();
			requirement.writeXML(out);
		}//for
		out.println("    </Rank>");
	}//writeXML

	/** Sets the number of required merit badges for this rank (not exported in interface) */
	public void setRequiredCount(int count) throws DataException {
		numRequiredBadges = count;
	}//setRequiredCount

	/** Returns the number of required merit badges for this rank */
	public int getRequiredCount() throws DataException {
		return numRequiredBadges;
	}//getRequiredCount
	
	/** Sets the total number of merit badges for this rank (not exported in interface) */
	public void setMeritBadgeCount(int count) throws DataException {
		numBadges = count;
	}//setMeritBadgeCount
	
	/** Returns the total number of merit badges for this rank */
	public int getMeritBadgeCount() throws DataException {
		return numBadges;
	}//getMeritBadgeCount

}//FileRankObject

