import java.util.*;
import java.rmi.*;


public class PIMCollection extends ArrayList {

  public Collection getNotes() {
	return(getPIMTypes("PIMNote"));
  }

  public Collection getNotes(String owner) {
	return(getPIMTypes("PIMNote",owner));
  }

  public Collection getTodos() {
	return(getPIMTypes("PIMTodo"));
  }

  public Collection getTodos(String owner) {
	return(getPIMTypes("PIMTodo",owner));
  }

  public Collection getAppointments() {
	return(getPIMTypes("PIMAppointment"));
  }

  public Collection getAppointments(String owner) {
	return(getPIMTypes("PIMAppointment",owner));
  }

  public Collection getContacts() {
	return(getPIMTypes("PIMContact"));
  }

  public Collection getContacts(String owner) {
	return(getPIMTypes("PIMContact", owner));
  }

  // all public (shared) items
  public PIMCollection getAll() {
	PIMCollection p = new PIMCollection();
	for (int i=0;i<size();i++) {
	  if ( ((PIMEntity) get(i)).getShared()) {
		p.add(get(i));
	  }
	}
	return(p);
  }

  // all items with the specified owner
  public PIMCollection getAllByOwner(String owner) {
	PIMCollection p = new PIMCollection();
	for (int i=0;i<size();i++) {
	  if ( ((PIMEntity) get(i)).getOwner().equals(owner)) {
		p.add(get(i));
	  }
	}
	return(p);
  }

  /**
   * method used internally to compare dates. Here we consider dates
   * to be matching if the day, month and year match (we don't care about
   * the time).
   * @param d1,d2 two Date objects to compare
   * @return true if both dates represent the same day.
   */

  boolean sameDate(Date d1, Date d2) {
	Calendar c1 = Calendar.getInstance();
	c1.setTime(d1);
	Calendar c2 = Calendar.getInstance();
	c2.setTime(d2);

	return( (c1.get(Calendar.DATE) == c2.get(Calendar.DATE)) &&
			(c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)) &&
			(c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)));
  }


  /** determines whether a PIMENtity is a dateable entity or not.
   */
  boolean isDateable(PIMEntity p) {
	return( p instanceof PIMDateable);
  }


  /** get all shared items that have a date that matches the specified date.
   */


  public PIMCollection getItemsForDate(Date d) {
	PIMCollection p = new PIMCollection();

	for (int i=0;i<size();i++) {
	  PIMEntity pe = (PIMEntity) get(i);
	  if (pe.getShared() && isDateable(pe) && sameDate(((PIMDateable)pe).getDate(),d)) {
		p.add(get(i));
	  }
	}
	return p;
  }

  /** get all items that have a specific date and owner
   */


  public PIMCollection getItemsForDate(Date d, String owner) {
	PIMCollection p = new PIMCollection();

	for (int i=0;i<size();i++) {
	  PIMEntity pe = (PIMEntity) get(i);
	  if (pe.getShared() && isDateable(pe) && 
		  pe.getOwner().equals(owner) && 
		  sameDate(((PIMDateable)pe).getDate(),d)) {
		p.add(get(i));
	  }
	}
	return p;
  }

  /** used to create collection of public (shared) items 
   */

  Collection getPIMTypes(String classname) {
	ArrayList x = new ArrayList();

	for (int i=0;i<size();i++) {
	  PIMEntity p = (PIMEntity) get(i);
	  if ((isPIMType(p,classname)) && ( p.getShared())) {
		x.add(get(i));
	  }
	}
	return(x);
  }

  /** creates a collection of all items found of a specific type
	  and with the specified owner
  */

  Collection getPIMTypes(String classname, String owner) {
	ArrayList x = new ArrayList();

	for (int i=0;i<size();i++) {
	  PIMEntity pe = (PIMEntity) get(i);
	  if ( (isPIMType(pe,classname)) && (pe.getOwner().equals(owner))) {
		x.add(pe);
	  }
	}
	return(x);
  }



  boolean isPIMType(PIMEntity p, String classname) {
	return( p.getClass().getName().equals(classname));
  }

}

