package com.srbenoit.log;

import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Logger;

/**
 * Resources used by the logging classes for localization.
 */
public class LogRes extends ListResourceBundle {

    /** message tag */
    public static final String CANT_LOAD_RES = "01";

    /** message tag */
    public static final String CANT_LOAD_PROPS = "02";

    /** message tag */
    public static final String LOGGING_STARTED = "03";

    /** message tag */
    public static final String CANT_OPEN_LOG = "04";

    /** message tag */
    public static final String BAD_PARAMS = "05";

    /** message tag */
    public static final String MISSING_PARAM = "06";

    /** message tag */
    public static final String BAD_PARAM = "07";

    /** message tag */
    public static final String CANT_CREATE_DIR = "08";

    /**
     * Constructs a new <code>ResourceBundle</code> with the localized resources, or supplies a
     * default set of resources if unable to load.
     *
     * @param   log  a log to which to write diagnostic messages
     * @return  the loaded resource bundle
     */
    public static ResourceBundle getInstance(final Logger log) {

        ResourceBundle res;

        // Load localized resources
        res = ResourceBundle.getBundle(LogRes.class.getName(), Locale.getDefault());

        if (res == null) {
            res = new LogRes();
            log.warning(res.getString(LogRes.CANT_LOAD_RES));
        }

        return res;
    }

    /**
     * Returns an array in which each item is a two-<code>Object</code> array, in which the first
     * element is the key, which must be a <code>String</code>, and the second element is the value
     * associated with that key.
     *
     * @return  the content array
     */
    @Override protected Object[][] getContents() {

        Object[][] contents;

        contents = new Object[][] {

                // Log message when resource bundle is not found and default
                // values are being used in place of loaded properties
                { CANT_LOAD_RES, "Unable to load localized resources - using defaults" },

                // Log message when there is an error loading configuration
                // properties MessageFormat replaces {0} by the directory path
                // from which server attempted to load properties
                { CANT_LOAD_PROPS, "Unable to load properties from \"{0}\"; using defaults." },

                // Log message when file logging is started.
                // MessageFormat replaces {0} with the log file path
                { LOGGING_STARTED, "File logging started in {0}" },

                // Log message when there is an error opening the log file for
                // writing. MessageFormat replaces {0} with the log file path
                // and {1} with the message from the exception that occurred
                // when opening the file
                { CANT_OPEN_LOG, "Unable to open log file \"{0}\": {1}" },

                // Log message when there is an error opening the log file for
                // writing. MessageFormat replaces {0} with the message of the
                // exception generated by the bad parameter
                { BAD_PARAMS, "Invalid logging parameters: {0}" },

                // Log message when a log properties file is missing a
                // specification for some parameter MessageFormat replaces {0}
                // with the parameter that is missing and {1} with the value
                // that was used as a default
                { MISSING_PARAM, "Missing \"{0}\" value, using \"{1}\"" },

                // Log message when a parameter specified in the properties is
                // invalid MessageFormat replaces {0} with the parameter tag,
                // {1} with the value that was found in the properties file,
                // and {2} with the value that was used as a default
                { BAD_PARAM, "Invalid \"{0}\" ({1}), using \"{2}\"" },

                // Log message when there is an error creating the log
                // directory. MessageFormat replaces {0} with the log file
                // path.
                { CANT_CREATE_DIR, "Unable to create log directory \"{0}\"" },
            };

        return contents;
    }
}
