package fr.tlse.irap.flare;

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 FlareFluxWrapper {

	private List<FlareFlux> flares;

	public FlareFluxWrapper() {
		flares = new ArrayList<FlareFlux>();
	}

	public FlareFluxWrapper(List<FlareFlux> flares) {
		this.flares = flares;
	}

	@XmlElement
	public List<FlareFlux> getFlares() {
		return flares;
	}

	public void setFlares(List<FlareFlux> flares) {
		this.flares = flares;
	}

	public FlareFlux getNearestFromTime(Calendar calendar) {

		FlareFlux previous = (flares.isEmpty() ? null : flares.get(0));
		FlareFlux next = null;

		for (FlareFlux flareFlux : this.flares) {
			if (flareFlux.getEventTime().after(calendar)) {
				next = flareFlux;
				break;
			}
			previous = flareFlux;
		}

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

}
