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 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 mr, @Nullable final LootContext.EntityTarget c) {
        super(arr);
        this.name = mr;
        this.resolutionContext = c;
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.SET_NAME;
    }
    
    @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 dat, @Nullable final LootContext.EntityTarget c) {
        if (c != null) {
            final Entity aom3 = dat.<Entity>getParamOrNull(c.getParam());
            if (aom3 != null) {
                final CommandSourceStack cz4 = aom3.createCommandSourceStack().withPermission(2);
                final CommandSourceStack cz5;
                final Entity aom4;
                return (UnaryOperator<Component>)(mr -> {
                    try {
                        return ComponentUtils.updateForEntity(cz5, mr, aom4, 0);
                    }
                    catch (CommandSyntaxException commandSyntaxException4) {
                        SetNameFunction.LOGGER.warn("Failed to resolve text component", (Throwable)commandSyntaxException4);
                        return mr;
                    }
                });
            }
        }
        return (UnaryOperator<Component>)(mr -> mr);
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        if (this.name != null) {
            bki.setHoverName(createResolver(dat, this.resolutionContext).apply(this.name));
        }
        return bki;
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetNameFunction> {
        @Override
        public void serialize(final JsonObject jsonObject, final SetNameFunction dcr, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dcr, jsonSerializationContext);
            if (dcr.name != null) {
                jsonObject.add("name", Component.Serializer.toJsonTree(dcr.name));
            }
            if (dcr.resolutionContext != null) {
                jsonObject.add("entity", jsonSerializationContext.serialize(dcr.resolutionContext));
            }
        }
        
        @Override
        public SetNameFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final Component mr5 = 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, mr5, c6, null);
        }
    }
}
