package org.ctrip.ops.sysdev.filters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.ctrip.ops.sysdev.utils.dateparser.DateParser;
import org.ctrip.ops.sysdev.utils.dateparser.FormatParser;
import org.ctrip.ops.sysdev.utils.dateparser.UnixMSParser;
import org.ctrip.ops.sysdev.utils.dateparser.UnixParser;
import org.jcodings.specific.UTF8Encoding;
import org.joni.Matcher;
import org.joni.Option;
import org.joni.Regex;
import org.joni.Region;

import scala.Tuple2;

public class Date extends BaseFilter {
	private static final Logger logger = Logger.getLogger(Date.class.getName());

	private String tagOnFailure;
	private String src;
	private String target;
	private boolean addYear;
	private List<DateParser> parsers;

	public Date(Map config, ArrayBlockingQueue preQueue) {
		super(config, preQueue);
	}

	@SuppressWarnings("unchecked")
	protected void prepare() {

		if (config.containsKey("src")) {
			this.src = (String) config.get("src");
		} else {
			this.src = "logtime";
		}

		if (config.containsKey("target")) {
			this.target = (String) config.get("target");
		} else {
			this.target = "@timestamp";
		}

		if (config.containsKey("tag_on_failure")) {
			this.tagOnFailure = (String) config.get("tag_on_failure");
		} else {
			this.tagOnFailure = "datefail";
		}

		if (config.containsKey("add_year")) {
			this.addYear = (boolean) config.get("add_year");
		} else {
			this.addYear = false;
		}
		this.parsers = new ArrayList<DateParser>();
		for (String format : (List<String>) config.get("formats")) {
			if (format.equalsIgnoreCase("UNIX")) {
				parsers.add(new UnixParser());
			} else if (format.equalsIgnoreCase("UNIXMS")) {
				parsers.add(new UnixMSParser());
			} else {
				parsers.add(new FormatParser(format, (String) config
						.get("timezone"), (String) config.get("locale")));
			}
		}
	};

	@Override
	protected void filter(Map event) {
		if (!event.containsKey(this.src)) {
			return;
		}

		boolean success = false;

		String input = ((String) event.get(this.src));

		if (addYear) {
			input = Calendar.getInstance().get(Calendar.YEAR) + input;
		}

		for (DateParser parser : this.parsers) {
			try {
				event.put(target, parser.parse(input));
				success = true;
				break;
			} catch (Exception e) {
				logger.trace(e.getMessage());
			}
		}

		if (success == false) {
			if (!event.containsKey("tags")) {
				event.put("tags",
						new ArrayList<String>(Arrays.asList(this.tagOnFailure)));
			} else {
				Object tags = event.get("tags");
				if (tags.getClass() == ArrayList.class
						&& ((ArrayList) tags).indexOf(this.tagOnFailure) == -1) {
					((ArrayList) tags).add(this.tagOnFailure);
				}
			}
		}
	};

}

@@ -116,34 +116,8 @@
 ar;%0A
-import java.util.HashMap;%0A
 impo
@@ -131,24 +131,24 @@
 .util.List;%0A
+
 import java.
@@ -208,72 +208,8 @@
 eue;
-%0Aimport java.util.logging.Level;%0Aimport java.util.regex.Pattern;
 %0A%0Aim
@@ -405,32 +405,32 @@
 r.UnixMSParser;%0A
+
 import org.ctrip
@@ -473,169 +473,8 @@
 ser;
-%0Aimport org.jcodings.specific.UTF8Encoding;%0Aimport org.joni.Matcher;%0Aimport org.joni.Option;%0Aimport org.joni.Regex;%0Aimport org.joni.Region;%0A%0Aimport scala.Tuple2;
 %0A%0Apu

remove unused import