package info.bukova.isspst;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.util.resource.Labels;

public class StringUtils
{
	public static boolean isNullOrEmpty(String str)
	{
		return ((str == null) || (str.isEmpty()));
	}

	public static boolean isNullOrTrimmedEmpty(String str)
	{
		return ((str == null) || (str.trim().isEmpty()));
	}

	public static String nullToEmptyString(String str)
	{
		return str == null ? "" : str;
	}

	private static String zeroToEmptyString(long i)
	{
		return i == 0 ? "" : String.valueOf(i);
	}

	public static String localizeDbErr(String sqlError)
	{
		String splitMessage[] = sqlError.split("'");
		String message = "";

		for (int i = 0; i < splitMessage.length; i++)
		{
			if (i % 2 == 0)
			{
				message += getLocalized(splitMessage[i]);
			}
			else
			{
				message += " '" + splitMessage[i] + "' ";
			}
		}

		return message;
	}

	public static String localize(String key)
	{
		return Labels.getLabel(key) == null ? key : Labels.getLabel(key);
	}

	private static String getLocalized(String str)
	{
		String words[] = str.split(" ");
		String key = "";

		for (String word : words)
		{
			if (!word.isEmpty())
			{
				key += word.substring(0, 1).toUpperCase() + word.substring(1);
			}
		}

		return Labels.getLabel("Db" + key);
	}

	public static boolean isEqualForFilter(String value, String search)
	{
		value = StringUtils.nullToEmptyString(value).toLowerCase();
		search = StringUtils.nullToEmptyString(search).toLowerCase();

		if (search.isEmpty())
		{
			return true;
		}

		return value.startsWith(search);
	}

	public static boolean isIcEqualForFilter(long value, long search)
	{
		String compareValue = StringUtils.zeroToEmptyString(value);
		String searchValue = StringUtils.zeroToEmptyString(search);
		return compareValue.startsWith(searchValue);
	}

	public static String encodeSpecialChars(String value)
	{
		if (value != null)
		{
			value = value.replace("²", "[up]2[/up]");
			value = value.replace("³", "[up]3[/up]");
		}

		return value;
	}

	public static String decodeSpecialChars(String value)
	{
		if (value != null)
		{
			value = value.replace("[up]2[/up]", "²");
			value = value.replace("[up]3[/up]", "³");
		}

		return value;
	}

	public static String addSeparator(String value, String separator, boolean toTail)
	{
		if ((value != null) && (separator != null))
		{
			if (!value.isEmpty() && !separator.isEmpty())
			{
				value = (toTail ? value + separator : separator + value);
			}
		}
		
		return value;
	}

	public static String addSeparator(String value, String separator)
	{
		return StringUtils.addSeparator(value, separator, true);
	}

	public static String join(List<String> list, String separator)
	{
		String result = "";
		
		for (int i = 0; i < list.size(); i++)
		{
			String item = StringUtils.nullToEmptyString(list.get(i));
			result = StringUtils.addSeparator(result, separator);
			result += item;
		}
		
		return result;
	}

	public static String joinNotEmpty(List<String> list, String separator)
	{
		String result = "";

		for (int i = 0; i < list.size(); i++)
		{
			String item = StringUtils.nullToEmptyString(list.get(i));

			if (!item.isEmpty())
			{
				result = StringUtils.addSeparator(result, separator);
				result += item;
			}
		}

		return result;
	}

	public static List<String> split(String value, String separator)
	{
		String tmp = value;
		List<String> list = new ArrayList<String>();

		if (tmp != null)
		{
			if ((separator == null) || separator.isEmpty() || tmp.isEmpty())
			{
				list.add(tmp);
			}
			else
			{
				int separatorLength = separator.length();
				
				while (!tmp.isEmpty())
				{
					String part = "";
					int idx = tmp.indexOf(separator);
					
					if (idx > -1)
					{
						part = tmp.substring(0, idx);
						list.add(part);
						tmp = tmp.substring(idx + separatorLength);

						if (tmp.isEmpty())
						{
							list.add("");
						}
					}
					else
					{
						list.add(tmp);
						tmp = "";
					}
				}
			}
		}

		return list;
	}
}
