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

package com.hypixel.hytale.logger;

import com.google.common.flogger.parser.DefaultPrintfMessageParser;
import com.google.common.flogger.parser.MessageParser;
import com.google.common.flogger.LogContext;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.LogManager;
import com.hypixel.hytale.logger.backend.HytaleUncaughtExceptionHandler;
import com.hypixel.hytale.logger.backend.HytaleLogManager;
import com.google.common.flogger.LoggingApi;
import io.sentry.IScopes;
import com.google.common.flogger.backend.LoggerBackend;
import com.google.common.flogger.backend.Platform;
import java.io.PrintStream;
import com.hypixel.hytale.logger.util.LoggerPrintStream;
import java.util.logging.Level;
import com.hypixel.hytale.logger.backend.HytaleConsole;
import com.hypixel.hytale.logger.backend.HytaleFileHandler;
import javax.annotation.Nonnull;
import com.hypixel.hytale.logger.backend.HytaleLoggerBackend;
import java.util.Map;
import com.google.common.flogger.AbstractLogger;

public class HytaleLogger extends AbstractLogger<Api>
{
    private static final Map<String, HytaleLogger> CACHE;
    private static final HytaleLogger LOGGER;
    static final NoOp NO_OP;
    @Nonnull
    private final HytaleLoggerBackend backend;
    
    public static void init() {
        final HytaleFileHandler fileHandler = HytaleFileHandler.INSTANCE;
        final HytaleConsole console = HytaleConsole.INSTANCE;
        HytaleLogger.LOGGER.at(Level.INFO).log("Logger Initialized");
    }
    
    public static void replaceStd() {
        if (!HytaleLoggerBackend.isJunitTest()) {
            System.setOut(new LoggerPrintStream(get("SOUT"), Level.INFO));
            System.setErr(new LoggerPrintStream(get("SERR"), Level.SEVERE));
        }
    }
    
    public static HytaleLogger getLogger() {
        return HytaleLogger.LOGGER;
    }
    
    @Nonnull
    public static HytaleLogger forEnclosingClass() {
        final String className = Platform.getCallerFinder().findLoggingClass(HytaleLogger.class);
        final String loggerName = classToLoggerName(className);
        return get(loggerName);
    }
    
    @Nonnull
    public static HytaleLogger forEnclosingClassFull() {
        final String loggingClass = Platform.getCallerFinder().findLoggingClass(HytaleLogger.class);
        return get(loggingClass);
    }
    
    @Nonnull
    public static HytaleLogger get(final String loggerName) {
        return HytaleLogger.CACHE.computeIfAbsent(loggerName, key -> new HytaleLogger(HytaleLoggerBackend.getLogger(key)));
    }
    
    private HytaleLogger(@Nonnull final HytaleLoggerBackend backend) {
        super(backend);
        this.backend = backend;
    }
    
    @Override
    public Api at(@Nonnull final Level level) {
        return this.isLoggable(level) ? new Context(level) : HytaleLogger.NO_OP;
    }
    
    public String getName() {
        return super.getName();
    }
    
    @Nonnull
    public Level getLevel() {
        return this.backend.getLevel();
    }
    
    public void setLevel(@Nonnull final Level level) {
        this.backend.setLevel(level);
    }
    
    @Nonnull
    public HytaleLogger getSubLogger(final String name) {
        return new HytaleLogger(this.backend.getSubLogger(name));
    }
    
    public void setSentryClient(@Nonnull final IScopes scope) {
        this.backend.setSentryClient(scope);
    }
    
    public void setPropagatesSentryToParent(final boolean propagate) {
        this.backend.setPropagatesSentryToParent(propagate);
    }
    
    @Nonnull
    private static String classToLoggerName(@Nonnull final String className) {
        final int lastIndexOf = className.lastIndexOf(46);
        String loggerName;
        if (lastIndexOf < 0 || className.length() <= lastIndexOf + 1) {
            loggerName = className;
        }
        else {
            loggerName = className.substring(lastIndexOf + 1);
        }
        return loggerName;
    }
    
    static {
        System.setProperty("java.util.logging.manager", HytaleLogManager.class.getName());
        HytaleUncaughtExceptionHandler.setup();
        final LogManager logManager = LogManager.getLogManager();
        if (!logManager.getClass().getName().equals(HytaleLogManager.class.getName())) {
            throw new IllegalStateException("""
                                            Log manager wasn't set! Please ensure HytaleLogger is the first logger to be initialized or
                                            use `System.setProperty("java.util.logging.manager", HytaleLogManager.class.getName());` at the start of your application.
                                            Log manager is: """ + String.valueOf(logManager));
        }
        CACHE = new ConcurrentHashMap<String, HytaleLogger>();
        LOGGER = new HytaleLogger(HytaleLoggerBackend.getLogger());
        NO_OP = new NoOp();
    }
    
    private static final class NoOp extends LoggingApi.NoOp<Api> implements Api
    {
    }
    
    final class Context extends LogContext<HytaleLogger, Api> implements Api
    {
        private Context(final Level level) {
            super(level, false);
        }
        
        @Nonnull
        @Override
        protected HytaleLogger getLogger() {
            return HytaleLogger.this;
        }
        
        @Nonnull
        @Override
        protected Api api() {
            return this;
        }
        
        @Nonnull
        @Override
        protected Api noOp() {
            return HytaleLogger.NO_OP;
        }
        
        @Override
        protected MessageParser getMessageParser() {
            return DefaultPrintfMessageParser.getInstance();
        }
    }
    
    public interface Api extends LoggingApi<Api>
    {
    }
}
