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 dfm) {
        this.scoreboard = dfm;
        if (this.delayLoad != null) {
            this.load(this.delayLoad);
        }
    }
    
    @Override
    public void load(final CompoundTag le) {
        if (this.scoreboard == null) {
            this.delayLoad = le;
            return;
        }
        this.loadObjectives(le.getList("Objectives", 10));
        this.scoreboard.loadPlayerScores(le.getList("PlayerScores", 10));
        if (le.contains("DisplaySlots", 10)) {
            this.loadDisplaySlots(le.getCompound("DisplaySlots"));
        }
        if (le.contains("Teams", 9)) {
            this.loadTeams(le.getList("Teams", 10));
        }
    }
    
    protected void loadTeams(final ListTag lk) {
        for (int integer3 = 0; integer3 < lk.size(); ++integer3) {
            final CompoundTag le4 = lk.getCompound(integer3);
            String string5 = le4.getString("Name");
            if (string5.length() > 16) {
                string5 = string5.substring(0, 16);
            }
            final PlayerTeam dfk6 = this.scoreboard.addPlayerTeam(string5);
            final Component mr7 = Component.Serializer.fromJson(le4.getString("DisplayName"));
            if (mr7 != null) {
                dfk6.setDisplayName(mr7);
            }
            if (le4.contains("TeamColor", 8)) {
                dfk6.setColor(ChatFormatting.getByName(le4.getString("TeamColor")));
            }
            if (le4.contains("AllowFriendlyFire", 99)) {
                dfk6.setAllowFriendlyFire(le4.getBoolean("AllowFriendlyFire"));
            }
            if (le4.contains("SeeFriendlyInvisibles", 99)) {
                dfk6.setSeeFriendlyInvisibles(le4.getBoolean("SeeFriendlyInvisibles"));
            }
            if (le4.contains("MemberNamePrefix", 8)) {
                final Component mr8 = Component.Serializer.fromJson(le4.getString("MemberNamePrefix"));
                if (mr8 != null) {
                    dfk6.setPlayerPrefix(mr8);
                }
            }
            if (le4.contains("MemberNameSuffix", 8)) {
                final Component mr8 = Component.Serializer.fromJson(le4.getString("MemberNameSuffix"));
                if (mr8 != null) {
                    dfk6.setPlayerSuffix(mr8);
                }
            }
            if (le4.contains("NameTagVisibility", 8)) {
                final Team.Visibility b8 = Team.Visibility.byName(le4.getString("NameTagVisibility"));
                if (b8 != null) {
                    dfk6.setNameTagVisibility(b8);
                }
            }
            if (le4.contains("DeathMessageVisibility", 8)) {
                final Team.Visibility b8 = Team.Visibility.byName(le4.getString("DeathMessageVisibility"));
                if (b8 != null) {
                    dfk6.setDeathMessageVisibility(b8);
                }
            }
            if (le4.contains("CollisionRule", 8)) {
                final Team.CollisionRule a8 = Team.CollisionRule.byName(le4.getString("CollisionRule"));
                if (a8 != null) {
                    dfk6.setCollisionRule(a8);
                }
            }
            this.loadTeamPlayers(dfk6, le4.getList("Players", 8));
        }
    }
    
    protected void loadTeamPlayers(final PlayerTeam dfk, final ListTag lk) {
        for (int integer4 = 0; integer4 < lk.size(); ++integer4) {
            this.scoreboard.addPlayerToTeam(lk.getString(integer4), dfk);
        }
    }
    
    protected void loadDisplaySlots(final CompoundTag le) {
        for (int integer3 = 0; integer3 < 19; ++integer3) {
            if (le.contains("slot_" + integer3, 8)) {
                final String string4 = le.getString("slot_" + integer3);
                final Objective dfj5 = this.scoreboard.getObjective(string4);
                this.scoreboard.setDisplayObjective(integer3, dfj5);
            }
        }
    }
    
    protected void loadObjectives(final ListTag lk) {
        for (int integer3 = 0; integer3 < lk.size(); ++integer3) {
            final CompoundTag le4 = lk.getCompound(integer3);
            final CompoundTag compoundTag;
            String string4;
            final Component mr5;
            final ObjectiveCriteria.RenderType a6;
            ObjectiveCriteria.byName(le4.getString("CriteriaName")).ifPresent(dfp -> {
                string4 = compoundTag.getString("Name");
                if (string4.length() > 16) {
                    string4 = string4.substring(0, 16);
                }
                mr5 = Component.Serializer.fromJson(compoundTag.getString("DisplayName"));
                a6 = ObjectiveCriteria.RenderType.byId(compoundTag.getString("RenderType"));
                this.scoreboard.addObjective(string4, dfp, mr5, a6);
                return;
            });
        }
    }
    
    @Override
    public CompoundTag save(final CompoundTag le) {
        if (this.scoreboard == null) {
            ScoreboardSaveData.LOGGER.warn("Tried to save scoreboard without having a scoreboard...");
            return le;
        }
        le.put("Objectives", this.saveObjectives());
        le.put("PlayerScores", this.scoreboard.savePlayerScores());
        le.put("Teams", this.saveTeams());
        this.saveDisplaySlots(le);
        return le;
    }
    
    protected ListTag saveTeams() {
        final ListTag lk2 = new ListTag();
        final Collection<PlayerTeam> collection3 = this.scoreboard.getPlayerTeams();
        for (final PlayerTeam dfk5 : collection3) {
            final CompoundTag le6 = new CompoundTag();
            le6.putString("Name", dfk5.getName());
            le6.putString("DisplayName", Component.Serializer.toJson(dfk5.getDisplayName()));
            if (dfk5.getColor().getId() >= 0) {
                le6.putString("TeamColor", dfk5.getColor().getName());
            }
            le6.putBoolean("AllowFriendlyFire", dfk5.isAllowFriendlyFire());
            le6.putBoolean("SeeFriendlyInvisibles", dfk5.canSeeFriendlyInvisibles());
            le6.putString("MemberNamePrefix", Component.Serializer.toJson(dfk5.getPlayerPrefix()));
            le6.putString("MemberNameSuffix", Component.Serializer.toJson(dfk5.getPlayerSuffix()));
            le6.putString("NameTagVisibility", dfk5.getNameTagVisibility().name);
            le6.putString("DeathMessageVisibility", dfk5.getDeathMessageVisibility().name);
            le6.putString("CollisionRule", dfk5.getCollisionRule().name);
            final ListTag lk3 = new ListTag();
            for (final String string9 : dfk5.getPlayers()) {
                ((AbstractList<StringTag>)lk3).add(StringTag.valueOf(string9));
            }
            le6.put("Players", lk3);
            ((AbstractList<CompoundTag>)lk2).add(le6);
        }
        return lk2;
    }
    
    protected void saveDisplaySlots(final CompoundTag le) {
        final CompoundTag le2 = new CompoundTag();
        boolean boolean4 = false;
        for (int integer5 = 0; integer5 < 19; ++integer5) {
            final Objective dfj6 = this.scoreboard.getDisplayObjective(integer5);
            if (dfj6 != null) {
                le2.putString("slot_" + integer5, dfj6.getName());
                boolean4 = true;
            }
        }
        if (boolean4) {
            le.put("DisplaySlots", le2);
        }
    }
    
    protected ListTag saveObjectives() {
        final ListTag lk2 = new ListTag();
        final Collection<Objective> collection3 = this.scoreboard.getObjectives();
        for (final Objective dfj5 : collection3) {
            if (dfj5.getCriteria() == null) {
                continue;
            }
            final CompoundTag le6 = new CompoundTag();
            le6.putString("Name", dfj5.getName());
            le6.putString("CriteriaName", dfj5.getCriteria().getName());
            le6.putString("DisplayName", Component.Serializer.toJson(dfj5.getDisplayName()));
            le6.putString("RenderType", dfj5.getRenderType().getId());
            ((AbstractList<CompoundTag>)lk2).add(le6);
        }
        return lk2;
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
