package net.minecraft.client.multiplayer;

import java.util.AbstractList;
import org.apache.logging.log4j.LogManager;
import java.util.Iterator;
import net.minecraft.Util;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import java.io.File;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.client.Minecraft;
import org.apache.logging.log4j.Logger;

public class ServerList {
    private static final Logger LOGGER;
    private final Minecraft minecraft;
    private final List<ServerData> serverList;
    
    public ServerList(final Minecraft dlx) {
        this.serverList = Lists.newArrayList();
        this.minecraft = dlx;
        this.load();
    }
    
    public void load() {
        try {
            this.serverList.clear();
            final CompoundTag le2 = NbtIo.read(new File(this.minecraft.gameDirectory, "servers.dat"));
            if (le2 == null) {
                return;
            }
            final ListTag lk3 = le2.getList("servers", 10);
            for (int integer4 = 0; integer4 < lk3.size(); ++integer4) {
                this.serverList.add(ServerData.read(lk3.getCompound(integer4)));
            }
        }
        catch (Exception exception2) {
            ServerList.LOGGER.error("Couldn't load server list", (Throwable)exception2);
        }
    }
    
    public void save() {
        try {
            final ListTag lk2 = new ListTag();
            for (final ServerData dys4 : this.serverList) {
                ((AbstractList<CompoundTag>)lk2).add(dys4.write());
            }
            final CompoundTag le3 = new CompoundTag();
            le3.put("servers", lk2);
            final File file4 = File.createTempFile("servers", ".dat", this.minecraft.gameDirectory);
            NbtIo.write(le3, file4);
            final File file5 = new File(this.minecraft.gameDirectory, "servers.dat_old");
            final File file6 = new File(this.minecraft.gameDirectory, "servers.dat");
            Util.safeReplaceFile(file6, file4, file5);
        }
        catch (Exception exception2) {
            ServerList.LOGGER.error("Couldn't save server list", (Throwable)exception2);
        }
    }
    
    public ServerData get(final int integer) {
        return this.serverList.get(integer);
    }
    
    public void remove(final ServerData dys) {
        this.serverList.remove(dys);
    }
    
    public void add(final ServerData dys) {
        this.serverList.add(dys);
    }
    
    public int size() {
        return this.serverList.size();
    }
    
    public void swap(final int integer1, final int integer2) {
        final ServerData dys4 = this.get(integer1);
        this.serverList.set(integer1, this.get(integer2));
        this.serverList.set(integer2, dys4);
        this.save();
    }
    
    public void replace(final int integer, final ServerData dys) {
        this.serverList.set(integer, dys);
    }
    
    public static void saveSingleServer(final ServerData dys) {
        final ServerList dyt2 = new ServerList(Minecraft.getInstance());
        dyt2.load();
        for (int integer3 = 0; integer3 < dyt2.size(); ++integer3) {
            final ServerData dys2 = dyt2.get(integer3);
            if (dys2.name.equals(dys.name) && dys2.ip.equals(dys.ip)) {
                dyt2.replace(integer3, dys);
                break;
            }
        }
        dyt2.save();
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
