//
// https://www.ramecera.it/myscrewdriver.html
// e-mail: screwdriver@ramecera.it
//
package it.ramecera.salad.analysis;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.ExactException;
import it.ramecera.salad.everyday.Mix;
import it.ramecera.salad.everyday.Strings;
import it.ramecera.salad.file.FileBase;

/**
 * <p>
 * Creation date: 2020-10-03<br>
 * Last updated: 2020-10-03<br>
 * Company: Ramecera™<br>
 * </p>
 * 
 * @author Massimo Maglioni
 *         <p>
 *         Description:
 *         </p>
 */
public class CutComments {

	/** conventional sign */
	final static private char BELL = 7;
	/** conventional sign */
	final static private String BELLS = String.valueOf(BELL);
	/** conventional sign */
	final static private char DQ = 34;

	/**
	 * <p>
	 * Creation date: 2020-04-18<br />
	 * Last updated: 2020-04-18<br />
	 * Company: Ramecera
	 * 
	 * @author Massimo Maglioni
	 *         <p>
	 *         Description:
	 *         </p>
	 * @param s text to process
	 * @param fb file informations
	 * @return 3 text strings
	 * @throws ExactException */
	public static String[] production(final String s, FileBase fb)

			throws ExactException {

		String[] d = new String[3];
		String t = s;
		/* I delete all carriage-return characters */
		t = t.replaceAll("\\r", "");
		/* mercy corrections: "/** /**" becomes "/**" */
		t = t.replaceAll("\\x2f\\x2a\\x2a\\x20\\x2f\\x2a\\x2a", "/**");
		/* mercy corrections: "/** /**" becomes "/**" */
		t = t.replaceAll("\\x2f\\x2a\\x2a\\x20+\\x2f\\x2a\\x2a", "/**");
		/* mercy corrections: delete initial string
		 * "slash** * *slash" */
		t = t.replaceFirst("\\x2f\\x2a\\x2a\\n\\x20\\x2a\\x20\\n\\x"
				+ "20\\x2a\\x2f\\n", "");
		/* mercy corrections: "// /*" becomes "/**" */
		t = t.replaceAll("\\x2f\\x2f\\x20+\\x2f\\x2a", "/*");
		String[] text1E, text2E;
		// -------- PRODUCTION -------------
		/* production of the fliles BCI */
		text1E = production1(fb.fileName, "bci", t, "/*", "*/");
		/* production of the fliles LCI and NUD */
		text2E = production2(fb.fileName, "lci", text1E[1], "//",
				Candies.LF);
		// ---------------------------------
		d[0] = text1E[0];
		d[1] = text2E[0];
		d[2] = text2E[1];
		return d;
	}

	/**
	 * <p>
	 * Creation date: 2020-04-09<br />
	 * Last updated: 2020-04-09<br />
	 * 
	 * @author Massimo Maglioni
	 *         <p>
	 *         Description: it produces the block comments text and a
	 *         java file whithout them (line comments are still
	 *         present).
	 *         </p>
	 * @param fileName obvious
	 * @param extension obvious
	 * @param input input text
	 * @param flip the opening string: slash-asterisk
	 * @param flop the closing string: asterisk-slash
	 * @return the two text, blocks comments and the nude text(line
	 *         comments are still present).
	 * @throws ExactException */
	private static String[] production1(final String fileName,
			final String extension, final String input,
			final String flip, final String flop)

			throws ExactException {

		String t = input;
		String[] u = new String[2];
		/* nude text */
		String tn = "";
		String cit = "";
		int lJ = indexSearch(t, flip, fileName, true);
		int wJ = lJ;
		int endJ = -1, wendJ = 0;
		System.out.println(fileName);
		while (lJ > -1) {
			wJ = lJ;
			/* I'm looking for the next one because I have to check that
			 * the closing of the comment does not go beyond the next
			 * opening; it would be an important irregularity */
			lJ = indexSearch(t, flip, lJ + 1, true, fileName);
			/* max is the maximum limit, it must not go beyond */
			int maxJ = 0;
			if (lJ == -1) {
				maxJ = t.length();
			} else {
				maxJ = lJ;
			}
			wendJ = endJ;
			/* my corrections (+1, +2, -2) keep in mind the opening and
			 * closing strings */
			endJ = indexSearch(t, flop, wJ + 1, true, fileName) + 2;
			if ((endJ == -1 | endJ > maxJ)) {
				throw new ExactException("In the file " + fileName
						+ " the succession of the comments is wrong.");
			} else {
				/* <b>single</b> is the single block comment */
				/* now the closure was found before maximum value
				 * (maxJ), so it would be fine... */
				String single = t.substring(wJ + 2, endJ - 2);
				String before = t.substring(wendJ + 1, wJ);
				/* ...but I must make sure that I have not left behind a
				 * closure, because it would denounce the consecutive
				 * presence of 2 closings in the previous block */
				if (indexSearch(before, flop, fileName, false) > -1) {
					throw new ExactException("In the file " + fileName
							+ " the string " + flop
							+ " is present in a wrong position.");
				}
				tn += before + extension + BELLS + Candies.LF;
				single = Strings.compact_A(single);
				/* mercy corrections: search " * ", replace " " */
				single = single.replaceAll("\\x20\\x2a\\x20", " ");
				cit += single + Candies.LF;
			}
		}
		tn += t.substring(endJ + 1);
		int cL = cit.length();
		if (cL > 0) {
			/* I delete the last character, it is a line feed */
			cit = cit.substring(0, cL - 1);
		}
		u[0] = cit;
		u[1] = tn;
		return u;
	}

