/**
 * Licensed under the MICE s.r.l.
 * End User License Agreement
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.micegroup.it/voila/license.html
 */
package it.mice.voila2.plugin.utils;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.WriterAppender;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

/**
 * @author Emilio Dolce (MICE)p
 *         <p/>
 *         To change this generated comment edit the template variable
 *         "typecomment": Window>Preferences>Java>Templates. To enable and
 *         disable the creation of type comments go to
 *         Window>Preferences>Java>Code Generation.
 */
public class Utility {
	public static final String LOG4J_PATTERN = "%d %-5p (%F:%L) - %m%n"; // "%d %-5p --> %m%n";
	public static final String CONSOLE_NAME = "VoilaPlugin";

	public static MessageConsoleStream setupConsole(String name) throws PartInitException {

		if (name == null) {
			name = CONSOLE_NAME;
		}
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++) {
			if (name.equals(existing[i].getName())) {
				MessageConsoleStream out = prepareConsole((MessageConsole) existing[i]);
				return out;
			}
		}
		// no console found, so create a new one
		MessageConsole myConsole = new MessageConsole(name, null);
		conMan.addConsoles(new IConsole[] { myConsole });

		MessageConsoleStream out = prepareConsole(myConsole);
		return out;
	}

	public static MessageConsoleStream prepareConsole(MessageConsole console) {
		console.activate();
		MessageConsoleStream out = console.newMessageStream();
		out.setActivateOnWrite(true);

		Appender a1Appender = LogManager.getRootLogger().getAppender("VoilaConsole");
		Level a1Level = LogManager.getRootLogger().getLevel();

		// Layout layout = new PatternLayout(LOG4J_PATTERN);
		if (a1Appender != null) {
			Appender appender = new WriterAppender(a1Appender.getLayout(), out);
			appender.setName("Current" + a1Appender.getName());

			LogManager.getRootLogger().addAppender(appender);
			// LogManager.getRootLogger().removeAppender(a1Appender);
			// LogManager.getRootLogger().setLevel(a1Level);
			((ConsoleAppender) a1Appender).setWriter(new java.io.PrintWriter(System.err));
			out.println("------------------------------------------------------------------------");
			out.println("----------------- Voilà2 Generation Console -----------------------------");
			out.println("------------------------------------------------------------------------");

//		Enumeration<Logger> loggers = LogManager.getCurrentLoggers();
//		while (loggers.hasMoreElements()) { 
//			Logger logger = (Logger) loggers.nextElement();
//			out.println(logger.getName());
//		}
		}
		return out;
	}

	/**
	 * Method showSystemProperties.
	 */
	public static void showSystemProperties() {
		Properties p = System.getProperties();
		Enumeration e = p.propertyNames();
		System.out.println("Start System properties list:");
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			System.out.println(name + "=" + p.getProperty(name));
		}
	}

	public static String getProjectId(String rootProject) {
		int i = rootProject.lastIndexOf("-");
		String projectName = rootProject;
		if (i > 0) {
			projectName = rootProject.substring(0, i);
		}
		return projectName;
	}

	public static String extractSuffixFromToken(String orderByColumn) {
		int spacePos = orderByColumn.indexOf(" ");
		if (spacePos == -1) {
			return "";
		}
		String suffix = orderByColumn.substring(spacePos + 1).trim();
		return suffix;
	}

	public static String encodeWord(String value) {
		return value.replaceAll("à", "a'").replaceAll("è", "e'").replaceAll("ì", "i'").replaceAll("ò", "o'")
				.replaceAll("ù", "u'").replaceAll("`", "'").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
	}
	
	
}
