/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 11/12/2018 : SCA : V2.2.5
 *  Creation 
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.common;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.tlse.irap.pt.client.services.ServiceFactory;
import fr.tlse.irap.shock.Shock;
import fr.tlse.irap.shock.ShockWrapper;

/* 
 * Positions of shocks are updated when time is changed.
 * @author frup03304
 * @version
 */
public class ShockCollection {

	protected static final Logger log = LoggerFactory.getLogger(ShockCollection.class);

	private static ShockWrapper shockWrapper;

	private static Date startDate = new Date();
	private static Date endDate = new Date();
	private static long PERIOD_DAYS = 30;
	
	/**
	 * Request Shocks
	 * @param dStart
	 */
	public static void requestShocks(Date dStart) {
		//log.debug("requestShocks for date = " + dStart + " and start date = " + startDate);

		if (!dStart.equals(startDate)) {
			startDate.setTime(dStart.getTime());
			endDate.setTime(dStart.getTime() + PERIOD_DAYS * 86400000);
			if (shockWrapper != null) {
				if (!shockWrapper.getShocks().isEmpty()) {
					shockWrapper.getShocks().clear();
				}
			}
			//log.debug("call shock ws from " + startDate + " and " + endDate);
			shockWrapper = ServiceFactory.getShockService().fetchShocks(startDate, endDate);
		}
	}

	public static List<Shock> getShocksFrom(Date dStart) {
		Date dEnd = new Date();
		dEnd.setTime(dStart.getTime() + PERIOD_DAYS * 86400000);
		return ServiceFactory.getShockService().fetchShocks(dStart, dEnd).getShocks();
	}

	public static Shock getShockNearDate(Date date) {
		if (shockWrapper != null) {
			return shockWrapper.getNearestFromTime(date);
		}
		return null;
	}

	public static ShockWrapper getShockWrapper() {
		return shockWrapper;
	}

	/**
	 * @return the shocks
	 */
	public static List<Shock> getShocks() {
		return shockWrapper.getShocks();
	}

}
