/*
 * Messages
 *
 * Created on 05.01.2009
 * Copyright (c) 2009 Kiesraad
 */
package de.ivu.wahl.result.i18n;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// TODO: ELECT-9480: Migrate text resources in elect.sitzberechnung.osv.legacy

/**
 * rudimentäre Lokalisierung - hauptsächlich/ausschließlich(?) für das Logging, fraglich, ob mittelfristig sinnvoll, oder ob nicht eine Sprache ausreichend ist
 */
@Deprecated
public class Messages {
  private static final String BUNDLE_NAME = "de.ivu.wahl.result.i18n.messages"; //$NON-NLS-1$

  private static final Logger LOGGER = LoggerFactory.getLogger(Messages.class);

  private static final Locale LOCALE = Locale.GERMANY; // Wird wohl reichen, falls nicht => als Parameter mitgeben

  private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, LOCALE);

  private Messages() {
    // hide constructor
  }

  /**
   * @return Localized message (without parameters)
   */
  @Deprecated
  public static String getString(String key, Object... args) {
    try {
      String result = RESOURCE_BUNDLE.getString(key);
      if (args != null) {
        return applyPattern(result, args);
      }
      return result;
    } catch (MissingResourceException e) {
      LOGGER.warn("Missing key: " + key);
      return '!' + key + '!' + '?' + Arrays.toString(args);
    }
  }

  /**
   * @return Localized message with binding <code>args</code>
   */
  @Deprecated
  public static String bind(String key, Object... args) {
    return getString(key, args);
  }

  /**
   * @return mesage with binding <code>args</code> (not localized)
   */
  private static String applyPattern(String message, Object... args) {
    return new MessageFormat(message, LOCALE).format(args);
  }


}
