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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;

@Plugin(
   name = "DatePatternConverter",
   category = "Converter"
)
@ConverterKeys({"d", "date"})
public final class DatePatternConverter extends LogEventPatternConverter implements ArrayPatternConverter {
   private static final String ABSOLUTE_FORMAT = "ABSOLUTE";
   private static final String ABSOLUTE_TIME_PATTERN = "HH:mm:ss,SSS";
   private static final String COMPACT_FORMAT = "COMPACT";
   private static final String COMPACT_PATTERN = "yyyyMMddHHmmssSSS";
   private static final String DATE_AND_TIME_FORMAT = "DATE";
   private static final String DATE_AND_TIME_PATTERN = "dd MMM yyyy HH:mm:ss,SSS";
   private static final String DEFAULT_FORMAT = "DEFAULT";
   static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
   private static final String ISO8601_BASIC_FORMAT = "ISO8601_BASIC";
   private static final String ISO8601_BASIC_PATTERN = "yyyyMMdd'T'HHmmss,SSS";
   static final String ISO8601_FORMAT = "ISO8601";
   static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss,SSS";
   private static final String UNIX_FORMAT = "UNIX";
   private static final String UNIX_MILLIS_FORMAT = "UNIX_MILLIS";
   private String cachedDateString;
   private final DatePatternConverter.Formatter formatter;
   private long lastTimestamp;

   public static DatePatternConverter newInstance(String[] options) {
      return new DatePatternConverter(options);
   }

   private DatePatternConverter(String[] options) {
      super("Date", "date");
      String patternOption = options != null && options.length > 0 ? options[0] : null;
      String pattern = null;
      DatePatternConverter.Formatter tempFormatter = null;
      if (patternOption != null && !patternOption.equalsIgnoreCase("DEFAULT")) {
         if (patternOption.equalsIgnoreCase("ISO8601")) {
            pattern = "yyyy-MM-dd'T'HH:mm:ss,SSS";
         } else if (patternOption.equalsIgnoreCase("ISO8601_BASIC")) {
            pattern = "yyyyMMdd'T'HHmmss,SSS";
         } else if (patternOption.equalsIgnoreCase("ABSOLUTE")) {
            pattern = "HH:mm:ss,SSS";
         } else if (patternOption.equalsIgnoreCase("DATE")) {
            pattern = "dd MMM yyyy HH:mm:ss,SSS";
         } else if (patternOption.equalsIgnoreCase("COMPACT")) {
            pattern = "yyyyMMddHHmmssSSS";
         } else if (patternOption.equalsIgnoreCase("UNIX")) {
            tempFormatter = new DatePatternConverter.UnixFormatter();
         } else if (patternOption.equalsIgnoreCase("UNIX_MILLIS")) {
            tempFormatter = new DatePatternConverter.UnixMillisFormatter();
         } else {
            pattern = patternOption;
         }
      } else {
         pattern = "yyyy-MM-dd HH:mm:ss,SSS";
      }

      if (pattern != null) {
         SimpleDateFormat tempFormat;
         try {
            tempFormat = new SimpleDateFormat(pattern);
         } catch (IllegalArgumentException var7) {
            LOGGER.warn((String)("Could not instantiate SimpleDateFormat with pattern " + patternOption), (Throwable)var7);
            tempFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
         }

         if (options != null && options.length > 1) {
            TimeZone tz = TimeZone.getTimeZone(options[1]);
            tempFormat.setTimeZone(tz);
         }

         tempFormatter = new DatePatternConverter.PatternFormatter(tempFormat);
      }

      this.formatter = (DatePatternConverter.Formatter)tempFormatter;
   }

   public void format(Date date, StringBuilder toAppendTo) {
      synchronized(this) {
         toAppendTo.append(this.formatter.format(date.getTime()));
      }
   }

   public void format(LogEvent event, StringBuilder output) {
      long timestamp = event.getTimeMillis();
      synchronized(this) {
         if (timestamp != this.lastTimestamp) {
            this.lastTimestamp = timestamp;
            this.cachedDateString = this.formatter.format(timestamp);
         }
      }

      output.append(this.cachedDateString);
   }

   public void format(Object obj, StringBuilder output) {
      if (obj instanceof Date) {
         this.format((Date)obj, output);
      }

      super.format(obj, output);
   }

   public void format(StringBuilder toAppendTo, Object... objects) {
      Object[] arr$ = objects;
      int len$ = objects.length;

      for(int i$ = 0; i$ < len$; ++i$) {
         Object obj = arr$[i$];
         if (obj instanceof Date) {
            this.format(obj, toAppendTo);
            break;
         }
      }

   }

   public String getPattern() {
      return this.formatter.toPattern();
   }

   private static class UnixMillisFormatter extends DatePatternConverter.Formatter {
      private UnixMillisFormatter() {
         super(null);
      }

      String format(long time) {
         return Long.toString(time);
      }

      // $FF: synthetic method
      UnixMillisFormatter(Object x0) {
         this();
      }
   }

   private static class UnixFormatter extends DatePatternConverter.Formatter {
      private UnixFormatter() {
         super(null);
      }

      String format(long time) {
         return Long.toString(time / 1000L);
      }

      // $FF: synthetic method
      UnixFormatter(Object x0) {
         this();
      }
   }

   private static class PatternFormatter extends DatePatternConverter.Formatter {
      private final SimpleDateFormat simpleDateFormat;

      PatternFormatter(SimpleDateFormat simpleDateFormat) {
         super(null);
         this.simpleDateFormat = simpleDateFormat;
      }

      String format(long time) {
         return this.simpleDateFormat.format(time);
      }

      public String toPattern() {
         return this.simpleDateFormat.toPattern();
      }
   }

   private abstract static class Formatter {
      private Formatter() {
      }

      abstract String format(long var1);

      public String toPattern() {
         return null;
      }

      // $FF: synthetic method
      Formatter(Object x0) {
         this();
      }
   }
}
