/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.util;

import java.io.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.except.*;

public class VCardEmail
{
  /*--------------------------------------------------------------------------------
   * Predicate class to use for searches
   *--------------------------------------------------------------------------------
   */

  static abstract class Predicate
  {
    public abstract boolean test(VCardEmail e);

  } // end class Predicate

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private boolean home_email;
  private boolean work_email;
  private boolean internet_email;
  private boolean x400_email;
  private boolean preferred;
  private String address;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  VCardEmail(boolean home_email, boolean work_email, boolean internet_email, boolean x400_email, boolean preferred,
	     String address)
  {
    this.home_email = home_email;
    this.work_email = work_email;
    this.internet_email = internet_email;
    this.x400_email = x400_email;
    this.preferred = preferred;
    this.address = address;

  } // end constructor

  VCardEmail(Element elt)
  {
    DOMElementHelper h = new DOMElementHelper(elt);
    home_email = h.hasChildElement("HOME");
    work_email = h.hasChildElement("WORK");
    internet_email = h.hasChildElement("INTERNET");
    x400_email = h.hasChildElement("X400");
    preferred = h.hasChildElement("PREF");
    if (!internet_email && !x400_email)
      internet_email = true; // default
    address = VCard.cleanup(h.getSubElementText("USERID"));
    if (address==null)
      address = VCard.cleanup(h.getElementText());

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  final void exportXML(Writer xml_writer) throws IOException
  {
    xml_writer.write("<EMAIL>");
    if (home_email)
      xml_writer.write("<HOME/>");
    if (work_email)
      xml_writer.write("<WORK/>");
    if (internet_email)
      xml_writer.write("<INTERNET/>");
    if (x400_email)
      xml_writer.write("<X400/>");
    if (preferred)
      xml_writer.write("<PREF/>");
    if (!StringUtil.isStringEmpty(address))
      xml_writer.write("<USERID>" + address + "</USERID>");
    xml_writer.write("</EMAIL>\n");

  } // end exportXML

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  public boolean equals(Object o)
  {
    if ((o==null) || !(o instanceof VCardEmail))
      return false;
    VCardEmail other = (VCardEmail)o;
    if (   (home_email!=other.home_email) || (work_email!=other.work_email) || (internet_email!=other.internet_email)
	|| (x400_email!=other.x400_email) || (preferred!=other.preferred))
      return false;
    return StringUtil.areEqual(address,other.address);

  } // end equals

  public int hashCode()
  {
    int rc = 0;
    int mask = 0x40000000;
    if (home_email)
      rc |= mask;
    mask >>= 1;
    if (work_email)
      rc |= mask;
    mask >>= 1;
    if (internet_email)
      rc |= mask;
    mask >>= 1;
    if (x400_email)
      rc |= mask;
    mask >>= 1;
    if (preferred)
      rc |= mask;
    if (address!=null)
      rc ^= address.hashCode();
    return rc;

  } // end hashCode

  /*--------------------------------------------------------------------------------
   * External getters/setters
   *--------------------------------------------------------------------------------
   */

  public final boolean isHomeEmail()
  {
    return home_email;

  } // end isHomeEmail

  public final boolean isWorkEmail()
  {
    return work_email;

  } // end isHomeEmail

  public final boolean isInternetEmail()
  {
    return internet_email;

  } // end isInternetEmail

  public final boolean isX400Email()
  {
    return x400_email;

  } // end isX400Email

  public final boolean isPreferred()
  {
    return preferred;

  } // end isPreferred

  public final String getAddress()
  {
    return address;

  } // end getAddress

} // end class VCardEmail
