package net.minecraft.world.scores;

import java.util.AbstractList;
import org.apache.logging.log4j.LogManager;
import java.util.Iterator;
import java.util.Collection;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import org.apache.logging.log4j.Logger;
import net.minecraft.world.level.saveddata.SavedData;

public class ScoreboardSaveData extends SavedData {
    private static final Logger LOGGER;
    private Scoreboard scoreboard;
    private CompoundTag delayLoad;
    
    public ScoreboardSaveData() {
        super("scoreboard");
    }
    
    public void setScoreboard(final Scoreboard cwl) {
        this.scoreboard = cwl;
        if (this.delayLoad != null) {
            this.load(this.delayLoad);
        }
    }
    
    @Override
    public void load(final CompoundTag jt) {
        if (this.scoreboard == null) {
            this.delayLoad = jt;
            return;
        }
        this.loadObjectives(jt.getList("Objectives", 10));
        this.scoreboard.loadPlayerScores(jt.getList("PlayerScores", 10));
        if (jt.contains("DisplaySlots", 10)) {
            this.loadDisplaySlots(jt.getCompound("DisplaySlots"));
        }
        if (jt.contains("Teams", 9)) {
            this.loadTeams(jt.getList("Teams", 10));
        }
    }
    
    protected void loadTeams(final ListTag jz) {
        for (int integer3 = 0; integer3 < jz.size(); ++integer3) {
            final CompoundTag jt4 = jz.getCompound(integer3);
            String string5 = jt4.getString("Name");
            if (string5.length() > 16) {
                string5 = string5.substring(0, 16);
            }
            final PlayerTeam cwj6 = this.scoreboard.addPlayerTeam(string5);
            final Component lf7 = Component.Serializer.fromJson(jt4.getString("DisplayName"));
            if (lf7 != null) {
                cwj6.setDisplayName(lf7);
            }
            if (jt4.contains("TeamColor", 8)) {
                cwj6.setColor(ChatFormatting.getByName(jt4.getString("TeamColor")));
            }
            if (jt4.contains("AllowFriendlyFire", 99)) {
                cwj6.setAllowFriendlyFire(jt4.getBoolean("AllowFriendlyFire"));
            }
            if (jt4.contains("SeeFriendlyInvisibles", 99)) {
                cwj6.setSeeFriendlyInvisibles(jt4.getBoolean("SeeFriendlyInvisibles"));
            }
            if (jt4.contains("MemberNamePrefix", 8)) {
                final Component lf8 = Component.Serializer.fromJson(jt4.getString("MemberNamePrefix"));
                if (lf8 != null) {
                    cwj6.setPlayerPrefix(lf8);
                }
            }
            if (jt4.contains("MemberNameSuffix", 8)) {
                final Component lf8 = Component.Serializer.fromJson(jt4.getString("MemberNameSuffix"));
                if (lf8 != null) {
                    cwj6.setPlayerSuffix(lf8);
                }
            }
            if (jt4.contains("NameTagVisibility", 8)) {
                final Team.Visibility b8 = Team.Visibility.byName(jt4.getString("NameTagVisibility"));
                if (b8 != null) {
                    cwj6.setNameTagVisibility(b8);
                }
            }
            if (jt4.contains("DeathMessageVisibility", 8)) {
                final Team.Visibility b8 = Team.Visibility.byName(jt4.getString("DeathMessageVisibility"));
                if (b8 != null) {
                    cwj6.setDeathMessageVisibility(b8);
                }
            }
            if (jt4.contains("CollisionRule", 8)) {
                final Team.CollisionRule a8 = Team.CollisionRule.byName(jt4.getString("CollisionRule"));
                if (a8 != null) {
                    cwj6.setCollisionRule(a8);
                }
            }
            this.loadTeamPlayers(cwj6, jt4.getList("Players", 8));
        }
    }
    
    protected void loadTeamPlayers(final PlayerTeam cwj, final ListTag jz) {
        for (int integer4 = 0; integer4 < jz.size(); ++integer4) {
            this.scoreboard.addPlayerToTeam(jz.getString(integer4), cwj);
        }
    }
    
    protected void loadDisplaySlots(final CompoundTag jt) {
        for (int integer3 = 0; integer3 < 19; ++integer3) {
            if (jt.contains("slot_" + integer3, 8)) {
                final String string4 = jt.getString("slot_" + integer3);
                final Objective cwi5 = this.scoreboard.getObjective(string4);
                this.scoreboard.setDisplayObjective(integer3, cwi5);
            }
        }
    }
    
