/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.fit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FitCatalog {

	private List<Fit> fits;

	public FitCatalog() {
		fits = new ArrayList<Fit>();
	}

	public FitCatalog(List<Fit> fits) {
		this.fits = fits;
	}

	@XmlElement(name = "fits")
	public List<Fit> getFits() {
		return fits;
	}

	public void setFits(List<Fit> fits) {
		this.fits = fits;
	}

	public Fit getNearestFromTime(Calendar calendar) {

		Fit previous = (fits.isEmpty() ? null : fits.get(0));
		Fit next = null;

		for (Fit fit : this.fits) {
			if (fit.getSunTime().after(calendar)) {
				next = fit;
				break;
			}
			previous = fit;
		}

		if (next == null)
			return previous;
		else {
			Long deltaPrevious = Math.abs(previous.getSunTime()
					.getTimeInMillis() - calendar.getTimeInMillis());
			Long deltaNext = Math.abs(next.getSunTime().getTimeInMillis()
					- calendar.getTimeInMillis());
			return (deltaPrevious < deltaNext ? previous : next);
		}
	}
}
