/*
 * 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) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.util;

import java.util.*;
import com.silverwrist.util.*;

public class BuildVCard
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String formatted_name = null;
  private String family_name = null;
  private String given_name = null;
  private String middle_name = null;
  private String prefix = null;
  private String suffix = null;
  private String nickname = null;
  private HashSet addresses = new HashSet();
  private HashSet phones = new HashSet();
  private HashSet email_addresses = new HashSet();
  private String mailer = null;
  private String timezone = null;
  private String title = null;
  private String role = null;
  private String orgname = null;
  private String note = null;
  private String sort_string = null;
  private String url = null;
  private java.sql.Date m_bday = null;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public BuildVCard()
  { // do nothing
  } // end constructor

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

  private final String realFormattedName()
  {
    if (!StringUtil.isStringEmpty(formatted_name))
      return formatted_name;
    StringBuffer buf = new StringBuffer();
    if (!StringUtil.isStringEmpty(prefix))
      buf.append(prefix).append(' ');
    if (!StringUtil.isStringEmpty(given_name))
      buf.append(given_name);
    if (!StringUtil.isStringEmpty(middle_name) || !StringUtil.isStringEmpty(family_name))
      buf.append(' ');
    if (!StringUtil.isStringEmpty(middle_name))
    { // figure out the middle name
      buf.append(middle_name);
      if (middle_name.length()==1)
	buf.append('.');
      if (!StringUtil.isStringEmpty(family_name))
	buf.append(' ');

    } // end if

    if (!StringUtil.isStringEmpty(family_name))
      buf.append(family_name);
    if (!StringUtil.isStringEmpty(suffix))
      buf.append(' ').append(suffix);
    String s = buf.toString().trim();
    if (s.length()==0)
      return null;
    return s;

  } // end realFormattedName

  /*--------------------------------------------------------------------------------
   * Public getters/setters
   *--------------------------------------------------------------------------------
   */

  public final String getFormattedName()
  {
    return formatted_name;

  } // end getFormattedName

  public final void setFormattedName(String s)
  {
    formatted_name = s;

  } // end setFormattedName

  public final String getFamilyName()
  {
    return family_name;

  } // end getFamilyName

  public final void setFamilyName(String s)
  {
    family_name = s;

  } // end setFamilyName

  public final String getGivenName()
  {
    return given_name;

  } // end getGivenName

  public final void setGivenName(String s)
  {
    given_name = s;

  } // end setGivenName

  public final String getMiddleName()
  {
    return middle_name;

  } // end getMiddleName

  public final void setMiddleName(String s)
  {
    middle_name = s;

  } // end setMiddleName

  public final String getPrefix()
  {
    return prefix;

  } // end getPrefix

  public final void setPrefix(String s)
  {
    prefix = s;

  } // end setPrefix

  public final String getSuffix()
  {
    return suffix;

  } // end getSuffix

  public final void setSuffix(String s)
  {
    suffix = s;

  } // end setSuffix

  public final String getNickname()
  {
    return nickname;

  } // end getNickname

  public final void setNickname(String s)
  {
    nickname = s;

  } // end setNickname

  public final String getMailer()
  {
    return mailer;

  } // end getMailer

  public final void setMailer(String s)
  {
    mailer = s;

  } // end setMailer

  public final String getTimeZone()
  {
    return timezone;

  } // end getTimeZone

  public final void setTimeZone(String s)
  {
    if ((s!=null) && !VCard.validateTZText(s))
      throw new IllegalArgumentException("not a valid timezone string");
    timezone = s;

  } // end setTimeZone

  public final void setTimeZone(TimeZone tz)
  {
    int ofs = tz.getRawOffset();
    if (ofs==0)
    { // we're dead-bang on Zulu time - recognize that fact
      timezone = "Z";
      return;

    } // end if

    StringBuffer buf = new StringBuffer();
    if (ofs<0)
    { // negative offset
      buf.append('-');
      ofs = -ofs;

    } // end if
    else // positive offset
      buf.append('+');

    ofs /= 60000; // convert to minutes
    int hrs = ofs / 60;  // get hours
    if (hrs>=10)
      buf.append(hrs);
    else
      buf.append('0').append(hrs);
    buf.append(':');
    ofs -= (hrs * 60);  // get minutes
    if (ofs>=10)
      buf.append(ofs);
    else
      buf.append('0').append(ofs);
    timezone = buf.toString();

  } // end setTimeZone

  public final String getTitle()
  {
    return title;

  } // end getTitle

  public final void setTitle(String s)
  {
    title = s;

  } // end setTitle

  public final String getRole()
  {
    return role;

  } // end getRole

  public final void setRole(String s)
  {
    role = s;

  } // end setRole

  public final String getOrganizationName()
  {
    return orgname;

  } // end getOrganizationName

  public final void setOrganizationName(String s)
  {
    orgname = s;

  } // end setOrganizationName

  public final String getNote()
  {
    return note;

  } // end getNode

  public final void setNote(String s)
  {
    note = s;

  } // end setNote

  public final String getSortString()
  {
    return sort_string;

  } // end getSortString

  public final void setSortString(String s)
  {
    sort_string = s;

  } // end sort_string

  public final String getURL()
  {
    return url;

  } // end getURL

  public final void setURL(String s)
  {
    url = s;

  } // end setURL

  public final java.sql.Date getBirthday()
  {
    return m_bday;

  } // end getBirthday

  public final void setBirthday(java.sql.Date d)
  {
    m_bday = d;

  } // end setBirthday

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public void reset()
  {
    formatted_name = null;
    family_name = null;
    given_name = null;
    middle_name = null;
    prefix = null;
    suffix = null;
    nickname = null;
    addresses.clear();
    phones.clear();
    email_addresses.clear();
    mailer = null;
    timezone = null;
    title = null;
    role = null;
    orgname = null;
    note = null;
    sort_string = null;
    url = null;
    m_bday = null;

  } // end reset

  public VCard create()
  {
    return new VCard(realFormattedName(),family_name,given_name,middle_name,prefix,suffix,nickname,addresses,phones,
		     email_addresses,mailer,timezone,title,role,orgname,note,sort_string,url,m_bday);

  } // end create

  public final List getAddresses()
  {
    if (addresses.isEmpty())
      return Collections.EMPTY_LIST;
    ArrayList rc = new ArrayList(addresses);
    rc.trimToSize();
    return Collections.unmodifiableList(rc);

  } // end getAddresses

  public final List getPhones()
  {
    if (phones.isEmpty())
      return Collections.EMPTY_LIST;
    ArrayList rc = new ArrayList(phones);
    rc.trimToSize();
    return Collections.unmodifiableList(rc);

  } // end getPhones

  public final List getEmailAddresses()
  {
    if (email_addresses.isEmpty())
      return Collections.EMPTY_LIST;
    ArrayList rc = new ArrayList(email_addresses);
    rc.trimToSize();
    return Collections.unmodifiableList(rc);

  } // end getEmailAddresses

  public final void addAddress(VCardAddress addr)
  {
    addresses.add(addr);

  } // end addAddress

  public final void removeAddress(VCardAddress addr)
  {
    addresses.remove(addr);

  } // end removeAddress

  public final void removeAllAddresses()
  {
    addresses.clear();

  } // end removeAllAddresses

  public final void addPhone(VCardPhone phone)
  {
    phones.add(phone);

  } // end addPhone

  public final void removePhone(VCardPhone phone)
  {
    phones.remove(phone);

  } // end removePhone

  public final void removeAllPhones()
  {
    phones.clear();

  } // end removeAllPhones

  public final void addEmailAddress(VCardEmail email)
  {
    email_addresses.add(email);

  } // end addEmailAddress

  public final void removeEmailAddress(VCardEmail email)
  {
    email_addresses.remove(email);

  } // end removeEmailAddress

  public final void removeAllEmailAddresses()
  {
    email_addresses.clear();

  } // end removeAllEmailAddresses

} // end class BuildVCard
