package net.minecraft.network.chat;

import net.minecraft.world.scores.Score;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.Scoreboard;
import net.minecraft.server.MinecraftServer;
import java.util.List;
import net.minecraft.world.entity.Entity;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.arguments.selector.EntitySelectorParser;
import com.mojang.brigadier.StringReader;
import javax.annotation.Nullable;
import net.minecraft.commands.arguments.selector.EntitySelector;

public class ScoreComponent extends BaseComponent implements ContextAwareComponent {
    private final String name;
    @Nullable
    private final EntitySelector selector;
    private final String objective;
    
    @Nullable
    private static EntitySelector parseSelector(final String string) {
        try {
            return new EntitySelectorParser(new StringReader(string)).parse();
        }
        catch (CommandSyntaxException ex) {
            return null;
        }
    }
    
    public ScoreComponent(final String string1, final String string2) {
        this(string1, parseSelector(string1), string2);
    }
    
    private ScoreComponent(final String string1, @Nullable final EntitySelector ez, final String string3) {
        this.name = string1;
        this.selector = ez;
        this.objective = string3;
    }
    
    public String getName() {
        return this.name;
    }
    
    public String getObjective() {
        return this.objective;
    }
    
    private String findTargetName(final CommandSourceStack cz) throws CommandSyntaxException {
        if (this.selector != null) {
            final List<? extends Entity> list3 = this.selector.findEntities(cz);
            if (!list3.isEmpty()) {
                if (list3.size() != 1) {
                    throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.create();
                }
                return ((Entity)list3.get(0)).getScoreboardName();
            }
        }
        return this.name;
    }
    
    private String getScore(final String string, final CommandSourceStack cz) {
        final MinecraftServer minecraftServer4 = cz.getServer();
        if (minecraftServer4 != null) {
            final Scoreboard dfm5 = minecraftServer4.getScoreboard();
            final Objective dfj6 = dfm5.getObjective(this.objective);
            if (dfm5.hasPlayerScore(string, dfj6)) {
                final Score dfl7 = dfm5.getOrCreatePlayerScore(string, dfj6);
                return Integer.toString(dfl7.getScore());
            }
        }
        return "";
    }
    
    @Override
    public ScoreComponent plainCopy() {
        return new ScoreComponent(this.name, this.selector, this.objective);
    }
    
    @Override
    public MutableComponent resolve(@Nullable final CommandSourceStack cz, @Nullable final Entity aom, final int integer) throws CommandSyntaxException {
        if (cz == null) {
            return new TextComponent("");
        }
        final String string5 = this.findTargetName(cz);
        final String string6 = (aom != null && string5.equals("*")) ? aom.getScoreboardName() : string5;
        return new TextComponent(this.getScore(string6, cz));
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof ScoreComponent) {
            final ScoreComponent mz3 = (ScoreComponent)object;
            return this.name.equals(mz3.name) && this.objective.equals(mz3.objective) && super.equals(object);
        }
        return false;
    }
    
    @Override
    public String toString() {
        return "ScoreComponent{name='" + this.name + '\'' + "objective='" + this.objective + '\'' + ", siblings=" + this.siblings + ", style=" + this.getStyle() + '}';
    }
}
