//
// https://www.ramecera.it/myscrewdriver.html
// e-mail: screwdriver@ramecera.it
//
/* This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html */
package it.ramecera.screwdriver.component;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.ExactException;
import it.ramecera.screwdriver.CandyScrew;
import it.ramecera.screwdriver.Markup;
import it.ramecera.screwdriver.Skill;
import it.ramecera.screwdriver.Train;

/** @author M. Maglioni */
final public class Navigation_jsp extends FileComponent {

	/** @param skill application main data
	 * @param snippetG name of the file containing the snippet */
	public Navigation_jsp(Skill skill, String snippetG) {
		super();
		this.skill = skill;
		this.snippetG = snippetG;
	}

	/** @throws ExactException exception specified by a numeric code */
	@Override
	public void synthesis(HashMap<String, Markup> markupE,
			List<Train> trainE, CandyScrew candyScrew)

			throws FileNotFoundException, IOException, ExactException {

		producedMaterial = "";
		producedMaterial += "<%@ page language=\"java\" "
				+ "pageEncoding=\"UTF-8\" contentType=\"text/html; "
				+ "charset=UTF-8\" %>" + Candies.LF;
		if (skill.springSecurity) {
			producedMaterial += "<%@ taglib uri=\"http://www.springframework.org"
					+ "/security/tags\" prefix=\"sec\"%>" + Candies.LF;
		}
		producedMaterial += "<%@ taglib prefix=\"c\" uri=\"http://java.sun."
				+ "com/jsp/jstl/core\" %>" + Candies.LF;
		producedMaterial += Candies.TABS
				+ "<div id=\"nav-bar\" class=\"navbar"
				+ (menuKit.navbarFixedTop ? " navbar-fixed-top" : "")
				+ "\">" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "<div class=\"navbar-inner\">" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "<div class=\"container\">" + Candies.LF;

		/* in the couples array, at least the first couple is needed,
		 * that is the brand or home */
		if (menuKit.clicE.size() == 0) {
			ExactException ee = new ExactException(
					"The individual elements of the menu description"
							+ " are not defined (blabel and burl markups in pair).");
			ee.code = 1100;
			ee.datum = ee.getMessage();
			ee.className = this.getClass().getName();
			throw ee;
		}
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ Candies.TABS + "<c:url var=\"place0\" value=\""
				+ menuKit.clicE.get(0)[1] + "\" />" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ Candies.TABS
				+ "<a class=\"brand\" href=\"${place0}\">"
				+ menuKit.clicE.get(0)[0] + "</a>" + Candies.LF;

		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ Candies.TABS + "<div class=\"nav-collapse\">"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ Candies.TABS + Candies.TABS + "<ul class=\"nav\">"
				+ Candies.LF;

		/* ordinary couples */
		for (int i = 1; i < menuKit.clicE.size(); i++) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "<c:url var=\"place" + i
					+ "\" value=\"" + menuKit.clicE.get(i)[1] + "\" />"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "<li><a id=\"name" + i
					+ "\" href=\"${place" + i + "}\">" + menuKit.clicE
							.get(i)[0] + "</a></li>" + Candies.LF;
		}
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ Candies.TABS + Candies.TABS + "</ul>" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ Candies.TABS + "</div>" + Candies.LF;
		if (skill.hasUseridPassword) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<div id=\"nav-account\" class=\"nav-collapse pull-right\">"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "<ul class=\"nav\">" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "<sec:authorize access=\"authenticated\" var=\"authenticated\"/>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "<c:choose>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<c:when test=\"${authenticated}\">" + Candies.LF;
			if (skill.authenticationMode.equals("trivial")) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ "<li id=\"greeting\"><div>Welcome \"<sec:authentication property=\"name\" />\"</div></li>"
						+ Candies.LF;
			} else {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ "<li id=\"greeting\"><div>Welcome "
						+ "<sec:authentication property=\"principal."
						+ skill.welcome + "\"" + " /></div></li>"
						+ Candies.LF;
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "<c:url var=\"logoutUrl\" value=\"/logout\"/>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "<li><a id=\"navLogoutLink\" href=\"${logoutUrl}\">Logout</a></li>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "</c:when>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<c:otherwise>"
					+ Candies.LF;
			if (skill.signup) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ "<c:url var=\"signupUrl\" value=\"/signup/form\"/>"
						+ Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS + Candies.TABS
						+ "<li><a id=\"navSignupLink\" href=\"${signupUrl}\">Signup</a></li>"
						+ Candies.LF;
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "<c:url var=\"loginUrl\" value=\"/login/form\"/>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "<li><a id=\"navLoginLink\" href=\"${loginUrl}\">Login</a></li>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "</c:otherwise>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "</c:choose>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ " </ul>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "</div>"
					+ Candies.LF;
		}
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "</div>" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "</div>"
				+ Candies.LF;
		producedMaterial += Candies.TABS + "</div>" + Candies.LF;
		producedMaterial += Candies.TABS
				+ "<div class=\"nav-bar\">&nbsp;</div>" + Candies.LF;

	}

	/* written only to comply with the abstract class FileComponent */
	@SuppressWarnings("javadoc")
	@Override
	/** @param markupE the list of markups
	 * @param skill together with the characteristics of the application
	 * @param trash file where the deleted files are moved
	 * @throws ExactException exception specified by a numeric code */
	public void directory(HashMap<String, Markup> markupE)

			throws ExactException {

	}
}