	/**
	 * <p>
	 * Creation date: 2020-04-09<br />
	 * Last updated: 2020-04-09<br />
	 * 
	 * @author Massimo Maglioni
	 *         <p>
	 *         Description: it produce the line comments text and a java
	 *         file whithout any comment (nude).
	 *         </p>
	 * @param fileName obvious
	 * @param extension obvious
	 * @param input input text
	 * @param flip the opening string: //
	 * @param flop the closing string: a line feed
	 * @return the two text, line comments and the nude text (comments
	 *         are present no more).
	 * @throws ExactException */
	private static String[] production2(final String fileName,
			final String extension, final String input,
			final String flip, final String flop)

			throws ExactException {

		String t = input;
		String[] u = new String[2];
		String tn = "";
		String cit = "";
		int lJ = indexSearch(t, flip, fileName, false);
		int endJ = 0, wendJ = 0;
		while (lJ > -1) {
			wendJ = endJ;
			endJ = indexSearch(t, flop, lJ + 1, false, fileName);
			if (endJ == -1) {
				endJ = t.length();
			}
			String single = t.substring(lJ, endJ);
			tn += t.substring(wendJ, lJ) + extension + BELLS;
			single = Strings.compact_A(single);
			cit += single.substring(2).trim() + Candies.LF;
			lJ = indexSearch(t, flip, endJ, false, fileName);
		}
		tn += t.substring(endJ);
		int cL = cit.length();
		if (cL > 0) {
			/* I delete the last character, it is a line feed */
			cit = cit.substring(0, cL - 1);
		}
		u[0] = cit;
		u[1] = tn;
		return u;
	}

	/**
	 * <p>
	 * Creation date: 2020-04-10<br />
	 * Last updated: 2020-04-10<br />
	 * 
	 * @author Massimo Maglioni
	 *         <p>
	 *         Description:
	 *         </p>
	 * @param text
	 * @param x
	 * @param k
	 * @param type true=block comment, false=line comment
	 * @param fileName obvious
	 * @return */
	private static int indexSearch(final String text, final String x,
			final int k, final boolean type, String fileName) {
		int j = 1;
		int m = k - 1;
		String before0 = null;
		while (j % 2 == 1) {
			m = text.indexOf(x, m + 1);
			if (m == -1 | (x.equals(Candies.LF) & !type)) {
				break;
			}
			int from = 0;
			if (k > 1) {
				from = k - 1;
			}
			if (type) {
				before0 = text.substring(from, m + 2); // block
			} else {
				before0 = text.substring(from + 1, m + 2); // line
			}
			/* I cancel all double backslashes \\ (to avoid error dued
			 * to possible presence of a string "\\" */
			String before1 = before0.replaceAll("\\x5c\\x5c", "");
			/* I cancel all double quotes contained in a constant string
			 * (x5cx22-->\") */
			String before2 = before1.replaceAll("\\x5c\\x22", "");
			// /* I cancel all characters contained in a line comment */
			// String before2 = before1.replaceAll("\\x2f\\x2f.+\\n",
			// "");
			/* I cancel all double quotes (\") contained in a block
			 * comment */
			String before3 = before2.replaceAll("\\n", "");
			String before4 = before3.replaceAll("\\r", "");
			String before5 = before4.replaceAll(
					"\\x2f\\x2a.+?\\x2a\\x2f", "");
			j = Mix.charQ(before5, DQ);
			if (j % 2 == 1) {
				// if (x.equals("//")) {
				// System.out.println("geppo");
				// }
				System.out.println("-----------------cerco>" + x
						+ "<---- in >" + fileName
						+ "<------------------");
				// System.out.println("intervento double-quote"
				// + Candies.LF + before0);
			}
		}
		return m;
	}

	/**
	 * <p>
	 * Creation date: 2020-04-10<br />
	 * Last updated: 2020-04-10<br />
	 * 
	 * @author Massimo Maglioni
	 *         <p>
	 *         Description:
	 *         </p>
	 * @param text
	 * @param x
	 * @param type true=block comment, false=line comment
	 * @param k
	 * @return */
	private static int indexSearch(final String text, final String x,
			final String fileName, boolean type) {
		return indexSearch(text, x, 0, type, fileName);
	}

}
