/**
 * Daily.java
 * Created on Jun 9, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.timers;

import java.util.Date;
import java.util.List;

import com.tandbergtv.watchpoint.pmm.entities.RuleParameter;
import com.tandbergtv.watchpoint.pmm.entities.RuleParameterDataType;
import com.tandbergtv.watchpoint.pmm.entities.RuleTypeParameter;

/**
 * This class returns a result object which indicates that this is a periodic task that should be run
 * the given periodicity (= daily in msec).
 * It sets the start date time using the startdate and jobTime given. 
 * Time is optional. If not specified uses midnight.
 * 
 * @author spuranik
 */
public class Daily implements ITimeRepresentation {

	private static long INTERVAL = 1L * 24L * 60L * 60L * 1000L;
	private static String MIDNIGHT = "12:00 am";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.job.timers.ITimeRepresentation#representTime(java.lang.String,
	 *      java.util.List)
	 */
	public Object getTime(List<RuleTypeParameter> ruleTypeParams, List<RuleParameter> params,
			Date startDate) {
		String time = TimerHelper.getParameter(ruleTypeParams, params, RuleParameterDataType.TIME);

		// if no time is specified set it to midnight
		if (time == null || time.trim().length() < 0) {
			time = MIDNIGHT;
		}

		Date jobStartDate = TimerHelper.getStartDateTime(startDate, time);
		TimerResult result = new TimerResult(true, INTERVAL, jobStartDate);
		return result;
	}

}
