//
// 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.salad.everyday.Strings;
import it.ramecera.salad.everyday.Universal;
import it.ramecera.salad.file.Equipment;
import it.ramecera.screwdriver.CandyScrew;
import it.ramecera.screwdriver.Cst;
import it.ramecera.screwdriver.Markup;
import it.ramecera.screwdriver.Skill;
import it.ramecera.screwdriver.Train;
import it.ramecera.screwdriver.outfits.JspBeanElement;

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

	private String[][] buttonsCrud = { { "en", "Create", "Read",
			"Update", "Delete" }, { "it", "Crea", "Leggi", "Modifica",
					"Cancella" }, { "fr", "Créer", "Lire", "Modifier",
							"Supprimer" } };

	/**
	 * <p>
	 * I produce the contents of a JSP.
	 * </p>
	 * 
	 * @author M. Maglioni
	 *         <p>
	 *         Company: Ramecera™<br>
	 *         Creation date: 2020-04-09<br>
	 *         Last updated: 2020-04-09<br>
	 *         </p>
	 * @param skill set of
	 * @param snippetG application descriptive data */
	public Base_jsp(Skill skill, String snippetG) {
		super();
		this.skill = skill;
		this.snippetG = snippetG;
	}

	/** @param markupE markups list
	 * @param trainE trains list
	 * @param candyScrew useful application data
	 * @throws FileNotFoundException obvious
	 * @throws IOException obvious
	 * @throws ExactException exception specified by a numeric code */
	final public void synthesis(HashMap<String, Markup> markupE,
			List<Train> trainE, CandyScrew candyScrew)

			throws FileNotFoundException, IOException, ExactException {

		String baseG = skill.candies.snippetsYS + snippetG;
		producedMaterial = Equipment.oneString(baseG);
		String header = "";
		if (jspKit.refresh > 0) {
			header += Candies.TABS
					+ "<meta http-equiv=\"refresh\" content=\""
					+ jspKit.refresh + "\">" + Candies.LF;
		}
		header += "<c:set var=\"pageTitle\" value=\"" + jspKit.fileName
				+ "\" scope=\"request\" />" + Candies.LF + Candies.TABS
				+ "<jsp:include page=\"../includes/header.jsp\" />";
		String footer = "<jsp:include page=\"../includes/footer.jsp\" />";
		String navigation = "<jsp:include page=\"../includes/navigation_"
				+ jspKit.navigationBar + ".jsp\" />";
		String p = ""; // collects material for pragma
		String s = "";
		String t = "";
		String b = ""; // collects material to belong
		if (jspKit.pageTitle != null) {
			b += Candies.TABS + "<div class=\"" + jspKit.classeCss
					+ "\">" + jspKit.pageTitle + "</div>" + Candies.LF;
		}
		boolean presenzaDivPrincipale = jspKit.jspType == Cst.FORM
				& jspKit.classeCss != null & !skill.hiberValid
				& !skill.springValid;
		String divPrincipale = "<div class=\"" + jspKit.classeCss
				+ "\">" + Candies.LF;
		if (presenzaDivPrincipale) {
			b += Candies.TABS + divPrincipale;
		}
		if (jspKit.jspType == Cst.FORM) {
			b += Candies.TABS + "<form:form action=\"./screw"
					+ jspKit.fileName
					+ "\" method=\"post\" modelAttribute=\""
					+ jspKit.fileName + "Form\"";
			if (jspKit.blank) {
				b += " target=\"_blank\"";
			}
			b += ">" + Candies.LF;
			b += Candies.TABS + Candies.TABS
					+ "<fieldset class=\"formdress\">" + Candies.LF;
			b += Candies.TABS + Candies.TABS + Candies.TABS + "<legend>"
					+ jspKit.legend + "</legend>" + Candies.LF;
			b += Candies.TABS + Candies.TABS + Candies.TABS
					+ "<form:errors path=\"*\" cssClass=\"alert alert-danger"
					+ "\" element=\"div\"/>" + Candies.LF;
		}
		/* for now it is assumed that only the "string" or
		 * "string array" properties participate */
		for (JspBeanElement jbe : jspKit.stringPropertyE) {
			// if (x.partecipa & jspKit.jspType! = Cst.FORM) {
			if (jbe.participate) {
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ divPrincipale + Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS
						+ "<c:out value=\"${belong." + jbe.name
						+ "}\"/>" + Candies.LF + Candies.TABS
						+ Candies.TABS + Candies.TABS + "</div>"
						+ Candies.LF;
			}
		}
		for (JspBeanElement jbe : jspKit.arrayStringPropertyE) {
			// if (x.partecipate & jspKit.jspType! = Cst.FORM) {
			if (jbe.participate) {
				b += Candies.TABS + divPrincipale;
				b += Candies.TABS + Candies.TABS
						+ "<c:forEach var=\"z\" begin=\"0\""
						+ " end=\"${fn:length(belong." + jbe.name
						+ ")}\" items=\"${belong." + jbe.name + "}\">"
						+ Candies.LF;
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ "<c:out value=\"${z}\"/><br>" + Candies.LF;
				b += Candies.TABS + Candies.TABS + "</c:forEach>"
						+ Candies.LF;
				b += Candies.TABS + "</div>" + Candies.LF;
			}
		}
		for (JspBeanElement jbe : jspKit.controlE) {
			if (jbe.participate) {
				switch (jbe.controlType) {
				case Cst.TEXT_BOX:
				case Cst.TEXT_AREA:
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<label for=\"" + jbe.name + "\">"
							+ jbe.label + "</label>" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<form:input path=\"" + jbe.name
							+ "\" type=\"text\" id=\"" + jbe.name
							+ "\" name=\"" + jbe.name
							+ "\" value=\"${belong." + jbe.name
							+ "}\" />" + Candies.LF;
					if (skill.hiberValid | skill.springValid) {
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ "<form:errors path=\"" + jbe.name
								+ "\" cssClass=\"text-danger\"/>"
								+ Candies.LF;
					}
					break;
				case Cst.LIST_BOX:
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<label for=\"id_" + jbe.name + "\">"
							+ jbe.label + "</label>" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<select name=\"" + jbe.name
							+ "\" id=\"id_" + jbe.name + "\">"
							+ Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS + "<option value=\"select\">"
							+ jbe.cartel.text + "</option>"
							+ Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS
							+ "<c:forEach items=\"${belong.arr_"
							+ jbe.name + "}\"" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS + Candies.TABS + "var=\"var_"
							+ jbe.name + "\">" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS + Candies.TABS
							+ "<option <c:if test=\"${var_" + jbe.name
							+ " == belong.selected}\">selected</c:if>>${var_"
							+ jbe.name + "}</option>" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS + "</c:forEach>"
							+ Candies.LF;
					if (jbe.button_label != null) {
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ Candies.TABS
								+ "<div class=\"form-actions\">"
								+ Candies.LF;
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ Candies.TABS + Candies.TABS
								+ "<input id=\"sub"
								+ "mit\" class=\"formn_btn\" na"
								+ "me=\"submit\" type=\"submit\" val"
								+ "ue=\"" + jbe.button_label.text
								+ "\"/>" + Candies.LF;
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ Candies.TABS + "</div>" + Candies.LF;
					}
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "</select>" + Candies.LF;
					break;
				case Cst.CHECK_BOX:
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<div class=\"labeldress\">" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS + "<label for=\"" + jbe.name
							+ "\">" + jbe.label + "</label>"
							+ Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "</div>" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<c:set var=\"cb_" + jbe.name
							+ "\" value=\"\" />" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<c:if test=\"${belong." + jbe.name
							+ "}\">" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ Candies.TABS + "<c:set var=\"cb_"
							+ jbe.name
							+ "\" value='checked=\"checked\"' />"
							+ Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "</c:if>" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<div class=\"checkdress\">" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<form:checkbox  path=\"" + jbe.name
							+ "\" " + "id=\"" + jbe.name + "\" name=\""
							+ jbe.name + "\" value=\"${cb_" + jbe.name
							+ "}\" />" + Candies.LF;
					if (skill.hiberValid | skill.springValid) {
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ "<form:errors path=\"" + jbe.name
								+ "\" cssClass=\"text-danger\"/>"
								+ Candies.LF;
					}
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "</div>" + Candies.LF;
					break;
				case Cst.RADIO_BUTTON:
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<label for=\"" + jbe.name + "\">"
							+ jbe.label + "</label>" + Candies.LF;
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "<table class=\"radiodress\">"
							+ Candies.LF;
					String[][] wE = (String[][]) (jbe.ftokens.substance);
					for (int tK = 0; tK < wE[0].length; tK++) {
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ Candies.TABS + "<tr>" + Candies.LF;
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ Candies.TABS + Candies.TABS + "<td>"
								+ wE[0][tK] + Candies.NON_BREAKING
								+ Candies.NON_BREAKING
								+ "</td><td><form:radiobutton path=\""
								+ jbe.name + "\" value=\"" + tK
								+ "\" checked=\""
								+ (jbe.startingRadio == tK ? "checked"
										: "") + "\"/></td>"
								+ Candies.LF;
						b += Candies.TABS + Candies.TABS + Candies.TABS
								+ Candies.TABS + "</tr>" + Candies.LF;
					}
					b += Candies.TABS + Candies.TABS + Candies.TABS
							+ "</table>" + Candies.LF;
					break;
				}
			}
		}
		// for (JspBeanElement jbe : jspKit.controlE) {
		// if (jbe.participate & (jbe.controlType.equals(Cst.TEXT_BOX)
		// | jbe.controlType.equals(Cst.TEXT_AREA))) {
		// }
		// }
		// for (JspBeanElement jbe : jspKit.controlE) {
		// if (jbe.participate & jbe.controlType.equals(
		// Cst.LIST_BOX)) {
		// }
		// }
		// for (JspBeanElement jbe : jspKit.controlE) {
		// if (jbe.participate & jbe.controlType.equals(
		// Cst.CHECK_BOX)) {
		// }
		// }
		// for (JspBeanElement jbe : jspKit.controlE) {
		// if (jbe.participate & jbe.controlType.equals(
		// Cst.RADIO_BUTTON)) {
		// }
		// }
		int lJ = 0;
		if (skill.defaulan > -1) {
			for (; lJ < buttonsCrud.length; lJ++) {
				if (buttonsCrud[lJ][0].equalsIgnoreCase(
						Universal.languageE[skill.defaulan][1])) {
					break;
				}
			}
			if (lJ == buttonsCrud.length) {
				lJ = 0;
			}
		}
		if (jspKit.jspType == Cst.FORM) {
			b += Candies.TABS + Candies.TABS + Candies.TABS
					+ "<div class=\"form-actions\">" + Candies.LF;
			if (jspKit.crud) {
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + "<input id=\"submit\""
						+ " formaction= \"./screw" + jspKit.fileName
						+ "C\"" + " class=\"btn\"" + " name=\"submit\""
						+ " type=\"submit\"" + " value=\""
						+ buttonsCrud[lJ][1] + "\"/>" + Candies.LF;
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + "<input id=\"submit\""
						+ " formaction= \"./screw" + jspKit.fileName
						+ "R\"" + " class=\"btn\"" + " name=\"submit\""
						+ " type=\"submit\"" + " value=\""
						+ buttonsCrud[lJ][2] + "\"/>" + Candies.LF;
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + "<input id=\"submit\""
						+ " formaction= \"./screw" + jspKit.fileName
						+ "U\"" + " class=\"btn\"" + " name=\"submit\""
						+ " type=\"submit\"" + " value=\""
						+ buttonsCrud[lJ][3] + "\"/>" + Candies.LF;
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS + "<input id=\"submit\""
						+ " formaction= \"./screw" + jspKit.fileName
						+ "D\"" + " class=\"btn\"" + " name=\"submit\""
						+ " type=\"submit\"" + " value=\""
						+ buttonsCrud[lJ][4] + "\"/>" + Candies.LF;
			} else {
				b += Candies.TABS + Candies.TABS + Candies.TABS
						+ Candies.TABS
						+ "<input id=\"submit\" class=\"btn\" name"
						+ "=\"submit\" type=\"submit\" value=\"Go\"/>"
						+ Candies.LF;
			}
			b += Candies.TABS + Candies.TABS + Candies.TABS + "</div>"
					+ Candies.LF;
			b += Candies.TABS + Candies.TABS + "</fieldset>"
					+ Candies.LF;
			b += Candies.TABS + "</form:form>" + Candies.LF;
		}
		if (presenzaDivPrincipale) {
			b += "</div>" + Candies.LF;
		}
		if (jspKit.pragma) {
			p += "<%" + Candies.LF;
			p += "response.setHeader(\"Pragma\",\"no-cache\");"
					+ Candies.LF;
			p += "response.setHeader(\"Cache-Control\", \"no-cache\");"
					+ Candies.LF;
			p += "response.setHeader(\"Expires\",\"-1\");" + Candies.LF;
			p += "response.setHeader(\"X-UA-Compatible\",\"IE=9, IE=edge\");"
					+ Candies.LF;
			p += "%>" + Candies.LF;
		}
		if (markupE.get("text").recurrenceE.size() > 0 & jspKit.fileName
				.equals("index")) {
			for (int m = 0; m < markupE.get("text").recurrenceE
					.size(); m++) {
				if (markupE.get("text").recurrenceE.get(
						m).locomotor.positionJ > jspKit.placeJ) {
					t = "<div class=\"" + markupE.get(
							"text").recurrenceE.get(m).wagonE.get(
									"cssclass").text + "\">" + markupE
											.get("text").recurrenceE
													.get(m).locomotor.text
							+ "</div>";
				}
			}
		}
		if (skill.springSecurity) {
			s += "<%@ taglib prefix=\"sec\""
					+ " uri=\"http://www.springframework.org"
					+ "/security/tags\" %>";
		}
		if (skill.tiles) {
			producedMaterial = Strings.replaceFirst(producedMaterial,
					"+header+", "");
			producedMaterial = Strings.replaceFirst(producedMaterial,
					"+footer+", "");
		}
		if (jspKit.navigationBar == null || jspKit.navigationBar
				.isEmpty()) {
			producedMaterial = Strings.replaceFirst(producedMaterial,
					"+navigation+", "");
		} else {
			producedMaterial = Strings.replaceFirst(producedMaterial,
					"+navigation+", navigation);
		}
		if (!skill.tiles) {
			int h = producedMaterial.indexOf("+header+");
			if (h > -1) {
				producedMaterial = producedMaterial.substring(0, h)
						+ header + producedMaterial.substring(h + 8);
			}
			int f = producedMaterial.indexOf("+footer+");
			if (f > -1) {
				producedMaterial = producedMaterial.substring(0, f)
						+ footer + producedMaterial.substring(f + 8);
			}
		}

		/* WE COOK EVERYTHING (x2b è il segno +) */
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+language+", buttonsCrud[lJ][0]);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+belong+", b);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+text+", t);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+pragma+", p);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+sec+", s);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bcharset\\x2b", skill.charset);
	}

	/* written only to comply with the abstract class FileComponent */
	@SuppressWarnings("javadoc")
	@Override
	final public void directory(HashMap<String, Markup> markupE)

			throws ExactException {

	}
}
