// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.logger.backend;

import java.time.temporal.TemporalAccessor;
import java.time.ZoneId;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.logging.Level;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.annotation.Nonnull;
import java.util.logging.LogRecord;
import java.util.function.BooleanSupplier;
import java.util.regex.Pattern;
import java.time.format.DateTimeFormatter;
import java.util.logging.Formatter;

public class HytaleLogFormatter extends Formatter
{
    private static final DateTimeFormatter DATE_FORMATTER;
    private static final Pattern ANSI_CONTROL_CODES;
    private BooleanSupplier ansi;
    public int maxModuleName;
    private int shorterCount;
    
    public HytaleLogFormatter(final BooleanSupplier ansi) {
        this.ansi = ansi;
    }
    
    @Nonnull
    @Override
    public String format(@Nonnull final LogRecord record) {
        String message = record.getMessage();
        if (message == null) {
            message = "null";
        }
        if (record.getParameters() != null && record.getParameters().length > 0) {
            try {
                message = String.format(message, record.getParameters());
            }
            catch (final RuntimeException e) {
                throw new IllegalArgumentException("Error logging using format string: " + record.getMessage(), (Throwable)e);
            }
        }
        if (record.getThrown() != null) {
            final StringWriter writer = new StringWriter();
            record.getThrown().printStackTrace(new PrintWriter(writer));
            message = message + "\n" + writer.toString();
        }
        final boolean ansi = this.ansi.getAsBoolean();
        if (ansi) {
            message += "\u001b[m";
        }
        if (!(record instanceof HytaleLoggerBackend.RawLogRecord)) {
            final String loggerName = record.getLoggerName();
            final int moduleNameTextSize = loggerName.length() + 3;
            if ((moduleNameTextSize > this.maxModuleName && moduleNameTextSize < 35) || this.shorterCount > 500) {
                this.maxModuleName = moduleNameTextSize;
                this.shorterCount = 0;
            }
            else if (moduleNameTextSize < this.maxModuleName) {
                ++this.shorterCount;
            }
            final StringBuilder sb = new StringBuilder(33 + this.maxModuleName + message.length());
            if (ansi) {
                String color = null;
                final int level = record.getLevel().intValue();
                if (level <= Level.ALL.intValue()) {
                    color = "\u001b[37m";
                }
                else if (level <= Level.FINEST.intValue()) {
                    color = "\u001b[36m";
                }
                else if (level <= Level.FINER.intValue()) {
                    color = "\u001b[34m";
                }
                else if (level <= Level.FINE.intValue()) {
                    color = "\u001b[35m";
                }
                else if (level <= Level.CONFIG.intValue()) {
                    color = "\u001b[32m";
                }
                else if (level <= Level.INFO.intValue()) {
                    color = "\u001b[m";
                }
                else if (level <= Level.WARNING.intValue()) {
                    color = "\u001b[33m";
                }
                else if (level <= Level.SEVERE.intValue()) {
                    color = "\u001b[31m";
                }
                if (color != null) {
                    sb.append(color);
                }
            }
            sb.append('[');
            HytaleLogFormatter.DATE_FORMATTER.formatTo(LocalDateTime.ofInstant(record.getInstant(), ZoneOffset.UTC), sb);
            sb.append(' ');
            String level2;
            if (Level.WARNING.equals(record.getLevel())) {
                level2 = "WARN";
            }
            else {
                level2 = record.getLevel().getName();
            }
            final int levelLength = level2.length();
            if (levelLength < 6) {
                sb.append(" ".repeat(6 - levelLength));
                sb.append(level2);
            }
            else {
                sb.append(level2, 0, 6);
            }
            sb.append("] ");
            sb.append(" ".repeat(Math.max(0, this.maxModuleName - moduleNameTextSize)));
            sb.append('[').append(loggerName).append("] ").append(ansi ? message : stripAnsi(message)).append('\n');
            return sb.toString();
        }
        if (!ansi) {
            return stripAnsi(message);
        }
        return message;
    }
    
    public static String stripAnsi(@Nonnull final String message) {
        return HytaleLogFormatter.ANSI_CONTROL_CODES.matcher(message).replaceAll("");
    }
    
    static {
        DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        ANSI_CONTROL_CODES = Pattern.compile("\u001b\\[[;\\d]*m");
    }
}
