/*
 * Decompiled with CFR 0.152.
 */
package net.minecraft.launcher;

import com.evilco.mc.nbt.stream.NbtInputStream;
import com.evilco.mc.nbt.stream.NbtOutputStream;
import com.evilco.mc.nbt.tag.TagByte;
import com.evilco.mc.nbt.tag.TagCompound;
import com.evilco.mc.nbt.tag.TagList;
import com.evilco.mc.nbt.tag.TagString;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.mc.main.Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ServerManager {
    private static final Logger LOGGER = LogManager.getLogger();

    public static void addServer(Byte hideServer, String serverName, String serverIP) {
        try {
            File serverData = new File(Util.getWorkingDirectory(), "servers.dat");
            if (serverData.exists()) {
                NbtInputStream nis = new NbtInputStream(new FileInputStream(serverData));
                TagCompound tag = (TagCompound)nis.readTag();
                List<TagCompound> tca = tag.getList("servers", TagCompound.class);
                ArrayList<String> currentIPS = new ArrayList<String>();
                for (TagCompound t : tca) {
                    currentIPS.add(t.getString("ip").toLowerCase());
                }
                if (!currentIPS.contains(serverIP.toLowerCase())) {
                    TagCompound toAdd = new TagCompound("");
                    toAdd.setTag(new TagByte("hideAddress", hideServer));
                    toAdd.setTag(new TagString("name", serverName));
                    toAdd.setTag(new TagString("ip", serverIP));
                    TagList tl = new TagList("servers");
                    tl.addTag(toAdd);
                    for (TagCompound tc : tca) {
                        tl.addTag(tc);
                    }
                    TagCompound finalTag = new TagCompound("");
                    finalTag.setTag(tl);
                    ByteArrayOutputStream outputStreamNBT = new ByteArrayOutputStream();
                    NbtOutputStream nbtOutputStream = new NbtOutputStream(outputStreamNBT);
                    nbtOutputStream.write(finalTag);
                    FileOutputStream outputStream = new FileOutputStream(serverData);
                    outputStreamNBT.writeTo(outputStream);
                    LOGGER.info("[Skaia Launcher Log] Added the server " + serverName + " with IP: " + serverIP + " to the server list!");
                } else {
                    LOGGER.info("[Skaia Launcher Log] Could not add the server " + serverName + " with IP: " + serverIP + " to the list as the IP already exists!");
                }
            }
        }
        catch (FileNotFoundException fileNotFoundException) {
        }
        catch (IOException iOException) {
            // empty catch block
        }
    }
}
