/*
 * 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@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.content;

import java.text.*;
import java.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.VeniceNamespaces;

public class DateFormatterTopHalf
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  public static final String NAMESPACE = VeniceNamespaces.SESSION_INFO_NAMESPACE;
  public static final String PROPERTY = "date.formatter.bh";

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

  private DateFormatterBottomHalf m_bh = null;  // bottom half we get our data from
  private DateFormat m_display_date = null;     // format to use for displaying dates
  private DateFormat m_activity_time = null;    // format to use for activity string times

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

  public DateFormatterTopHalf(ServiceProvider sp)
  {
    try
    { // get the SessionInfoProvider and the SessionInfo from the services
      SessionInfoProvider sip = (SessionInfoProvider)(sp.queryService(SessionInfoProvider.class));
      SessionInfo session = sip.getSessionInfo();

      // now get the bottom half!
      m_bh = (DateFormatterBottomHalf)(session.getObject(NAMESPACE,PROPERTY));

    } // end try
    catch (NoSuchServiceException e)
    { // ignore this exception
    } // end catch
    catch (NoSuchObjectException e)
    { // ignore this exception
    } // end catch

    if (m_bh==null)  // default this!
      m_bh = new DefaultDateFormatterBottomHalf(sp);

  } // end constructor

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

  public String formatDate(java.util.Date date)
  {
    synchronized (this)
    { // create the display date formatter if we don't yet have it
      if (m_display_date==null)
      { // create the display date formatter
	m_display_date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,m_bh.getLocale());
	m_display_date.setTimeZone(m_bh.getTimeZone());

      } // end if

    } // end synchronized block

    return m_display_date.format(date);

  } // end formatDate

  public String getActivityString(Date date)
  {
    ResourceBundle b = ResourceBundle.getBundle("com.silverwrist.venice.content.ContentMessages",
						Locale.getDefault());
    if (date==null)
      return b.getString("activity.never");  // safeguard

    // Set up the two calendars we'll use for comparison.
    Calendar c_last = new GregorianCalendar(m_bh.getTimeZone(),m_bh.getLocale());
    c_last.setTime(date);
    Calendar c_now = new GregorianCalendar(m_bh.getTimeZone(),m_bh.getLocale());

    // Determine the exact number of days in the delta.
    int delta_days = 0;
    while (   (c_last.get(Calendar.YEAR)!=c_now.get(Calendar.YEAR))
	   || (c_last.get(Calendar.DAY_OF_YEAR)!=c_now.get(Calendar.DAY_OF_YEAR)))
    { // advance until we're pointing at the same year and the same day of the year
      delta_days++;
      c_last.add(Calendar.DAY_OF_YEAR,1);

    } // end while

    // For today or yesterday, we want to paste the time in as well.
    String s = null;
    if (delta_days<=1)
    { // set up to convert the time into a string
      synchronized (this)
      { // make sure we don't accidentally create the object twice
	if (m_activity_time==null)
	{ // get the "activity" time formatter
	  m_activity_time = DateFormat.getTimeInstance(DateFormat.MEDIUM,m_bh.getLocale());
	  m_activity_time.setTimeZone(m_bh.getTimeZone());

	} // end if

      } // end synchronized block

      s = m_activity_time.format(date);

    } // end if

    // Return the actual activity string.
    String pattern = null;
    Object[] args = new Object[1];
    if (delta_days==0)
    { // "Today, 12:34:56"
      pattern = b.getString("activity.today");
      args[0] = s;

    } // end if
    else if (delta_days==1)
    { // "Yesterday, 12:34:56"
      pattern = b.getString("activity.yesterday");
      args[0] = s;

    } // end else if
    else
    { // "N days ago"
      pattern = b.getString("activity.daysago");
      args[0] = String.valueOf(delta_days);

    } // end else

    return MessageFormat.format(pattern,args);

  } // end getActivityString

} // end class DateFormatterTopHalf
