package com.github.onsdigital.api.taxonomy;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.core.Context;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import com.github.davidcarboni.ResourceUtils;
import com.github.davidcarboni.restolino.json.Serialiser;
import com.github.onsdigital.json.Data;
import com.github.onsdigital.json.TaxonomyNode;

public abstract class Taxonomy {

	@GET
	public void serveTemplate(@Context HttpServletRequest request,
			@Context HttpServletResponse response) throws IOException {

		// Ensures ResourceUtils gets the right classloader when running
		// reloadable in development:
		ResourceUtils.classLoaderClass = Taxonomy.class;
		URI uri = URI.create(request.getRequestURI());
		Data data = getNodeData(uri);
		try (InputStream html = selectTemplate(data)) {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF8");
			IOUtils.copy(html, response.getOutputStream());
		}
	}

	/**
	 * Accesses the data.json file for this node.
	 * 
	 * @param uri
	 * @return A {@link TaxonomyNode} representation.
	 * @throws IOException
	 */
	private Data getNodeData(URI uri) throws IOException {

		// Get the data for this node:
		String jsonResourceName = "/files" + join(uri.getPath(), "data.json");
		Data data;
		try (InputStream json = ResourceUtils.getStream(jsonResourceName)) {
			data = Serialiser.deserialise(json, Data.class);
		}

		return data;
	}

	private InputStream selectTemplate(Data data) throws IOException {

		// Select T2 or T3 depending on whether there are children:
		String templateResourceName;
		if (data.children == null || data.children.size() == 0) {
			// t3
			templateResourceName = "/files/t3.html";
		} else {
			// t2
			templateResourceName = "/files/t2.html";
		}
		return ResourceUtils.getStream(templateResourceName);
	}

	/**
	 * We could optimise this by using StringBuilder.
	 * 
	 * @param path
	 * @param file
	 * @return
	 */
	private static String join(String path, String file) {
		String result;
		if (!StringUtils.endsWith(path, "/")) {
			result = path + "/" + file;
		} else {
			result = path + file;
		}
		if (!StringUtils.startsWith(result, "/")) {
			result = "/" + result;
		}
		return result;
	}
}

@@ -949,51 +949,246 @@
 ;%0A%09%09
-try (InputStream html = selectTemplate(data
+String templateResourceName;%0A%09%09if (StringUtils.equals(data.level, %22t2%22))%0A%09%09%09templateResourceName = %22/files/t2.html%22;%0A%09%09else%0A%09%09%09templateResourceName = %22/files/t3.html%22;%0A%09%09try (InputStream html = ResourceUtils.getStream(templateResourceName
 )) %7B
@@ -1809,411 +1809,8 @@
 %09%7D%0A%0A
-%09private InputStream selectTemplate(Data data) throws IOException %7B%0A%0A%09%09// Select T2 or T3 depending on whether there are children:%0A%09%09String templateResourceName;%0A%09%09if (data.children == null %7C%7C data.children.size() == 0) %7B%0A%09%09%09// t3%0A%09%09%09templateResourceName = %22/files/t3.html%22;%0A%09%09%7D else %7B%0A%09%09%09// t2%0A%09%09%09templateResourceName = %22/files/t2.html%22;%0A%09%09%7D%0A%09%09return ResourceUtils.getStream(templateResourceName);%0A%09%7D%0A%0A
 %09/**

Tweak to serve templates based on the "level" property in data.json instead of fudging the selection.