package net.minecraft.scoreboard;

import java.util.Collection;
import java.util.Iterator;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.WorldSavedData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ScoreboardSaveData extends WorldSavedData
{
    private static final Logger logger = LogManager.getLogger();
    private Scoreboard theScoreboard;
    private NBTTagCompound field_96506_b;
    private static final String __OBFID = "CL_00000620";

    public ScoreboardSaveData()
    {
        this("scoreboard");
    }

    public ScoreboardSaveData(String p_i2310_1_)
    {
        super(p_i2310_1_);
    }

    public void func_96499_a(Scoreboard p_96499_1_)
    {
        this.theScoreboard = p_96499_1_;

        if (this.field_96506_b != null)
        {
            this.readFromNBT(this.field_96506_b);
        }
    }

    /**
     * reads in data from the NBTTagCompound into this MapDataBase
     */
    public void readFromNBT(NBTTagCompound nbt)
    {
        if (this.theScoreboard == null)
        {
            this.field_96506_b = nbt;
        }
        else
        {
            this.func_96501_b(nbt.getTagList("Objectives", 10));
            this.func_96500_c(nbt.getTagList("PlayerScores", 10));

            if (nbt.hasKey("DisplaySlots", 10))
            {
                this.func_96504_c(nbt.getCompoundTag("DisplaySlots"));
            }

            if (nbt.hasKey("Teams", 9))
            {
                this.func_96498_a(nbt.getTagList("Teams", 10));
            }
        }
    }

    protected void func_96498_a(NBTTagList p_96498_1_)
    {
        for (int i = 0; i < p_96498_1_.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = p_96498_1_.getCompoundTagAt(i);
            ScorePlayerTeam scoreplayerteam = this.theScoreboard.createTeam(nbttagcompound.getString("Name"));
            scoreplayerteam.setTeamName(nbttagcompound.getString("DisplayName"));

            if (nbttagcompound.hasKey("TeamColor", 8))
            {
                scoreplayerteam.func_178774_a(EnumChatFormatting.getValueByName(nbttagcompound.getString("TeamColor")));
            }

            scoreplayerteam.setNamePrefix(nbttagcompound.getString("Prefix"));
            scoreplayerteam.setNameSuffix(nbttagcompound.getString("Suffix"));

            if (nbttagcompound.hasKey("AllowFriendlyFire", 99))
            {
                scoreplayerteam.setAllowFriendlyFire(nbttagcompound.getBoolean("AllowFriendlyFire"));
            }

            if (nbttagcompound.hasKey("SeeFriendlyInvisibles", 99))
            {
                scoreplayerteam.setSeeFriendlyInvisiblesEnabled(nbttagcompound.getBoolean("SeeFriendlyInvisibles"));
            }

            Team.EnumVisible enumvisible;

            if (nbttagcompound.hasKey("NameTagVisibility", 8))
            {
                enumvisible = Team.EnumVisible.func_178824_a(nbttagcompound.getString("NameTagVisibility"));

                if (enumvisible != null)
                {
                    scoreplayerteam.func_178772_a(enumvisible);
                }
            }

            if (nbttagcompound.hasKey("DeathMessageVisibility", 8))
            {
                enumvisible = Team.EnumVisible.func_178824_a(nbttagcompound.getString("DeathMessageVisibility"));

                if (enumvisible != null)
                {
                    scoreplayerteam.func_178773_b(enumvisible);
                }
            }

            this.func_96502_a(scoreplayerteam, nbttagcompound.getTagList("Players", 8));
        }
    }

    protected void func_96502_a(ScorePlayerTeam p_96502_1_, NBTTagList p_96502_2_)
    {
        for (int i = 0; i < p_96502_2_.tagCount(); ++i)
        {
            this.theScoreboard.func_151392_a(p_96502_2_.getStringTagAt(i), p_96502_1_.getRegisteredName());
        }
    }

    protected void func_96504_c(NBTTagCompound p_96504_1_)
    {
        for (int i = 0; i < 19; ++i)
        {
            if (p_96504_1_.hasKey("slot_" + i, 8))
            {
                String s = p_96504_1_.getString("slot_" + i);
                ScoreObjective scoreobjective = this.theScoreboard.getObjective(s);
                this.theScoreboard.setObjectiveInDisplaySlot(i, scoreobjective);
            }
        }
    }

    protected void func_96501_b(NBTTagList p_96501_1_)
    {
        for (int i = 0; i < p_96501_1_.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = p_96501_1_.getCompoundTagAt(i);
            IScoreObjectiveCriteria iscoreobjectivecriteria = (IScoreObjectiveCriteria)IScoreObjectiveCriteria.INSTANCES.get(nbttagcompound.getString("CriteriaName"));

            if (iscoreobjectivecriteria != null)
            {
                ScoreObjective scoreobjective = this.theScoreboard.addScoreObjective(nbttagcompound.getString("Name"), iscoreobjectivecriteria);
                scoreobjective.setDisplayName(nbttagcompound.getString("DisplayName"));
                scoreobjective.func_178767_a(IScoreObjectiveCriteria.EnumRenderType.func_178795_a(nbttagcompound.getString("RenderType")));
            }
        }
    }

    protected void func_96500_c(NBTTagList p_96500_1_)
    {
        for (int i = 0; i < p_96500_1_.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = p_96500_1_.getCompoundTagAt(i);
            ScoreObjective scoreobjective = this.theScoreboard.getObjective(nbttagcompound.getString("Objective"));
            Score score = this.theScoreboard.getValueFromObjective(nbttagcompound.getString("Name"), scoreobjective);
            score.setScorePoints(nbttagcompound.getInteger("Score"));

            if (nbttagcompound.hasKey("Locked"))
            {
                score.func_178815_a(nbttagcompound.getBoolean("Locked"));
            }
        }
    }

    /**
     * write data to NBTTagCompound from this MapDataBase, similar to Entities and TileEntities
     */
    public void writeToNBT(NBTTagCompound nbt)
    {
        if (this.theScoreboard == null)
        {
            logger.warn("Tried to save scoreboard without having a scoreboard...");
        }
        else
        {
            nbt.setTag("Objectives", this.func_96505_b());
            nbt.setTag("PlayerScores", this.func_96503_e());
            nbt.setTag("Teams", this.func_96496_a());
            this.func_96497_d(nbt);
        }
    }

    protected NBTTagList func_96496_a()
    {
        NBTTagList nbttaglist = new NBTTagList();
        Collection collection = this.theScoreboard.getTeams();
        Iterator iterator = collection.iterator();

        while (iterator.hasNext())
        {
            ScorePlayerTeam scoreplayerteam = (ScorePlayerTeam)iterator.next();
            NBTTagCompound nbttagcompound = new NBTTagCompound();
            nbttagcompound.setString("Name", scoreplayerteam.getRegisteredName());
            nbttagcompound.setString("DisplayName", scoreplayerteam.func_96669_c());

            if (scoreplayerteam.func_178775_l().func_175746_b() >= 0)
            {
                nbttagcompound.setString("TeamColor", scoreplayerteam.func_178775_l().getFriendlyName());
            }

            nbttagcompound.setString("Prefix", scoreplayerteam.getColorPrefix());
            nbttagcompound.setString("Suffix", scoreplayerteam.getColorSuffix());
            nbttagcompound.setBoolean("AllowFriendlyFire", scoreplayerteam.getAllowFriendlyFire());
            nbttagcompound.setBoolean("SeeFriendlyInvisibles", scoreplayerteam.func_98297_h());
            nbttagcompound.setString("NameTagVisibility", scoreplayerteam.func_178770_i().field_178830_e);
            nbttagcompound.setString("DeathMessageVisibility", scoreplayerteam.func_178771_j().field_178830_e);
            NBTTagList nbttaglist1 = new NBTTagList();
            Iterator iterator1 = scoreplayerteam.getMembershipCollection().iterator();

            while (iterator1.hasNext())
            {
                String s = (String)iterator1.next();
                nbttaglist1.appendTag(new NBTTagString(s));
            }

            nbttagcompound.setTag("Players", nbttaglist1);
            nbttaglist.appendTag(nbttagcompound);
        }

        return nbttaglist;
    }

    protected void func_96497_d(NBTTagCompound p_96497_1_)
    {
        NBTTagCompound nbttagcompound1 = new NBTTagCompound();
        boolean flag = false;

        for (int i = 0; i < 19; ++i)
        {
            ScoreObjective scoreobjective = this.theScoreboard.getObjectiveInDisplaySlot(i);

            if (scoreobjective != null)
            {
                nbttagcompound1.setString("slot_" + i, scoreobjective.getName());
                flag = true;
            }
        }

        if (flag)
        {
            p_96497_1_.setTag("DisplaySlots", nbttagcompound1);
        }
    }

    protected NBTTagList func_96505_b()
    {
        NBTTagList nbttaglist = new NBTTagList();
        Collection collection = this.theScoreboard.getScoreObjectives();
        Iterator iterator = collection.iterator();

        while (iterator.hasNext())
        {
            ScoreObjective scoreobjective = (ScoreObjective)iterator.next();

            if (scoreobjective.getCriteria() != null)
            {
                NBTTagCompound nbttagcompound = new NBTTagCompound();
                nbttagcompound.setString("Name", scoreobjective.getName());
                nbttagcompound.setString("CriteriaName", scoreobjective.getCriteria().getName());
                nbttagcompound.setString("DisplayName", scoreobjective.getDisplayName());
                nbttagcompound.setString("RenderType", scoreobjective.func_178766_e().func_178796_a());
                nbttaglist.appendTag(nbttagcompound);
            }
        }

        return nbttaglist;
    }

    protected NBTTagList func_96503_e()
    {
        NBTTagList nbttaglist = new NBTTagList();
        Collection collection = this.theScoreboard.func_96528_e();
        Iterator iterator = collection.iterator();

        while (iterator.hasNext())
        {
            Score score = (Score)iterator.next();

            if (score.getObjective() != null)
            {
                NBTTagCompound nbttagcompound = new NBTTagCompound();
                nbttagcompound.setString("Name", score.getPlayerName());
                nbttagcompound.setString("Objective", score.getObjective().getName());
                nbttagcompound.setInteger("Score", score.getScorePoints());
                nbttagcompound.setBoolean("Locked", score.func_178816_g());
                nbttaglist.appendTag(nbttagcompound);
            }
        }

        return nbttaglist;
    }
}