package it.mice.voila.runtime.springmvc.taglibs;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import org.apache.commons.lang.StringEscapeUtils;


public class Functions {
	public static String decodeHistoryTypeStyle(Integer historyType) {
		if (historyType == 10) {
			return "fa-plus bg-blue";
		}
		if (historyType == 20) {
			return "fa-edit bg-aqua";
		}
		if (historyType == 30) {
			return "fa-edit bg-aqua";
		}
		if (historyType == 40) {
			return "fa-edit bg-aqua";
		}
		return "fa-edit bg-aqua";
	}

	public static String decodeKanbanStyle(Integer counter) {
		switch (counter) {
		case 0:
			return "danger";

		case 1:
			return "warning";

		case 2:
			return "info";

		default:
			return "success";
		}
	}

	public static String escapeJava(Object value) {
		return StringEscapeUtils.escapeJava(nullSafeGet(value));
	}

	public static String escapeCsv(Object value) {
		return StringEscapeUtils.escapeCsv(nullSafeGet(value));
	}

	public static String escapeHtml(Object value) {
		return StringEscapeUtils.escapeHtml(nullSafeGet(value));
	}

	public static String escapeJavaScript(Object value) {
		return StringEscapeUtils.escapeJavaScript(nullSafeGet(value));
	}

	public static String escapeSql(Object value) {
		return StringEscapeUtils.escapeSql(nullSafeGet(value));
	}

	public static String escapeXml(Object value) {
		return StringEscapeUtils.escapeXml(nullSafeGet(value));
	}

	public static String toJsDate(Date value) {
		if (value == null) {
			return "null";
		}
		return "new Date(" + value.getTime() + ")";
	}

	public static Object outNullOrValue(Object value) {
		if (value == null) {
			return "null";
		}
		return value;
	}

	private static String nullSafeGet(Object value) {
		return value != null ? value.toString() : "";
	}

	public static String urlEncode(String value) throws UnsupportedEncodingException {
	    return URLEncoder.encode(value, "UTF-8");
	}}
