package me.km.snuviscript;

import me.hammerle.snuviscript.code.ISnuviLogger;
import me.hammerle.snuviscript.code.Script;
import me.hammerle.snuviscript.exceptions.StackTrace;
import me.km.permissions.Permissions;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.StringTextComponent;

public class SnuviLogger implements ISnuviLogger {
    private boolean printErrorToConsole = true;
    private boolean printDebugToConsole = true;
    private MinecraftServer server = null;
    private Permissions perms = null;
    private final RingArray<String> debugHistory = new RingArray(100);
    private final RingArray<String> errorHistory = new RingArray(100);

    public void setConsoleErrorLogging(boolean b) {
        printErrorToConsole = b;
    }

    public void setConsoleDebugLogging(boolean b) {
        printDebugToConsole = b;
    }

    public void setPlayerLoggingData(MinecraftServer server, Permissions perms) {
        this.server = server;
        this.perms = perms;
    }

    public RingArray<String> getDebugHistory() {
        return debugHistory;
    }

    public RingArray<String> getErrorHistory() {
        return errorHistory;
    }

    @Override
    public void print(String message, Exception ex, String function, String scriptname, Script sc, StackTrace lines) {
        StringBuilder sb = new StringBuilder();
        sb.append("[§cLogger§r] ");

        String color;
        if(ex == null) {
            color = "§e";
            sb.append(color).append(message).append("§r");
        } else {
            color = "§c";
            sb.append(color).append(ex.getClass().getSimpleName()).append("§r: '").append(color).append(ex.getMessage());
            if(message != null && !message.isEmpty()) {
                sb.append(" - ").append(message);
            }
            sb.append("§r'");
        }
        if(scriptname != null && !scriptname.isEmpty()) {
            sb.append(" in script '").append(color).append(scriptname).append("§r'");
        }
        if(sc != null) {
            sb.append(" id '").append(color).append(sc.getId()).append("§r'");
        }
        if(function != null && !function.isEmpty()) {
            sb.append(" in function '").append(color).append(function).append("§r'");
        }
        if(lines != null) {
            sb.append(" in line '").append(color).append(lines).append("§r'");
        }

        String s = sb.toString();
        StringTextComponent text = new StringTextComponent(s);
        if(ex == null) {
            debugHistory.add(s);
            if(printDebugToConsole && server != null) {
                server.sendMessage(text);
            }
        } else {
            errorHistory.add(s);
            if(printErrorToConsole && server != null) {
                server.sendMessage(text);
            }
        }
        if(server != null && perms != null && server.getPlayerList() != null) {
            server.getPlayerList().getPlayers().forEach(p -> {
                if(perms.has(p, "script.error")) {
                    p.sendMessage(text);
                }
            });
        }
    }
}
