/*
 *   Copyright 2011 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.brutex.xservices.ws;

import java.math.BigInteger;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.datatype.XMLGregorianCalendar;

import net.brutex.xservices.types.DateFormatType;
import net.brutex.xservices.types.DateInfoExtendedType;
import net.brutex.xservices.types.DateInfoType;
import net.brutex.xservices.types.DateTimeUnits;
import net.brutex.xservices.types.TimeZoneType;
import net.brutex.xservices.util.BrutexNamespaces;


import org.apache.cxf.annotations.WSDLDocumentation;
import org.apache.cxf.annotations.WSDLDocumentationCollection;

/**
 * Date and time related services.
 * @author Brian Rosenberger
 *
 */
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES)
@WSDLDocumentationCollection(
    {
    	@WSDLDocumentation(value = BrutexNamespaces.BRUTEX_COPYRIGHT, placement = WSDLDocumentation.Placement.TOP)
    }
)
public interface DateService {
	
	public static final String SERVICE_NAME = "DateService";
	
	final String OPERATION_GETDATE = "getDate";
	final String OPERATION_GETDATEEXTENDED = "getDateExtended";
	final String OPERATION_GETTIMESTAMP = "getTimestamp";
	final String OPERATION_GETTIMESTAMP2 = "getTimestamp2";
	final String OPERATION_GETINTIMEZONE = "getInTimezone";
	final String OPERATION_FORMATDATE = "formatDate";
	final String OPERATION_FORMATDATEADVANCED = "formatDateAdvanced";	
	final String OPERATION_PARSEDATE = "parseDate";
	final String OPERATION_PARSEDATEADVANCED = "parseDateAdvanced";
	final String OPERATION_DATETIMEDIFF = "dateTimeDiff";
	final String OPERATION_DATETIMEDIFF2 = "dateTimeDiff2";
	final String OPERATION_DATEADD = "dateAdd";
	final String OPERATION_GETTIMEZONES = "getTimezones";
	final String PARAM_TIMEZONE = "timezone";
	final String PARAM_DATETIME = "datetime";
	final String PARAM_FORMAT = "format";
	final String PARAM_UNIT = "unit";
	
	/**
	 * Get current date and time.
	 * 
	 * @return Current date and time.
	 * @throws XServicesFault 
	 */
	@WebMethod(operationName=OPERATION_GETDATE)
	@WSDLDocumentation(value="Get current date and time.")
	public abstract DateInfoType getDate() 
			throws XServicesFault;
	
	/**
	 * Get current date and time (extended version).
	 * 
	 * @return Current date and time.
	 * @throws XServicesFault 
	 */
	@WebMethod(operationName=OPERATION_GETDATEEXTENDED)
	@WSDLDocumentation(value="Get current date and time in different formats.")
	public abstract DateInfoExtendedType getDateExtended() 
			throws XServicesFault;
	
	/**
	 * Get milliseconds since 01.01.1970.
	 * 
	 * @return timestamp milliseconds
	 */
	@WebMethod(operationName=OPERATION_GETTIMESTAMP)
	@WSDLDocumentation(value="Get milliseconds since 01.01.1970 (Unix timestap).")
	public abstract BigInteger getTimestamp();

	/**
	 * Get seconds since 01.01.1970.
	 * 
	 * @return timestamp seconds
	 */
	@WebMethod(operationName=OPERATION_GETTIMESTAMP2)
	@WSDLDocumentation(value="Get seconds since 01.01.1970 (Unix timestap).")
	public abstract BigInteger getTimestamp2();
	
