package com.tandbergtv.metadatamanager.validation;

import java.util.Locale;
import java.util.ResourceBundle;
import java.text.MessageFormat;

/**
 * Util class to lookup the message given the code and language.
 * 
 * @author spuranik
 * 
 */
public class ErrorCodeLookup {

	private static String RESOURCE_NAME = "com.tandbergtv.metadatamanager.validation.resources.ErrorMessages";

	/**
	 * Returns the message corresponding to the code in the given language
	 * 
	 * @param error
	 * @param locale
	 * @return
	 */
	public static String lookup(ValidationError error, Locale locale) {
		ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_NAME, locale);

		String message = bundle.getString(error.getErrorCode());
		Object[] args = new Object[error.getErrorFields().size()
				+ error.getErrorInfo().size()];

		// add fields first and then the info
		for (int i = 0; i < error.getErrorFields().size(); i++) {
			args[i] = error.getErrorFields().get(i);
		}
		for (int i = 0, j = error.getErrorFields().size(); i < error
				.getErrorInfo().size(); i++, j++) {
			args[j] = error.getErrorInfo().get(i);
		}
		return MessageFormat.format(message, args);
	}
}
