package org.apache.logging.log4j.core.filter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.StructuredDataMessage;

@Plugin(
   name = "StructuredDataFilter",
   category = "Core",
   elementType = "filter",
   printObject = true
)
public final class StructuredDataFilter extends MapFilter {
   private StructuredDataFilter(Map<String, List<String>> map, boolean oper, Filter.Result onMatch, Filter.Result onMismatch) {
      super(map, oper, onMatch, onMismatch);
   }

   public Filter.Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
      return msg instanceof StructuredDataMessage ? this.filter((StructuredDataMessage)msg) : Filter.Result.NEUTRAL;
   }

   public Filter.Result filter(LogEvent event) {
      Message msg = event.getMessage();
      return msg instanceof StructuredDataMessage ? this.filter((StructuredDataMessage)msg) : super.filter(event);
   }

   protected Filter.Result filter(StructuredDataMessage message) {
      boolean match = false;
      Iterator i$ = this.getMap().entrySet().iterator();

      while(i$.hasNext()) {
         Entry<String, List<String>> entry = (Entry)i$.next();
         String toMatch = this.getValue(message, (String)entry.getKey());
         if (toMatch != null) {
            match = ((List)entry.getValue()).contains(toMatch);
         } else {
            match = false;
         }

         if (!this.isAnd() && match || this.isAnd() && !match) {
            break;
         }
      }

      return match ? this.onMatch : this.onMismatch;
   }

   private String getValue(StructuredDataMessage data, String key) {
      if (key.equalsIgnoreCase("id")) {
         return data.getId().toString();
      } else if (key.equalsIgnoreCase("id.name")) {
         return data.getId().getName();
      } else if (key.equalsIgnoreCase("type")) {
         return data.getType();
      } else {
         return key.equalsIgnoreCase("message") ? data.getFormattedMessage() : (String)data.getData().get(key);
      }
   }

   @PluginFactory
   public static StructuredDataFilter createFilter(@PluginElement("Pairs") KeyValuePair[] pairs, @PluginAttribute("operator") String oper, @PluginAttribute("onMatch") Filter.Result match, @PluginAttribute("onMismatch") Filter.Result mismatch) {
      if (pairs != null && pairs.length != 0) {
         Map<String, List<String>> map = new HashMap();
         KeyValuePair[] arr$ = pairs;
         int len$ = pairs.length;

         for(int i$ = 0; i$ < len$; ++i$) {
            KeyValuePair pair = arr$[i$];
            String key = pair.getKey();
            if (key == null) {
               LOGGER.error("A null key is not valid in MapFilter");
            } else {
               String value = pair.getValue();
               if (value == null) {
                  LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
               } else {
                  List<String> list = (List)map.get(pair.getKey());
                  if (list != null) {
                     list.add(value);
                  } else {
                     List<String> list = new ArrayList();
                     list.add(value);
                     map.put(pair.getKey(), list);
                  }
               }
            }
         }

         if (map.isEmpty()) {
            LOGGER.error("StructuredDataFilter is not configured with any valid key value pairs");
            return null;
         } else {
            boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
            return new StructuredDataFilter(map, isAnd, match, mismatch);
         }
      } else {
         LOGGER.error("keys and values must be specified for the StructuredDataFilter");
         return null;
      }
   }
}
