package rest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

@Path("/")
@Produces("text/plain")
public class RestService {

	private static Logger LOGGER = Logger.getLogger("jai.jaxrs");
	
	private static SimpleDateFormat sdf= new SimpleDateFormat("MMddyyyy");
	
	private static String messageBienvenue="Bienvenue à l'espace ";
	
	private static String messagePlusieursPassages="De nouveau bienvenue à l'espace ";

	@Path("/badge/{id}/{espace}")
	@GET
	public String validerBadge(@PathParam("id") String id, @PathParam("espace") String espace, @Context ServletContext context) {
		LOGGER.info("validation du badge " + id + " a l'espace " + espace);
		Map<String,String> mapPassageMicrofolies = (Map<String, String>) context.getAttribute("MapPassageMicrofolies");
		String stringVerifPassage = sdf.format(new Date())+ " " + espace;
		String messageRetour=messageBienvenue + espace;
		if(mapPassageMicrofolies==null) {
			mapPassageMicrofolies=new HashMap<String,String>();
			mapPassageMicrofolies.put(id, stringVerifPassage);
		}
		else {
			String listPassage = mapPassageMicrofolies.get(id);
			if(listPassage==null) {
				mapPassageMicrofolies.put(id,new Date().toString() + " " + espace);
			} else {
				if(!listPassage.contains(stringVerifPassage)){
					mapPassageMicrofolies.put(id,listPassage);
				} else {
					messageRetour=messagePlusieursPassages + espace;
				}
			}
		}	
		context.setAttribute("MapPassageMicrofolies", mapPassageMicrofolies);
		return messageRetour;
	}

	@Path("freq/{espace}/{from}/{to}")
	@GET
	public String frequence(@PathParam("espace") String espace, @PathParam("from") Date from, @PathParam("to") Date to) {
		LOGGER.info("frequence de " + from + " a " + to + " espace " + espace);
		return "frequence de " + from + " a " + to + " espace " + espace;
	}

	@Path("profile/{id}")
	@GET
	public String profil(@PathParam("id")String id) {
		LOGGER.info("profil " +id);
		return "Profil de " + id;
	}

}
