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

import com.google.gson.JsonParseException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import net.minecraft.tags.SerializationTags;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import java.util.Iterator;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import java.util.function.Consumer;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;

public class TagEntry extends LootPoolSingletonContainer {
    private final Tag<Item> tag;
    private final boolean expand;
    
    private TagEntry(final Tag<Item> adf, final boolean boolean2, final int integer3, final int integer4, final LootItemCondition[] arr, final LootItemFunction[] arr) {
        super(integer3, integer4, arr, arr);
        this.tag = adf;
        this.expand = boolean2;
    }
    
    @Override
    public LootPoolEntryType getType() {
        return LootPoolEntries.TAG;
    }
    
    public void createItemStack(final Consumer<ItemStack> consumer, final LootContext dat) {
        this.tag.getValues().forEach(bke -> consumer.accept(new ItemStack(bke)));
    }
    
    private boolean expandTag(final LootContext dat, final Consumer<LootPoolEntry> consumer) {
        if (this.canRun(dat)) {
            for (final Item bke5 : this.tag.getValues()) {
                consumer.accept(new EntryBase() {
                    @Override
                    public void createItemStack(final Consumer<ItemStack> consumer, final LootContext dat) {
                        consumer.accept(new ItemStack(bke5));
                    }
                });
            }
            return true;
        }
        return false;
    }
    
    @Override
    public boolean expand(final LootContext dat, final Consumer<LootPoolEntry> consumer) {
        if (this.expand) {
            return this.expandTag(dat, consumer);
        }
        return super.expand(dat, consumer);
    }
    
    public static Builder<?> expandTag(final Tag<Item> adf) {
        return LootPoolSingletonContainer.simpleBuilder((integer2, integer3, arr, arr) -> new TagEntry(adf, true, integer2, integer3, arr, arr));
    }
    
    public static class Serializer extends LootPoolSingletonContainer.Serializer<TagEntry> {
        @Override
        public void serialize(final JsonObject jsonObject, final TagEntry dbt, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dbt, jsonSerializationContext);
            jsonObject.addProperty("name", SerializationTags.getInstance().getItems().getIdOrThrow(dbt.tag).toString());
            jsonObject.addProperty("expand", dbt.expand);
        }
        
        @Override
        protected TagEntry deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final int integer3, final int integer4, final LootItemCondition[] arr, final LootItemFunction[] arr) {
            final ResourceLocation uh8 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "name"));
            final Tag<Item> adf9 = SerializationTags.getInstance().getItems().getTag(uh8);
            if (adf9 == null) {
                throw new JsonParseException("Can't find tag: " + uh8);
            }
            final boolean boolean10 = GsonHelper.getAsBoolean(jsonObject, "expand");
            return new TagEntry(adf9, boolean10, integer3, integer4, arr, arr, null);
        }
    }
}
