package me.coley.recaf.ui;

import org.objectweb.asm.Type;

import me.coley.recaf.Recaf;
import me.coley.recaf.config.ThemeConfig;
import me.coley.recaf.config.UiConfig;

/**
 * Utility methods creating HTML for labels.
 * 
 * @author Matt
 *
 */
public interface HtmlRenderer {
	/**
	 * HTML escape '&amp;', '&lt;' and '&gt;'.
	 *
	 * @param s
	 *            Text to escape
	 * @return Text with amp, lt, and gt escaped correctly.
	 */
	default String escape(String s) {
		return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
	}

	/**
	 * Converts a given type to a string.
	 *
	 * @param type
	 *            The type object.
	 * @return String representation of the type object.
	 */
	default String getTypeStr(Type type) {
		return getTypeStr(type, null);
	}

	/**
	 * Converts a given type to a string. Output will be simplified if enabled
	 * in passed options.
	 * 
	 * @param type
	 *            The type object.
	 * @param options
	 *            Options object.
	 * @return String representation of the type object.
	 * @see me.coley.recaf.config.UiConfig
	 */
	default String getTypeStr(Type type, UiConfig options) {
		String s = "";
		if (type.getDescriptor().length() == 1) {
			switch (type.getDescriptor().charAt(0)) {
			case 'Z':
				return "boolean";
			case 'I':
				return "int";
			case 'J':
				return "long";
			case 'D':
				return "double";
			case 'F':
				return "float";
			case 'B':
				return "byte";
			case 'C':
				return "char";
			case 'S':
				return "short";
			case 'V':
				return "void";
			default:
				return type.getDescriptor();
			}
		} else {
			s += type.getInternalName();
		}
		if (options != null && options.opcodeSimplifyDescriptors && s.contains("/")) {
			s = s.substring(s.lastIndexOf("/") + 1);
			if (s.endsWith(";")) {
				s = s.substring(0, s.length() - 1);
			}
		}
		return s;
	}

	/**
	 * Italicize the given text.
	 *
	 * @param input
	 *            Text to italicize.
	 * @return HTML markup to italicize the text.
	 */
	default String italic(String input) {
		return "<i>" + input + "</i>";
	}

	/**
	 * Bold the given text.
	 *
	 * @param input
	 *            Text to bold.
	 * @return HTML markup with the text bolded.
	 */
	default String bold(String input) {
		return "<b>" + input + "</b>";
	}

	/**
	 * Color the given text.
	 *
	 * @param color
	 *            The color to turn the text, must be a valid HTML color.
	 * @param input
	 *            The text to color.
	 * @return HTML markup with the text colored appropriately.
	 */
	default String color(String color, String input) {
		return "<font color=\"" + color + "\">" + input + "</font>";
	}

	/**
	 * @return Colors instance.
	 */
	default ThemeConfig getTheme() {
		return Recaf.INSTANCE.confTheme;
	}
}

@@ -1158,18 +1158,8 @@
 s = 
-%22%22;%0A%09%09if (
 type
@@ -1178,461 +1178,243 @@
 or()
-.length() == 1) %7B%0A%09%09%09switch (type.getDescriptor().charAt(0)) %7B%0A%09%09%09case 'Z':%0A%09%09%09%09return %22boolean%22;%0A%09%09%09case 'I':%0A%09%09%09%09return %22int%22;%0A%09%09%09case 'J':%0A%09%09%09%09return %22long%22;%0A%09%09%09case 'D':%0A%09%09%09%09return %22double%22;%0A%09%09%09case 'F':%0A%09%09%09%09return %22float%22;%0A%09%09%09case 'B':%0A%09%09%09%09return %22byte%22;%0A%09%09%09case 'C':%0A%09%09%09%09return %22char%22;%0A%09%09%09case 'S':%0A%09%09%09%09return %22short%22;%0A%09%09%09case 'V':%0A%09%09%09%09return %22void%22;%0A%09%09%09default:%0A%09%09%09%09return type.getDescriptor
+;%0A%09%09// Check if field type. If so, then format as class name.%0A%09%09if (!s.contains(%22(%22) && (s.length() == 1 %7C%7C s.startsWith(%22L%22)%7C%7C s.startsWith(%22%5B%22))) %7B%0A%09%09%09s = type.getClassName
 ();%0A%09%09
-%09
 %7D%0A%09%09
-%7D else %7B%0A%09%09%09s += type.getInternalName();%0A%09%09%7D
+// If simplification is on, substring away package.
 %0A%09%09i
@@ -1484,17 +1484,17 @@
 ntains(%22
-/
+.
 %22)) %7B%0A%09%09
@@ -1525,17 +1525,17 @@
 ndexOf(%22
-/
+.
 %22) + 1);
@@ -1541,91 +1541,73 @@
 ;%0A%09%09
-%09if (s.endsWith(%22;%22)) %7B%0A%09%09%09%09s = s.substring(0, s.length() - 1);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return s
+%7D%0A%09%09// Return name in internal style%0A%09%09return s.replace(%22.%22, %22/%22)
 ;%0A%09%7D

Fix #23