package net.minecraft.world.level.storage.loot.functions;

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import net.minecraft.world.item.ItemStack;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.network.chat.ComponentUtils;
import java.util.function.UnaryOperator;
import net.minecraft.world.entity.Entity;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import java.util.Set;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import javax.annotation.Nullable;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.network.chat.Component;
import org.apache.logging.log4j.Logger;

public class SetNameFunction extends LootItemConditionalFunction {
    private static final Logger LOGGER;
    private final Component name;
    @Nullable
    private final LootContext.EntityTarget resolutionContext;
    
    private SetNameFunction(final LootItemCondition[] arr, @Nullable final Component lf, @Nullable final LootContext.EntityTarget c) {
        super(arr);
        this.name = lf;
        this.resolutionContext = c;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return (Set<LootContextParam<?>>)((this.resolutionContext != null) ? ImmutableSet.<LootContextParam<? extends Entity>>of(this.resolutionContext.getParam()) : ImmutableSet.<LootContextParam<? extends Entity>>of());
    }
    
    public static UnaryOperator<Component> createResolver(final LootContext crx, @Nullable final LootContext.EntityTarget c) {
        if (c != null) {
            final Entity akq3 = crx.<Entity>getParamOrNull(c.getParam());
            if (akq3 != null) {
                final CommandSourceStack cq4 = akq3.createCommandSourceStack().withPermission(2);
                final CommandSourceStack cq5;
                final Entity akq4;
                return (UnaryOperator<Component>)(lf -> {
                    try {
                        return ComponentUtils.updateForEntity(cq5, lf, akq4, 0);
                    }
                    catch (CommandSyntaxException commandSyntaxException4) {
                        SetNameFunction.LOGGER.warn("Failed to resolve text component", (Throwable)commandSyntaxException4);
                        return lf;
                    }
                });
            }
        }
        return (UnaryOperator<Component>)(lf -> lf);
    }
    
    public ItemStack run(final ItemStack ben, final LootContext crx) {
        if (this.name != null) {
            ben.setHoverName(createResolver(crx, this.resolutionContext).apply(this.name));
        }
        return ben;
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetNameFunction> {
        public Serializer() {
            super(new ResourceLocation("set_name"), SetNameFunction.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final SetNameFunction ctr, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, ctr, jsonSerializationContext);
            if (ctr.name != null) {
                jsonObject.add("name", Component.Serializer.toJsonTree(ctr.name));
            }
            if (ctr.resolutionContext != null) {
                jsonObject.add("entity", jsonSerializationContext.serialize(ctr.resolutionContext));
            }
        }
        
        @Override
        public SetNameFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final Component lf5 = Component.Serializer.fromJson(jsonObject.get("name"));
            final LootContext.EntityTarget c6 = GsonHelper.<LootContext.EntityTarget>getAsObject(jsonObject, "entity", (LootContext.EntityTarget)null, jsonDeserializationContext, LootContext.EntityTarget.class);
            return new SetNameFunction(arr, lf5, c6, null);
        }
    }
}
