package be.ac.ulb.code.wit.resources;

import java.util.HashMap;
import java.util.Map;

import org.apache.abdera.i18n.templates.Template;

/**
 * Definition of the paths used by the service.
 * @author fpicalausa
 *
 */
public class ServicePaths {
	private ServicePaths() {}
	
	/**
	 * The root uri of the service
	 */
	public static final String    BASE_URI = "http://localhost:8080";
	
	public static final String SERVICEDOC = 	"/";
	public static final String CONTACTS = 		"/contacts";
	public static final String CONTACT = 		CONTACTS + "/{contactid}";
	public static final String GROUPS = 		"/groups";
	public static final String GROUP = 			GROUPS + "/{group}";
	public static final String GROUP_CONTACTS = 	GROUP + "/contacts";
	public static final String GROUP_CONTACT = 	GROUP_CONTACTS + "/{contactid}";
	
	/**
	 * Given a local path, compute the corresponding URI.
	 * @param path The local path of the service, as defined by constants in {@link ServicePaths}.
	 * @return The full URI corresponding to the local path.
	 */
	public static String getFullPath(String path) {
		return BASE_URI + path;
	}
	
	/**
	 * Compute an URI given a local path template
	 * @param path The local path template
	 * @param args The pairs of key/values to be injected in the template. 
	 * 				Each odd argument of this list is a key, while the next
	 * 				(even) argument is the corresponding value.
	 * @return A String representation of the URI corresponding to the local path,
	 * 			with template variables assigned to the given values.
	 */
	public static String getURI(final String path, final String... args) {
		final Map<String, Object> argsmap = new HashMap<String, Object>();
		String key = null;
		boolean parseKey = true;
		
		for (String arg:args) {
			if (parseKey) {
				key = arg;
			}
			else {
				argsmap.put(key, arg);
			}
			parseKey ^= true; // Equivalent to parseKey = !parseKey, but faster
		}
		
		return new Template(getFullPath(path)).expand(argsmap);
	}
}
