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

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.ExactException;
import it.ramecera.salad.everyday.Mix;
import it.ramecera.salad.everyday.StartingOperations;

/**
 * <p>
 * Treatment of property data. This data is conventionally called <b>
 * candies</b>.
 * </p>
 * <p>
 * Creation date: 2020-04-09<br>
 * modification date: 2020-04-09<br>
 * Company: Ramecera™
 * </p>
 * <p>
 * Modifications:<br>
 * </p>
 * 
 * @author M. Maglioni */
final public class CandyScrew implements Candies {

	/** application name */
	public String appName;
	/** ELE file */
	public String ele;
	/** input files */
	public File inputG;
	/** input directory */
	public String inputYS;
	/** input directory */
	public File inputY;
	/** output directory */
	public String outputYS;
	/** output directory, as a file */
	public File outputY;
	/** box directory, contains everything you need to work */
	public String boxYS = "box";
	/** box directory, as a file */
	public File boxY;
	/** include directory */
	public String includeYS;
	/** include directory, as a file */
	public File includeY;
	/** snippets directory */
	public String snippetsYS;
	/** snippets directory, as a file */
	public File snippetsY;
	/** if I encode the methods already in the .ele file */
	public boolean inside;
	/** name of a file that must not be modified at all */
	public String freezeS;
	/** the names of the files that must not be changed at all */
	public List<String> freezeE = new ArrayList<String>();
	/** the names of the files that must not be changed at all */
	public List<String> freezeDueE = new ArrayList<String>();
	/** the names of the files that need to be changed, however asking
	 * for permission */
	// public List <String> bullDozerE = new ArrayList <String> ();

	/** the list of properties */
	public Properties properties;

	/**
	 * <p>
	 * Reads the properties file.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-09-24<br>
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @param fileProperties name of properties file
	 * 
	 * @throws ExactException objects if the data contained in the
	 *         properties file is incorrect */
	public void readProperties(String fileProperties)

			throws ExactException {

		String prop = fileProperties + ".properties";
		Path pp = new File(prop).toPath();
		if (!Files.exists(pp)) {
			throw new ExactException("The file " + Mix.dress_A(prop)
					+ " does not exist.", 1400, this.getClass()
							.getName(), "");
		} else if (Files.isDirectory(pp)) {
			throw new ExactException("The file " + Mix.dress_A(prop)
					+ " is not actually a directory.", 1401, this
							.getClass().getName(), "");
		}
		properties = StartingOperations.myProperties(prop, true);
		inputYS = StartingOperations.whetherPresent(properties,
				Cst.DIRE_INPUT);
		inputY = new File(inputYS);
		if (!inputY.exists()) {
			throw new ExactException("The directory «" + inputYS
					+ "» does not exist.", 1400, this.getClass()
							.getName(), "");
		} else if (!inputY.isDirectory()) {
			throw new ExactException("The directory «" + inputYS
					+ "» is not actually a directory.", 1401, this
							.getClass().getName(), "");
		}
		inputYS = Mix.dress_CU(inputYS);
		inputG = new File(inputYS + ele);
		if (!inputG.exists()) {
			throw new ExactException("The file «" + ele
					+ "» does not exist.", 1402, this.getClass()
							.getName(), "");
		} else if (!inputG.isFile()) {
			throw new ExactException("The file «" + appName
					+ "» non è in realtà un file.", 1403, this
							.getClass().getName(), "");
		}
		outputYS = StartingOperations.whetherPresent(properties,
				Cst.DIR_OUTPUT);
		outputY = new File(outputYS);
		if (!outputY.exists()) {
			throw new ExactException("The directory «" + outputY
					+ "» does not exist.", 1404, this.getClass()
							.getName(), "");
		} else if (!outputY.isDirectory()) {
			throw new ExactException("The directory «" + outputYS
					+ "» is not actually a directory.", 1405, this
							.getClass().getName(), "");
		}
		outputYS = Mix.dress_CU(outputYS);
		boxYS = "box";
		boxY = new File(inputYS + boxYS);
		if (!boxY.exists()) {
			throw new ExactException("The directory «" + boxY
					+ "» does not exist.", 1408, this.getClass()
							.getName(), "");
		} else if (!boxY.isDirectory()) {
			throw new ExactException("The directory «" + boxYS
					+ "» is not actually a directory.", 1409, this
							.getClass().getName(), "");
		}
		boxYS = Mix.dress_CU(boxYS);
		includeYS = boxYS + "include/";
		includeY = new File(includeYS);
		snippetsYS = boxYS + "snippets/";
		snippetsY = new File(snippetsYS);
		inside = StartingOperations.propertyPresenteBoolean(properties,
				Cst.INSIDE);
		freezeS = properties.getProperty(Cst.FREEZE);
		if (freezeS != null) {
			String[] fE = null;
			fE = freezeS.split("\\x2c"); // x2c is the comma
			for (int i = 0; i < fE.length; i++) {
				freezeE.add(fE[i].trim());
			}
		}
	}

	/**
	 * <p>
	 * Reads the properties file.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09<br>
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @return the total string produced */
	@Override
	public String toString() {
		String s = "";
		s += Candies.LF + Candies.LF;
		s += Cst.CUT0;
		s += "DATA PRESENT IN PROPERTIES, INCLUDING THE DERIVATIVE ONES"
				+ Candies.LF;
		s += Cst.CUT0;
		s += "input file, only the file name: " + appName + Candies.LF;
		s += "input file: " + inputG.getPath() + Candies.LF;
		;
		s += "input directory: " + inputYS + Candies.LF;
		s += "input directory, as file: " + inputY.getPath()
				+ Candies.LF;
		s += "output directory: " + outputYS + Candies.LF;
		s += "output directory, as file: " + outputY.getPath()
				+ Candies.LF;
		s += "include directory: " + includeYS + Candies.LF;
		s += "include directory, as file: " + includeY.getPath()
				+ Candies.LF;
		s += "snippets directory: " + snippetsYS + Candies.LF;
		s += "snippets directory, as file: " + snippetsY.getPath()
				+ Candies.LF;
		s += Candies.LF;
		return s;
	}
}
