/**
 * Copyright 2013  Simon Arlott
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package eu.lp0.slf4j.android;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Properties;

import org.slf4j.Logger;

/**
 * Loads properties from {@code /eu/lp0/slf4j/android/config.properties}.
 * 
 * <dl>
 * <dt>{@code tag}<strong>.logger.package.name</strong><strong>.logger.class.name</strong>{@code =}<strong>TagName</strong></dt>
 * <dd>Set the tag for the specified logger prefix.</dd>
 * <dt>{@code level}<strong>.logger.package.name</strong><strong>.logger.class.name</strong>{@code =}<strong>LEVEL</strong></dt>
 * <dd>Override the {@linkplain LogLevel log level} for the specified logger prefix.</dd>
 * </dl>
 * 
 * With no configuration, logger names are automatically compacted to fit the Android 23 character tag limit.
 * 
 * @author Simon Arlott
 */
public final class Config {
	private final Logger log = LoggerFactory.getInternalLogger();
	private final CategoryList<String> tag = new CategoryList<String>();
	private final CategoryList<LogLevel> level = new CategoryList<LogLevel>();

	Config() {
		final Properties props = new Properties();
		final URL url = getClass().getResource("config.properties");

		if (url != null) {
			log.debug("Loading properties file from {}", url);

			try {
				props.load(new InputStreamReader(url.openStream()));
			} catch (IOException e) {
				log.error("Error loading properties file from {}", url, e);
				props.clear();
			}
		}

		for (final Entry<Object, Object> entry : props.entrySet()) {
			final String key = (String)entry.getKey();
			final String value = (String)entry.getValue();

			if (key.startsWith("tag")) {
				if (key.length() == 3) {
					if (value.isEmpty() || value.length() > LoggerFactory.MAX_TAG_LEN) {
						log.warn("Ignoring invalid tag {} for {}", value, key);
					} else {
						tag.put("", value);
					}
				} else if (key.charAt(3) == '.') {
					if (value.isEmpty() || value.length() > LoggerFactory.MAX_TAG_LEN) {
						log.warn("Ignoring invalid tag {} for {}", value, key);
					} else {
						tag.put(key.substring(4), value);
					}
				}
			} else if (key.startsWith("level")) {
				try {
					if (key.length() == 5) {
						level.put("", LogLevel.valueOf(value));
					} else if (key.charAt(5) == '.') {
						level.put(key.substring(6), LogLevel.valueOf(value));
					}
				} catch (IllegalArgumentException e) {
					log.warn("Ignoring invalid log level {} for {}", value, key);
				}
			}
		}
	}

	final String getTag(String name) {
		return tag.get(name);
	}

	final LogLevel getLevel(String name) {
		return level.get(name);
	}
}

@@ -2962,34 +2962,35 @@
 ing invalid 
-tag %7B%7D for
+default tag
  %7B%7D%22, value,
@@ -2980,37 +2980,32 @@
 t tag %7B%7D%22, value
-, key
 );%0A%09%09%09%09%09%7D else %7B
@@ -3206,24 +3206,37 @@
 , value, key
+.substring(4)
 );%0A%09%09%09%09%09%7D el
@@ -3338,19 +3338,8 @@
 ) %7B%0A
-%09%09%09%09try %7B%0A%09
 %09%09%09%09
@@ -3359,24 +3359,35 @@
 h() == 5) %7B%0A
+%09%09%09%09%09try %7B%0A
 %09%09%09%09%09%09level.
@@ -3422,24 +3422,138 @@
 ue));%0A%09%09%09%09%09%7D
+ catch (IllegalArgumentException e) %7B%0A%09%09%09%09%09%09log.warn(%22Ignoring invalid default log level %7B%7D%22, value);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
  else if (ke
@@ -3570,24 +3570,35 @@
 ) == '.') %7B%0A
+%09%09%09%09%09try %7B%0A
 %09%09%09%09%09%09level.
@@ -3651,22 +3651,16 @@
 ;%0A%09%09%09%09%09%7D
-%0A%09%09%09%09%7D
  catch (
@@ -3685,24 +3685,25 @@
 eption e) %7B%0A
+%09
 %09%09%09%09%09log.war
@@ -3754,18 +3754,38 @@
 lue, key
-);
+.substring(6));%0A%09%09%09%09%09%7D
 %0A%09%09%09%09%7D%0A%09

improve config error messages