package net.minecraft.network.chat;

import java.util.List;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.scores.Score;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.Scoreboard;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.StringUtil;
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;
    private String value;
    
    public ScoreComponent(final String string1, final String string2) {
        this.value = "";
        this.name = string1;
        this.objective = string2;
        EntitySelector ep4 = null;
        try {
            final EntitySelectorParser eq5 = new EntitySelectorParser(new StringReader(string1));
            ep4 = eq5.parse();
        }
        catch (CommandSyntaxException ex) {}
        this.selector = ep4;
    }
    
    public String getName() {
        return this.name;
    }
    
    public String getObjective() {
        return this.objective;
    }
    
    public void setValue(final String string) {
        this.value = string;
    }
    
    @Override
    public String getContents() {
        return this.value;
    }
    
    private void resolve(final CommandSourceStack cq) {
        final MinecraftServer minecraftServer3 = cq.getServer();
        if (minecraftServer3 != null && minecraftServer3.isInitialized() && StringUtil.isNullOrEmpty(this.value)) {
            final Scoreboard cwl4 = minecraftServer3.getScoreboard();
            final Objective cwi5 = cwl4.getObjective(this.objective);
            if (cwl4.hasPlayerScore(this.name, cwi5)) {
                final Score cwk6 = cwl4.getOrCreatePlayerScore(this.name, cwi5);
                this.setValue(String.format("%d", cwk6.getScore()));
            }
            else {
                this.value = "";
            }
        }
    }
    
    @Override
    public ScoreComponent copy() {
        final ScoreComponent ll2 = new ScoreComponent(this.name, this.objective);
        ll2.setValue(this.value);
        return ll2;
    }
    
    @Override
    public Component resolve(@Nullable final CommandSourceStack cq, @Nullable final Entity akq, final int integer) throws CommandSyntaxException {
        if (cq == null) {
            return this.copy();
        }
        String string5;
        if (this.selector != null) {
            final List<? extends Entity> list6 = this.selector.findEntities(cq);
            if (list6.isEmpty()) {
                string5 = this.name;
            }
            else {
                if (list6.size() != 1) {
                    throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.create();
                }
                string5 = ((Entity)list6.get(0)).getScoreboardName();
            }
        }
        else {
            string5 = this.name;
        }
        final String string6 = (akq != null && string5.equals("*")) ? akq.getScoreboardName() : string5;
        final ScoreComponent ll7 = new ScoreComponent(string6, this.objective);
        ll7.setValue(this.value);
        ll7.resolve(cq);
        return ll7;
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof ScoreComponent) {
            final ScoreComponent ll3 = (ScoreComponent)object;
            return this.name.equals(ll3.name) && this.objective.equals(ll3.objective) && super.equals(object);
        }
        return false;
    }
    
    @Override
    public String toString() {
        return "ScoreComponent{name='" + this.name + '\'' + "objective='" + this.objective + '\'' + ", siblings=" + this.siblings + ", style=" + this.getStyle() + '}';
    }
}
