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

import java.util.stream.Collector;
import com.google.common.collect.Streams;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import java.util.Iterator;
import com.google.gson.JsonElement;
import com.google.gson.JsonArray;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.CompoundTag;
import java.util.function.UnaryOperator;
import net.minecraft.nbt.ListTag;
import java.util.function.Consumer;
import net.minecraft.nbt.StringTag;
import java.util.function.Function;
import net.minecraft.world.item.ItemStack;
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 java.util.Collection;
import com.google.common.collect.ImmutableList;
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 java.util.List;

public class SetLoreFunction extends LootItemConditionalFunction {
    private final boolean replace;
    private final List<Component> lore;
    @Nullable
    private final LootContext.EntityTarget resolutionContext;
    
    public SetLoreFunction(final LootItemCondition[] arr, final boolean boolean2, final List<Component> list, @Nullable final LootContext.EntityTarget c) {
        super(arr);
        this.replace = boolean2;
        this.lore = ImmutableList.copyOf(list);
        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 ItemStack run(final ItemStack ben, final LootContext crx) {
        final ListTag jz4 = this.getLoreTag(ben, !this.lore.isEmpty());
        if (jz4 != null) {
            if (this.replace) {
                jz4.clear();
            }
            final UnaryOperator<Component> unaryOperator5 = SetNameFunction.createResolver(crx, this.resolutionContext);
            this.lore.stream().map(unaryOperator5).map(Component.Serializer::toJson).map(StringTag::valueOf).forEach(jz4::add);
        }
        return ben;
    }
    
    @Nullable
    private ListTag getLoreTag(final ItemStack ben, final boolean boolean2) {
        CompoundTag jt4;
        if (ben.hasTag()) {
            jt4 = ben.getTag();
        }
        else {
            if (!boolean2) {
                return null;
            }
            jt4 = new CompoundTag();
            ben.setTag(jt4);
        }
        CompoundTag jt5;
        if (jt4.contains("display", 10)) {
            jt5 = jt4.getCompound("display");
        }
        else {
            if (!boolean2) {
                return null;
            }
            jt5 = new CompoundTag();
            jt4.put("display", jt5);
        }
        if (jt5.contains("Lore", 9)) {
            return jt5.getList("Lore", 8);
        }
        if (boolean2) {
            final ListTag jz6 = new ListTag();
            jt5.put("Lore", jz6);
            return jz6;
        }
        return null;
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetLoreFunction> {
        public Serializer() {
            super(new ResourceLocation("set_lore"), SetLoreFunction.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final SetLoreFunction ctq, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, ctq, jsonSerializationContext);
            jsonObject.addProperty("replace", ctq.replace);
            final JsonArray jsonArray5 = new JsonArray();
            for (final Component lf7 : ctq.lore) {
                jsonArray5.add(Component.Serializer.toJsonTree(lf7));
            }
            jsonObject.add("lore", jsonArray5);
            if (ctq.resolutionContext != null) {
                jsonObject.add("entity", jsonSerializationContext.serialize(ctq.resolutionContext));
            }
        }
        
        @Override
        public SetLoreFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final boolean boolean5 = GsonHelper.getAsBoolean(jsonObject, "replace", false);
            final List<Component> list6 = Streams.stream((Iterable<Object>)GsonHelper.getAsJsonArray(jsonObject, "lore")).map(Component.Serializer::fromJson).collect(ImmutableList.toImmutableList());
            final LootContext.EntityTarget c7 = GsonHelper.<LootContext.EntityTarget>getAsObject(jsonObject, "entity", (LootContext.EntityTarget)null, jsonDeserializationContext, LootContext.EntityTarget.class);
            return new SetLoreFunction(arr, boolean5, list6, c7);
        }
    }
}
