/*
 * Copyright (C) 2007 Helge Hess <helge.hess@opengroupware.org>
 * 
 * This file is part of JOPE.
 * 
 * JOPE is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later version.
 * 
 * JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with JOPE; see the file COPYING. If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.opengroupware.jope.foundation;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
 * UDate
 * 
 * Helper methods for the Date/Calendar objects.
 */
public class UDate extends NSObject {
  protected static Log log = LogFactory.getLog("UDate");

  private UDate() { } /* do not allow construction */
  
  /* dealing with time reference points for calendars */

  public static Date beginningOfWeekAsDateInLocale(Date _date, Locale _locale) {
    Calendar cal = Calendar.getInstance(_locale);
    if (_date != null) cal.setTime(_date);
    return UDate.beginningOfWeekAsDate(cal);
  }
  
  public static Date beginningOfWeekAsDate(Calendar _calendar) {
    return UDate.beginningOfWeekAsCalendar(_calendar).getTime();
  }
  
  public static Calendar beginningOfWeekAsCalendar(Calendar _calendar) {
    if (_calendar == null) {
      /* not recommended as the timezone is most likely incorrect */
      _calendar = Calendar.getInstance();
    }
    else
      _calendar = (Calendar)_calendar.clone();
    
    _calendar.set(Calendar.DAY_OF_WEEK, _calendar.getFirstDayOfWeek());
    _calendar.set(Calendar.HOUR_OF_DAY, 0);
    _calendar.set(Calendar.MINUTE,      0);
    _calendar.set(Calendar.SECOND,      0);
    _calendar.set(Calendar.MILLISECOND, 0);
    return _calendar;
  }
  
  public static Calendar beginningOfMonthAsCalendar(Calendar _calendar) {
    if (_calendar == null) {
      /* not recommended as the timezone is most likely incorrect */
      _calendar = Calendar.getInstance();
    }
    
    _calendar.set(Calendar.DAY_OF_MONTH,
        _calendar.getMinimum(Calendar.DAY_OF_MONTH));
    _calendar.set(Calendar.HOUR_OF_DAY,  0);
    _calendar.set(Calendar.MINUTE,      0);
    _calendar.set(Calendar.SECOND,      0);
    _calendar.set(Calendar.MILLISECOND, 0);
    return _calendar;
  }
  
  /* adding */
  
  public static Date dateByAdding
    (Date _date, int _years, int _months, int _days,
     int _hours, int _mins, int _secs,
     TimeZone _tz)
  {
    if (_date == null) return null;
    
    Calendar cal = _tz != null
      ? Calendar.getInstance(_tz)
      : Calendar.getInstance();
      
    return UDate.calendarByAdding
      (cal, _years, _months, _days, _hours, _mins, _secs).getTime();
  }

  public static Calendar calendarByAdding
    (Calendar _cal, int _years, int _months, int _days,
     int _hours, int _mins, int _secs)
  {
    if (_cal == null) return null;
    
    Calendar cal = (Calendar)_cal.clone();
    
    if (_years  != 0) cal.add(Calendar.YEAR,  _years);
    if (_months != 0) cal.add(Calendar.MONTH, _months);
    if (_days   != 0) cal.add(Calendar.DAY_OF_MONTH, _days);
    if (_hours  != 0) cal.add(Calendar.HOUR,   _hours);
    if (_mins   != 0) cal.add(Calendar.MINUTE, _mins);
    if (_secs   != 0) cal.add(Calendar.SECOND, _secs);
    
    return cal;
  }

  public static Date dateByAdding
    (Calendar _cal, int _years, int _months, int _days,
     int _hours, int _mins, int _secs)
  {
    return _cal != null ? UDate.calendarByAdding
      (_cal, _years, _months, _days, _hours, _mins, _secs).getTime() : null;
  }

}
