/**
 * 
 */
package com.tandbergtv.workflow.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.tandbergtv.workflow.web.common.StaticCodes;

/**
 * @author rao
 * 
 */
public class CommonUtils {

	private static final Logger logger = Logger.getLogger(CommonUtils.class);

	private static final String FILE_NAME = "ApplicationUI";
	public static final String TIME_FORMAT = "timeFormat";
	public static final String DATE_FORMAT = "dateFormat";
	public static final String CHARACTER_ENCODING_PROPERTY = "characterEncoding";

	/**
	 * Method to sort the given Map based on the values
	 * 
	 * @param map
	 *            sorted Map
	 * @return The Sorted Map
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> sort(Map<String, String> map) {

		Entry<String, String>[] entries = new Entry[map.entrySet().size()];
		entries = ((Set<Entry<String, String>>) map.entrySet()).toArray(entries);

		// Sorting the HashMap values
		Arrays.sort(entries, new Comparator<Entry<String, String>>() {
			public int compare(Entry<String, String> lhs, Entry<String, String> rhs) {
				return (lhs.getValue().toUpperCase()).compareTo(rhs.getValue().toString()
						.toUpperCase());
			}
		});

		logger.debug("Before Sorting ---->" + map);
		map.clear();
		map = new LinkedHashMap<String, String>();

		for (int i = 0; i < entries.length; i++) {
			map.put(entries[i].getKey().toString(), entries[i].getValue().toString());
		}
		logger.debug("After Sorting---->" + map);
		return map;
	}

	public static String formatTime(Date date) {
		ResourceBundle rbTimeFormat = ResourceBundle.getBundle(FILE_NAME);
		String format = rbTimeFormat.getString(TIME_FORMAT);
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		if (date != null) {
			return dateFormat.format(date);
		}
		return StaticCodes.EMPTY_STRING;
	}

	public static synchronized ResourceBundle getApplicationUIResourceBundle() {
		return ResourceBundle.getBundle(FILE_NAME);
	}

	public static String formatDate(Date date) {
		ResourceBundle rbTimeFormat = ResourceBundle.getBundle(FILE_NAME);
		String format = rbTimeFormat.getString(DATE_FORMAT);
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		if (date != null) {
			return dateFormat.format(date);
		}
		return StaticCodes.EMPTY_STRING;
	}

	public static Date getDate(String date) {
		String format = getDateFormat();
		// verify the format via length and then parse it
		if (date.trim().length() != format.length()) {
			throw new RuntimeException("Date: " + date + " is not in the format: " + format);
		}
		SimpleDateFormat df = new SimpleDateFormat(format);
		df.setLenient(false);
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static String getDateFormat() {
		ResourceBundle rbTimeFormat = ResourceBundle.getBundle(CommonUtils.FILE_NAME);
		return rbTimeFormat.getString(CommonUtils.DATE_FORMAT);
	}
	
	/**
	 * Get the Character Encoding used by the Web UI
	 * 
	 * @return The Character Encoding
	 */
	public static String getCharacterEncoding() {
		ResourceBundle bundle = ResourceBundle.getBundle(CommonUtils.FILE_NAME);
		return bundle.getString(CommonUtils.CHARACTER_ENCODING_PROPERTY);
	}
}
