package ca.corefacility.bioinformatics.irida.ria.dialects.processors.icons;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Attribute;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.processor.element.AbstractMarkupSubstitutionElementProcessor;

import com.google.common.collect.ImmutableList;

/**
 * Element processor to handle icons in the IRIDA UI.
 */
public class FontAwesomeIconElementProcessor extends AbstractMarkupSubstitutionElementProcessor {
	private static final Logger logger = LoggerFactory.getLogger(FontAwesomeIconElementProcessor.class);

	// This is required!  This determines the type of icon to add (see list of attributes below).
	private static final String TYPE_ATTRIBUTE = "type";
	// Optional.  This will add a class to give the icon a fixed width.  Use when creating lists which have icons.
	private static final String FIXED_ATTRIBUTE = "fixed";
	// Optional. THis will add a class to give the icon a larger size.
	private static final String SIZE_ATTRIBUTE = "size";

	//	DOM Element to create the icon in.
	private static final String ICON_CONTAINER = "span";

	public FontAwesomeIconElementProcessor() {
		super("icon");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) {
		// Create the DOM element for the icons
		final Element container = new Element(ICON_CONTAINER);
		/*
		Get the type of icon needed.  If the icon is not available you should not create the DOM element.
		 */
		final String type = element.getAttributeValue(TYPE_ATTRIBUTE);
		element.removeAttribute(TYPE_ATTRIBUTE);
		try {
			FontAwesome fontAwesome = FontAwesome.builder(type).fixedWidth(element.hasAttribute(FIXED_ATTRIBUTE))
					.setIconSize(element.getAttributeValue(SIZE_ATTRIBUTE)).build();
			container.setAttribute("class", fontAwesome.getClassString());

			// Copy over and other remaining attributes
			Map<String, Attribute> attrmap = element.getAttributeMap();
			for (String attr : attrmap.keySet()) {
				container.setAttribute(attr, attrmap.get(attr).getValue());
			}

			return ImmutableList.of(container);
		}
		catch (IconNotFoundException e){
			logger.error(e.getMessage());
			return null;
		}
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public int getPrecedence() {
		return 1000;
	}
}

@@ -702,16 +702,86 @@
 lass);%0A%0A
+%09// DOM tag name%0A%09private static final String DOM_TAG_NAME = %22icon%22;%0A%0A
 %09// This
@@ -867,16 +867,16 @@
 below).%0A
-
 %09private
@@ -1358,22 +1358,28 @@
 %09%09super(
-%22icon%22
+DOM_TAG_NAME
 );%0A%09%7D%0A%0A%09
@@ -1543,22 +1543,16 @@
 icons%0A%09%09
-final 
 Element 
@@ -1705,22 +1705,16 @@
 %09%09 */%0A%09%09
-final 
 String t
@@ -2319,19 +2319,17 @@
 er);%0A%09%09%7D
-%0A%09%09
+ 
 catch (I
@@ -2351,16 +2351,17 @@
 ption e)
+ 
 %7B%0A%09%09%09log
@@ -2415,17 +2415,13 @@
 %09%7D%0A%0A
+
 %09/**%0A
-%09 *%0A
 %09 * 

Remove final keyword and made the tag name a static variable.