/*
 * 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 VCardPhone
{
  /*--------------------------------------------------------------------------------
   * Predicate class to use for searches
   *--------------------------------------------------------------------------------
   */

  static abstract class Predicate
  {
    public abstract boolean test(VCardPhone p);

  } // end class Predicate

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

  private boolean home_phone;
  private boolean work_phone;
  private boolean voice_phone;
  private boolean fax_phone;
  private boolean pager;
  private boolean message;
  private boolean cell_phone;
  private boolean video_phone;
  private boolean bbs_phone;
  private boolean modem_phone;
  private boolean isdn_phone;
  private boolean pcs_phone;
  private boolean preferred;
  private String number;

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

  VCardPhone(boolean home_phone, boolean work_phone, boolean voice_phone, boolean fax_phone, boolean pager,
	     boolean message, boolean cell_phone, boolean video_phone, boolean bbs_phone, boolean modem_phone,
	     boolean isdn_phone, boolean pcs_phone, boolean preferred, String number)
  {
    this.home_phone = home_phone;
    this.work_phone = work_phone;
    this.voice_phone = voice_phone;
    this.fax_phone = fax_phone;
    this.pager = pager;
    this.message = message;
    this.cell_phone = cell_phone;
    this.video_phone = video_phone;
    this.bbs_phone = bbs_phone;
    this.modem_phone = modem_phone;
    this.isdn_phone = isdn_phone;
    this.pcs_phone = pcs_phone;
    this.preferred = preferred;
    this.number = number;

  } // end constructor

  VCardPhone(Element elt)
  {
    DOMElementHelper h = new DOMElementHelper(elt);
    home_phone = h.hasChildElement("HOME");
    work_phone = h.hasChildElement("WORK");
    voice_phone = h.hasChildElement("VOICE");
    fax_phone = h.hasChildElement("FAX");
    pager = h.hasChildElement("PAGER");
    message = h.hasChildElement("MSG");
    cell_phone = h.hasChildElement("CELL");
    video_phone = h.hasChildElement("VIDEO");
    bbs_phone = h.hasChildElement("BBS");
    modem_phone = h.hasChildElement("MODEM");
    isdn_phone = h.hasChildElement("ISDN");
    pcs_phone = h.hasChildElement("PCS");
    preferred = h.hasChildElement("PREF");
    number = VCard.cleanup(h.getSubElementText("NUMBER"));
    if (number==null)
      number = VCard.cleanup(h.getElementText());

  } // end constructor

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

  final void exportXML(Writer xml_writer) throws IOException
  {
    xml_writer.write("<TEL>");
    if (home_phone)
      xml_writer.write("<HOME/>");
    if (work_phone)
      xml_writer.write("<WORK/>");
    if (voice_phone)
      xml_writer.write("<VOICE/>");
    if (fax_phone)
      xml_writer.write("<FAX/>");
    if (pager)
      xml_writer.write("<PAGER/>");
    if (message)
      xml_writer.write("<MSG/>");
    if (cell_phone)
      xml_writer.write("<CELL/>");
    if (video_phone)
      xml_writer.write("<VIDEO/>");
    if (bbs_phone)
      xml_writer.write("<BBS/>");
    if (modem_phone)
      xml_writer.write("<MODEM/>");
    if (isdn_phone)
      xml_writer.write("<ISDN/>");
    if (pcs_phone)
      xml_writer.write("<PCS/>");
    if (preferred)
      xml_writer.write("<PREF/>");
    if (!StringUtil.isStringEmpty(number))
      xml_writer.write("<NUMBER>" + number + "</NUMBER>");
    xml_writer.write("</TEL>\n");

  } // end exportXML

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

  public boolean equals(Object o)
  {
    if ((o==null) || !(o instanceof VCardPhone))
      return false;
    VCardPhone other = (VCardPhone)o;
    if (   (home_phone!=other.home_phone) || (work_phone!=other.work_phone) || (voice_phone!=other.voice_phone)
        || (fax_phone!=other.fax_phone) || (pager!=other.pager) || (message!=other.message)
        || (cell_phone!=other.cell_phone) || (video_phone!=other.video_phone) || (bbs_phone!=other.bbs_phone)
        || (modem_phone!=other.modem_phone) || (isdn_phone!=other.isdn_phone) || (pcs_phone!=other.pcs_phone)
        || (preferred==other.preferred))
      return false;
    return StringUtil.areEqual(number,other.number);

  } // end equals

  public int hashCode()
  {
    int rc = 0;
    int mask = 0x40000000;
    if (home_phone)
      rc |= mask;
    mask >>= 1;
    if (work_phone)
      rc |= mask;
    mask >>= 1;
    if (voice_phone)
      rc |= mask;
    mask >>= 1;
    if (fax_phone)
      rc |= mask;
    mask >>= 1;
    if (pager)
      rc |= mask;
    mask >>= 1;
    if (message)
      rc |= mask;
    mask >>= 1;
    if (cell_phone)
      rc |= mask;
    mask >>= 1;
    if (video_phone)
      rc |= mask;
    mask >>= 1;
    if (bbs_phone)
      rc |= mask;
    mask >>= 1;
    if (modem_phone)
      rc |= mask;
    mask >>= 1;
    if (isdn_phone)
      rc |= mask;
    mask >>= 1;
    if (pcs_phone)
      rc |= mask;
    mask >>= 1;
    if (preferred)
      rc |= mask;
    if (number!=null)
      rc ^= number.hashCode();
    return rc;

  } // end hashCode

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

  public final boolean isHomePhone()
  {
    return home_phone;

  } // end isHomePhone

  public final boolean isWorkPhone()
  {
    return work_phone;

  } // end isWorkPhone

  public final boolean isVoicePhone()
  {
    return voice_phone;

  } // end isVoicePhone

  public final boolean isFax()
  {
    return fax_phone;

  } // end isFax

  public final boolean isPager()
  {
    return pager;

  } // end isPager

  public final boolean isMessage()
  {
    return message;

  } // end isMessage

  public final boolean isCellPhone()
  {
    return cell_phone;

  } // end isCellPhone

  public final boolean isVideoPhone()
  {
    return video_phone;

  } // end isVideoPhone

  public final boolean isBBS()
  {
    return bbs_phone;

  } // end isBBS

  public final boolean isModemPhone()
  {
    return modem_phone;

  } // end isModemPhone

  public final boolean isISDNPhone()
  {
    return isdn_phone;

  } // end isISDNPhone

  public final boolean isPCSPhone()
  {
    return pcs_phone;

  } // end isPCSPhone

  public final boolean isPreferred()
  {
    return preferred;

  } // end isPreferred

  public final String getNumber()
  {
    return number;

  } // end getNumber

} // end class VCardPhone
