package net.minecraft.stats;

import java.util.AbstractList;
import org.apache.logging.log4j.LogManager;
import java.util.Optional;
import net.minecraft.ResourceLocationException;
import java.util.function.Consumer;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import java.util.Collections;
import java.util.Iterator;
import net.minecraft.resources.ResourceLocation;
import java.util.List;
import net.minecraft.network.protocol.game.ClientboundRecipePacket;
import net.minecraft.advancements.CriteriaTriggers;
import com.google.common.collect.Lists;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.crafting.Recipe;
import java.util.Collection;
import org.apache.logging.log4j.Logger;

public class ServerRecipeBook extends RecipeBook {
    private static final Logger LOGGER;
    
    public int addRecipes(final Collection<Recipe<?>> collection, final ServerPlayer ze) {
        final List<ResourceLocation> list4 = Lists.newArrayList();
        int integer5 = 0;
        for (final Recipe<?> bmu7 : collection) {
            final ResourceLocation uh8 = bmu7.getId();
            if (!this.known.contains(uh8) && !bmu7.isSpecial()) {
                this.add(uh8);
                this.addHighlight(uh8);
                list4.add(uh8);
                CriteriaTriggers.RECIPE_UNLOCKED.trigger(ze, bmu7);
                ++integer5;
            }
        }
        this.sendRecipes(ClientboundRecipePacket.State.ADD, ze, list4);
        return integer5;
    }
    
    public int removeRecipes(final Collection<Recipe<?>> collection, final ServerPlayer ze) {
        final List<ResourceLocation> list4 = Lists.newArrayList();
        int integer5 = 0;
        for (final Recipe<?> bmu7 : collection) {
            final ResourceLocation uh8 = bmu7.getId();
            if (this.known.contains(uh8)) {
                this.remove(uh8);
                list4.add(uh8);
                ++integer5;
            }
        }
        this.sendRecipes(ClientboundRecipePacket.State.REMOVE, ze, list4);
        return integer5;
    }
    
    private void sendRecipes(final ClientboundRecipePacket.State a, final ServerPlayer ze, final List<ResourceLocation> list) {
        ze.connection.send(new ClientboundRecipePacket(a, list, Collections.emptyList(), this.guiOpen, this.filteringCraftable, this.furnaceGuiOpen, this.furnaceFilteringCraftable));
    }
    
    public CompoundTag toNbt() {
        final CompoundTag le2 = new CompoundTag();
        le2.putBoolean("isGuiOpen", this.guiOpen);
        le2.putBoolean("isFilteringCraftable", this.filteringCraftable);
        le2.putBoolean("isFurnaceGuiOpen", this.furnaceGuiOpen);
        le2.putBoolean("isFurnaceFilteringCraftable", this.furnaceFilteringCraftable);
        le2.putBoolean("isBlastingFurnaceGuiOpen", this.blastingFurnaceGuiOpen);
        le2.putBoolean("isBlastingFurnaceFilteringCraftable", this.blastingFurnaceFilteringCraftable);
        le2.putBoolean("isSmokerGuiOpen", this.smokerGuiOpen);
        le2.putBoolean("isSmokerFilteringCraftable", this.smokerFilteringCraftable);
        final ListTag lk3 = new ListTag();
        for (final ResourceLocation uh5 : this.known) {
            ((AbstractList<StringTag>)lk3).add(StringTag.valueOf(uh5.toString()));
        }
        le2.put("recipes", lk3);
        final ListTag lk4 = new ListTag();
        for (final ResourceLocation uh6 : this.highlight) {
            ((AbstractList<StringTag>)lk4).add(StringTag.valueOf(uh6.toString()));
        }
        le2.put("toBeDisplayed", lk4);
        return le2;
    }
    
    public void fromNbt(final CompoundTag le, final RecipeManager bmv) {
        this.guiOpen = le.getBoolean("isGuiOpen");
        this.filteringCraftable = le.getBoolean("isFilteringCraftable");
        this.furnaceGuiOpen = le.getBoolean("isFurnaceGuiOpen");
        this.furnaceFilteringCraftable = le.getBoolean("isFurnaceFilteringCraftable");
        this.blastingFurnaceGuiOpen = le.getBoolean("isBlastingFurnaceGuiOpen");
        this.blastingFurnaceFilteringCraftable = le.getBoolean("isBlastingFurnaceFilteringCraftable");
        this.smokerGuiOpen = le.getBoolean("isSmokerGuiOpen");
        this.smokerFilteringCraftable = le.getBoolean("isSmokerFilteringCraftable");
        final ListTag lk4 = le.getList("recipes", 8);
        this.loadRecipes(lk4, this::add, bmv);
        final ListTag lk5 = le.getList("toBeDisplayed", 8);
        this.loadRecipes(lk5, this::addHighlight, bmv);
    }
    
    private void loadRecipes(final ListTag lk, final Consumer<Recipe<?>> consumer, final RecipeManager bmv) {
        for (int integer5 = 0; integer5 < lk.size(); ++integer5) {
            final String string6 = lk.getString(integer5);
            try {
                final ResourceLocation uh7 = new ResourceLocation(string6);
                final Optional<? extends Recipe<?>> optional8 = bmv.byKey(uh7);
                if (!optional8.isPresent()) {
                    ServerRecipeBook.LOGGER.error("Tried to load unrecognized recipe: {} removed now.", uh7);
                }
                else {
                    consumer.accept(optional8.get());
                }
            }
            catch (ResourceLocationException t7) {
                ServerRecipeBook.LOGGER.error("Tried to load improperly formatted recipe: {} removed now.", string6);
            }
        }
    }
    
    public void sendInitialRecipeBook(final ServerPlayer ze) {
        ze.connection.send(new ClientboundRecipePacket(ClientboundRecipePacket.State.INIT, this.known, this.highlight, this.guiOpen, this.filteringCraftable, this.furnaceGuiOpen, this.furnaceFilteringCraftable));
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
