/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.velocitycore;

import de.steamwar.sql.SWException;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.AbstractConfiguration;
import org.apache.logging.log4j.core.config.Property;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;

public class ErrorLogger extends AbstractAppender {

    ErrorLogger() {
        super("SW ErrorLogger", null, null, false, Property.EMPTY_ARRAY);
        start();

        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        context.getConfiguration().addAppender(this);
        context.getRootLogger().addAppender(this);
        context.updateLoggers();
    }

    void unregister() {
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        ((AbstractConfiguration) context.getConfiguration()).removeAppender(getName());
        context.getRootLogger().removeAppender(this);
        context.updateLoggers();
    }

    @Override
    public void append(LogEvent event) {
        if (event.getLevel().intLevel() > Level.WARN.intLevel()) return;

        String message = event.getMessage().getFormattedMessage();
        for (String reason : filteredMessages) {
            if (message.contains(reason)) return;
        }

        ByteArrayOutputStream stacktraceOutput = new ByteArrayOutputStream();
        if (event.getThrown() != null)
            event.getThrown().printStackTrace(new PrintStream(stacktraceOutput));
        String stacktrace = stacktraceOutput.toString();
        for (String reason : filteredStacktraces) {
            if (stacktrace.contains(reason)) return;
        }

        SWException.log(message, stacktrace);
    }

    private static final List<String> filteredMessages = List.of(
            "read timed out",
            "disconnected while connecting to Lobby"
    );
    private static final List<String> filteredStacktraces = List.of(
            "ErrorLogger",
            "Connection reset by peer"
    );
}
