//
// 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.outfits;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.Mix;
import it.ramecera.screwdriver.Cst;

/**
 * <p>
 * Elements forming a set of request maps to be grouped in a class of
 * jspType @Controller.
 * </p>
 * <p>
 * Company: Ramecera™<br>
 * Creation date: 2020-04-09<br>
 * Last updated: 2020-04-09
 * </p>
 * <p>
 * Modifications:<br>
 * </p>
 * 
 * @author M. Maglioni */
final public class RequestMap {

	/** the complete map */
	public String[] map;
	/** the first segment of the map path, which must be cut to be
	 * shared by all methods, in the class RequestMapping */
	public String firstSegmentPath;
	/** the path / jsp where the check ends */
	public String pathJsp;
	/** the path / jsp indicated as redirect */
	public String pathRedirect;
	/** the name of the jsp */
	public String jsp;
	/** the name of the jsp with a capital initial */
	public String jspU;
	/** the name of the path */
	public String path;
	/** type of JSP requested by the request map: 0 = plain; 1 = form; 2
	 * = model */
	public int tipo;
	/** type of the JSP requested by the request map, lowercase
	 * initial */
	public String tipoX;
	/** type of the JSP requested by the request map, capital initial */
	public String tipoU;

	/** useful constructor
	 * 
	 * @param map the complete map
	 * @param pathJsp the path / jsp where the control ends
	 * @param pathJspRedirect TODO
	 * @param tipo JSP type: 0 = plain; 1 = form; 2 = model */

	public RequestMap(final String map, final String pathJsp,
			final String pathJspRedirect, final int tipo) {
		super();
		String[] s = map.split("\\x2c");
		this.map = new String[s.length];
		for (int i = 0; i < s.length; i++) {
			s[i] = s[i].trim();
			this.map[i] = s[i];
		}
		for (int i = 0; i < s.length; i++) {
			String[] m = s[i].split("\\x2f"); // x2f is the slash
			/* if any of the paths do not have slashes, I give up the
			 * first segment */
			if (m.length == 1) {
				this.firstSegmentPath = "";
				break;
				/* if it is the first path, and it is useful, I take
				 * it */
			} else if (i == 0) {
				this.firstSegmentPath = m[0];
				/* if it is a subsequent path, check that the first
				 * segment is not different; in which case, I give up
				 * the first segment */
			} else if (!m[0].equals(this.firstSegmentPath)) {
				this.firstSegmentPath = "";
				break;
			}
		}
		this.pathJsp = pathJsp;
		this.pathRedirect = pathJspRedirect;
		String[] p = pathJsp.split("\\x2f"); // x2f is the slash
		if (p.length == 1) {
			this.path = "";
		} else {
			this.path = p[0];
		}
		this.jsp = p[p.length - 1];
		this.jspU = Mix.uppercaseFirstChar(this.jsp);
		this.tipo = tipo;
		this.tipoX = Cst.JX[tipo];
		this.tipoU = Cst.JU[tipo];

	}

	/** default constructor */
	public RequestMap() {

	}

	/**
	 * <p>
	 * Classic toString method.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @return the compound string */
	public String toString() {
		String s = "";
		s += "map=" + map + "; tipo=" + tipo + "; primoSegmentoPath="
				+ firstSegmentPath + Candies.LF;
		return s;

	}

	/**
	 * <p>
	 * Classic clone method.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @return the compound string */
	public RequestMap clone() {
		RequestMap x = new RequestMap();
		x.map = map;
		x.firstSegmentPath = firstSegmentPath;
		x.pathJsp = pathJsp;
		x.jsp = jsp;
		x.jspU = jspU;
		x.path = path;
		x.tipo = tipo;
		x.tipoX = tipoX;
		x.tipoU = tipoU;
		return x;
	}
}
