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.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 LootItemFunctionType getType() {
        return LootItemFunctions.SET_LORE;
    }
    
    @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 bki, final LootContext dat) {
        final ListTag lk4 = this.getLoreTag(bki, !this.lore.isEmpty());
        if (lk4 != null) {
            if (this.replace) {
                lk4.clear();
            }
            final UnaryOperator<Component> unaryOperator5 = SetNameFunction.createResolver(dat, this.resolutionContext);
            this.lore.stream().map(unaryOperator5).map(Component.Serializer::toJson).map(StringTag::valueOf).forEach(lk4::add);
        }
        return bki;
    }
    
    @Nullable
    private ListTag getLoreTag(final ItemStack bki, final boolean boolean2) {
        CompoundTag le4;
        if (bki.hasTag()) {
            le4 = bki.getTag();
        }
        else {
            if (!boolean2) {
                return null;
            }
            le4 = new CompoundTag();
            bki.setTag(le4);
        }
        CompoundTag le5;
        if (le4.contains("display", 10)) {
            le5 = le4.getCompound("display");
        }
        else {
            if (!boolean2) {
                return null;
            }
            le5 = new CompoundTag();
            le4.put("display", le5);
        }
        if (le5.contains("Lore", 9)) {
            return le5.getList("Lore", 8);
        }
        if (boolean2) {
            final ListTag lk6 = new ListTag();
            le5.put("Lore", lk6);
            return lk6;
        }
        return null;
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetLoreFunction> {
        @Override
        public void serialize(final JsonObject jsonObject, final SetLoreFunction dcq, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dcq, jsonSerializationContext);
            jsonObject.addProperty("replace", dcq.replace);
            final JsonArray jsonArray5 = new JsonArray();
            for (final Component mr7 : dcq.lore) {
                jsonArray5.add(Component.Serializer.toJsonTree(mr7));
            }
            jsonObject.add("lore", jsonArray5);
            if (dcq.resolutionContext != null) {
                jsonObject.add("entity", jsonSerializationContext.serialize(dcq.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);
        }
    }
}