    protected void loadObjectives(final ListTag jz) {
        for (int integer3 = 0; integer3 < jz.size(); ++integer3) {
            final CompoundTag jt4 = jz.getCompound(integer3);
            final CompoundTag compoundTag;
            String string4;
            final Component lf5;
            final ObjectiveCriteria.RenderType a6;
            ObjectiveCriteria.byName(jt4.getString("CriteriaName")).ifPresent(cwo -> {
                string4 = compoundTag.getString("Name");
                if (string4.length() > 16) {
                    string4 = string4.substring(0, 16);
                }
                lf5 = Component.Serializer.fromJson(compoundTag.getString("DisplayName"));
                a6 = ObjectiveCriteria.RenderType.byId(compoundTag.getString("RenderType"));
                this.scoreboard.addObjective(string4, cwo, lf5, a6);
                return;
            });
        }
    }
    
    @Override
    public CompoundTag save(final CompoundTag jt) {
        if (this.scoreboard == null) {
            ScoreboardSaveData.LOGGER.warn("Tried to save scoreboard without having a scoreboard...");
            return jt;
        }
        jt.put("Objectives", this.saveObjectives());
        jt.put("PlayerScores", this.scoreboard.savePlayerScores());
        jt.put("Teams", this.saveTeams());
        this.saveDisplaySlots(jt);
        return jt;
    }
    
    protected ListTag saveTeams() {
        final ListTag jz2 = new ListTag();
        final Collection<PlayerTeam> collection3 = this.scoreboard.getPlayerTeams();
        for (final PlayerTeam cwj5 : collection3) {
            final CompoundTag jt6 = new CompoundTag();
            jt6.putString("Name", cwj5.getName());
            jt6.putString("DisplayName", Component.Serializer.toJson(cwj5.getDisplayName()));
            if (cwj5.getColor().getId() >= 0) {
                jt6.putString("TeamColor", cwj5.getColor().getName());
            }
            jt6.putBoolean("AllowFriendlyFire", cwj5.isAllowFriendlyFire());
            jt6.putBoolean("SeeFriendlyInvisibles", cwj5.canSeeFriendlyInvisibles());
            jt6.putString("MemberNamePrefix", Component.Serializer.toJson(cwj5.getPlayerPrefix()));
            jt6.putString("MemberNameSuffix", Component.Serializer.toJson(cwj5.getPlayerSuffix()));
            jt6.putString("NameTagVisibility", cwj5.getNameTagVisibility().name);
            jt6.putString("DeathMessageVisibility", cwj5.getDeathMessageVisibility().name);
            jt6.putString("CollisionRule", cwj5.getCollisionRule().name);
            final ListTag jz3 = new ListTag();
            for (final String string9 : cwj5.getPlayers()) {
                ((AbstractList<StringTag>)jz3).add(StringTag.valueOf(string9));
            }
            jt6.put("Players", jz3);
            ((AbstractList<CompoundTag>)jz2).add(jt6);
        }
        return jz2;
    }
    
    protected void saveDisplaySlots(final CompoundTag jt) {
        final CompoundTag jt2 = new CompoundTag();
        boolean boolean4 = false;
        for (int integer5 = 0; integer5 < 19; ++integer5) {
            final Objective cwi6 = this.scoreboard.getDisplayObjective(integer5);
            if (cwi6 != null) {
                jt2.putString("slot_" + integer5, cwi6.getName());
                boolean4 = true;
            }
        }
        if (boolean4) {
            jt.put("DisplaySlots", jt2);
        }
    }
    
    protected ListTag saveObjectives() {
        final ListTag jz2 = new ListTag();
        final Collection<Objective> collection3 = this.scoreboard.getObjectives();
        for (final Objective cwi5 : collection3) {
            if (cwi5.getCriteria() == null) {
                continue;
            }
            final CompoundTag jt6 = new CompoundTag();
            jt6.putString("Name", cwi5.getName());
            jt6.putString("CriteriaName", cwi5.getCriteria().getName());
            jt6.putString("DisplayName", Component.Serializer.toJson(cwi5.getDisplayName()));
            jt6.putString("RenderType", cwi5.getRenderType().getId());
            ((AbstractList<CompoundTag>)jz2).add(jt6);
        }
        return jz2;
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