	/**
	 * Display a date time with a different time zone. 
	 * Changes representation only (no conversion).
	 * 
	 * @param cal date time.
	 * @param timezone time zone
	 * @return date time
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_GETINTIMEZONE)
	public abstract String getInTimezone(
			@WebParam(name=PARAM_DATETIME) @XmlElement(required=true) Date cal,
			@WebParam(name=PARAM_TIMEZONE) @XmlElement(required=true) String timezone) throws XServicesFault;
	
	/**
	 * Formats a date with pre-defined patterns.
	 * 
	 * @param cal date time to be formatted in ISO8601
	 * @param format Pattern to be used for date formating
	 * @return formatted date/time string
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_FORMATDATE)
	public abstract String formatDate(
			@WebParam(name=PARAM_DATETIME) @XmlElement(required=true) Date cal,
			@WebParam(name=PARAM_FORMAT) @XmlElement(required=true) DateFormatType format) throws XServicesFault;
	
	@WebMethod(operationName=OPERATION_GETTIMEZONES)
	public abstract List<TimeZoneType> getTimezones() throws XServicesFault;
	
	/**
	 * Formats a date with a free form pattern.
	 * Uses SimpleDateFormat patterns
	 * The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

    Letter	Date or Time Component	Presentation	Examples
    G	Era designator	Text	AD
    y	Year	Year	1996; 96
    M	Month in year	Month	July; Jul; 07
    w	Week in year	Number	27
    W	Week in month	Number	2
    D	Day in year	Number	189
    d	Day in month	Number	10
    F	Day of week in month	Number	2
    E	Day in week	Text	Tuesday; Tue
    a	Am/pm marker	Text	PM
    H	Hour in day (0-23)	Number	0
    k	Hour in day (1-24)	Number	24
    K	Hour in am/pm (0-11)	Number	0
    h	Hour in am/pm (1-12)	Number	12
    m	Minute in hour	Number	30
    s	Second in minute	Number	55
    S	Millisecond	Number	978
    z	Time zone	General time zone	Pacific Standard Time; PST; GMT-08:00
    Z	Time zone	RFC 822 time zone	-0800
	 * @param cal Date time to be formatted
	 * @param format Format string
	 * @return Date time formatted according to format string
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_FORMATDATEADVANCED)
	public abstract String formatDateAdvanced(
			@WebParam(name=PARAM_DATETIME) @XmlElement(required=true) Date cal,
			@WebParam(name=PARAM_FORMAT) @XmlElement(required=true) String format) throws XServicesFault;
	
	/**
	 * Converts a string into date using pre-defined date formats.
	 * 
	 * @param s Date/ time as string
	 * @param format date format
	 * @param timezone timezone
	 * @return XML Date
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_PARSEDATE)
	@WSDLDocumentation(value="Converts a string into date using pre-defined date formats.")
	public abstract Date parseDate(
			@WebParam(name=PARAM_DATETIME) @XmlElement(required=true) String s,
			@WebParam(name=PARAM_FORMAT) @XmlElement(required=true) DateFormatType format,
			@WebParam(name=PARAM_TIMEZONE) String timezone) throws XServicesFault;
	
	/**
	 * Converts a string into date using any format.
	 * @param s date/ time as string
	 * @param format date format
	 * @param timezone timezone
	 * @return XML Date
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_PARSEDATEADVANCED)
	public abstract GregorianCalendar parseDateAdvanced(
			@WebParam(name=PARAM_DATETIME) @XmlElement(required=true) String s,
			@WebParam(name=PARAM_FORMAT) @XmlElement(required=true) String format,
			@WebParam(name=PARAM_TIMEZONE) String timezone) throws XServicesFault;
	
	/**
	 * Calculate elapsed time between two dates.
	 * @param fromCal First date.
	 * @param toCal Second date.
	 * @return Elapsed time in milliseconds
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_DATETIMEDIFF)
	public abstract BigInteger dateTimeDiff(
			@WebParam(name="fromDateTime") @XmlElement(required=true) Date fromCal,
			@WebParam(name="toDateTime") @XmlElement(required=true) Date toCal) throws XServicesFault;
	
	/**
	 * Fully elapsed units between two dates.
	 * 4:15:10-4:15:55 in minutes = 0 and in seconds = 45
	 * 
	 * @param fromCal
	 * @param toCal
	 * @param unit
	 * @return Date/time difference in unit
	 * @throws XServicesFault
	 */
	@WebMethod(operationName=OPERATION_DATETIMEDIFF2)
	@WSDLDocumentation(value="Get elapsed time between to dates.")
	public abstract BigInteger dateTimeDiff2(
			@WebParam(name="fromDateTime") @XmlElement(required=true) Date fromCal,
			@WebParam(name="toDateTime") @XmlElement(required=true) Date toCal,
			@WebParam(name=PARAM_UNIT) DateTimeUnits unit) throws XServicesFault;
	
	/**
	 * Add or subtract a time span from a date.
	 * 
	 * @param cal The initial date.
	 * @param value The amount to add.
	 * @param unit The unit the amount is defined in.
	 * @return New date and time.
	 * @throws XServicesFault
	 * 
	 */
	@WebMethod(operationName=OPERATION_DATEADD)
	@WSDLDocumentation(value="Add or substract a time span from a date.")
	public abstract GregorianCalendar dateAdd(
			@WebParam(name=PARAM_DATETIME) @XmlElement(required=true) GregorianCalendar cal,
			@WebParam(name="value") @XmlElement(required=true) BigInteger value,
			@WebParam(name=PARAM_UNIT) @XmlElement(required=true) DateTimeUnits unit) throws XServicesFault;
	
}
