package soa.jaxrslabs.booktrainwebservicerestexercice1;

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

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("trains")
public class TrainRessource {

	@GET
	@Produces(value = MediaType.TEXT_XML)
	public List<Train> getTrains() {
		System.out.println("getTrains");
		return BookTrainBD.getTrains();
	}

	@GET
	@Produces(value = MediaType.TEXT_XML)
	@Path("{id}")
	public Train getTrain(@PathParam("id") String numTrain) {
		System.out.println("getTrain");
		for (Train currentTrain : BookTrainBD.getTrains()) {
			if (currentTrain.getNumTrain().equals(numTrain)) {
				return currentTrain;
			}
		}
		return null;
	}

	@GET
	@Produces(value = MediaType.TEXT_XML)
	@Path("search")
	public List<Train> searchTrainsByCriteria(
			@QueryParam("departure") String departure,
			@QueryParam("arrival") String arrival,
			@QueryParam("departureHour") int departureHour) {
		System.out.println("searchTrainsByCriteria");
		List<Train> foundTrains = new ArrayList<Train>();
		for (Train currentTrain : BookTrainBD.getTrains()) {
			if (currentTrain.getVilleDepart().equals(departure)
					&& currentTrain.getVilleArrivee().equals(arrival)
					&& currentTrain.getHeureDepart() == departureHour) {
				foundTrains.add(currentTrain);
			}
		}
		return foundTrains;
	}
	
	@Path("booktrains")
	public BookTrainRessource getBookTrainRessource() {
		return new BookTrainRessource();
	}
}
