// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.protocol.packets.assets;

import java.util.Objects;
import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import java.util.Iterator;
import com.hypixel.hytale.protocol.io.PacketIO;
import java.util.HashMap;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.CraftingRecipe;
import java.util.Map;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.UpdateType;
import com.hypixel.hytale.protocol.Packet;

public class UpdateRecipes implements Packet
{
    public static final int PACKET_ID = 60;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 10;
    public static final int MAX_SIZE = 1677721600;
    @Nonnull
    public UpdateType type;
    @Nullable
    public Map<String, CraftingRecipe> recipes;
    @Nullable
    public String[] removedRecipes;
    
    @Override
    public int getId() {
        return 60;
    }
    
    public UpdateRecipes() {
        this.type = UpdateType.Init;
    }
    
    public UpdateRecipes(@Nonnull final UpdateType type, @Nullable final Map<String, CraftingRecipe> recipes, @Nullable final String[] removedRecipes) {
        this.type = UpdateType.Init;
        this.type = type;
        this.recipes = recipes;
        this.removedRecipes = removedRecipes;
    }
    
    public UpdateRecipes(@Nonnull final UpdateRecipes other) {
        this.type = UpdateType.Init;
        this.type = other.type;
        this.recipes = other.recipes;
        this.removedRecipes = other.removedRecipes;
    }
    
    @Nonnull
    public static UpdateRecipes deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateRecipes obj = new UpdateRecipes();
        final byte nullBits = buf.getByte(offset);
        obj.type = UpdateType.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 10 + buf.getIntLE(offset + 2);
            final int recipesCount = VarInt.peek(buf, varPos0);
            if (recipesCount < 0) {
                throw ProtocolException.negativeLength("Recipes", recipesCount);
            }
            if (recipesCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Recipes", recipesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            obj.recipes = new HashMap<String, CraftingRecipe>(recipesCount);
            int dictPos = varPos0 + varIntLen;
            for (int i = 0; i < recipesCount; ++i) {
                final int keyLen = VarInt.peek(buf, dictPos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, dictPos);
                final String key = PacketIO.readVarString(buf, dictPos);
                dictPos += keyVarLen + keyLen;
                final CraftingRecipe val = CraftingRecipe.deserialize(buf, dictPos);
                dictPos += CraftingRecipe.computeBytesConsumed(buf, dictPos);
                if (obj.recipes.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("recipes", key);
                }
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 10 + buf.getIntLE(offset + 6);
            final int removedRecipesCount = VarInt.peek(buf, varPos2);
            if (removedRecipesCount < 0) {
                throw ProtocolException.negativeLength("RemovedRecipes", removedRecipesCount);
            }
            if (removedRecipesCount > 4096000) {
                throw ProtocolException.arrayTooLong("RemovedRecipes", removedRecipesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + removedRecipesCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("RemovedRecipes", varPos2 + varIntLen + removedRecipesCount * 1, buf.readableBytes());
            }
            obj.removedRecipes = new String[removedRecipesCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < removedRecipesCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("removedRecipes[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("removedRecipes[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.removedRecipes[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 10;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 2);
            int pos0 = offset + 10 + fieldOffset0;
            final int dictLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0);
            for (int i = 0; i < dictLen; ++i) {
                final int sl = VarInt.peek(buf, pos0);
                pos0 += VarInt.length(buf, pos0) + sl;
                pos0 += CraftingRecipe.computeBytesConsumed(buf, pos0);
            }
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 6);
            int pos2 = offset + 10 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                final int sl = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2) + sl;
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.recipes != null) {
            nullBits |= 0x1;
        }
        if (this.removedRecipes != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        final int recipesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int removedRecipesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.recipes != null) {
            buf.setIntLE(recipesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.recipes.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Recipes", this.recipes.size(), 4096000);
            }
            VarInt.write(buf, this.recipes.size());
            for (final Map.Entry<String, CraftingRecipe> e : this.recipes.entrySet()) {
                PacketIO.writeVarString(buf, e.getKey(), 4096000);
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(recipesOffsetSlot, -1);
        }
        if (this.removedRecipes != null) {
            buf.setIntLE(removedRecipesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.removedRecipes.length > 4096000) {
                throw ProtocolException.arrayTooLong("RemovedRecipes", this.removedRecipes.length, 4096000);
            }
            VarInt.write(buf, this.removedRecipes.length);
            for (final String item : this.removedRecipes) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(removedRecipesOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 10;
        if (this.recipes != null) {
            int recipesSize = 0;
            for (final Map.Entry<String, CraftingRecipe> kvp : this.recipes.entrySet()) {
                recipesSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.recipes.size()) + recipesSize;
        }
        if (this.removedRecipes != null) {
            int removedRecipesSize = 0;
            for (final String elem : this.removedRecipes) {
                removedRecipesSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.removedRecipes.length) + removedRecipesSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 10) {
            return ValidationResult.error("Buffer too small: expected at least 10 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int recipesOffset = buffer.getIntLE(offset + 2);
            if (recipesOffset < 0) {
                return ValidationResult.error("Invalid offset for Recipes");
            }
            int pos = offset + 10 + recipesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Recipes");
            }
            final int recipesCount = VarInt.peek(buffer, pos);
            if (recipesCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Recipes");
            }
            if (recipesCount > 4096000) {
                return ValidationResult.error("Recipes exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < recipesCount; ++i) {
                final int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += CraftingRecipe.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int removedRecipesOffset = buffer.getIntLE(offset + 6);
            if (removedRecipesOffset < 0) {
                return ValidationResult.error("Invalid offset for RemovedRecipes");
            }
            int pos = offset + 10 + removedRecipesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for RemovedRecipes");
            }
            final int removedRecipesCount = VarInt.peek(buffer, pos);
            if (removedRecipesCount < 0) {
                return ValidationResult.error("Invalid array count for RemovedRecipes");
            }
            if (removedRecipesCount > 4096000) {
                return ValidationResult.error("RemovedRecipes exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < removedRecipesCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in RemovedRecipes");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in RemovedRecipes");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public UpdateRecipes clone() {
        final UpdateRecipes copy = new UpdateRecipes();
        copy.type = this.type;
        if (this.recipes != null) {
            final Map<String, CraftingRecipe> m = new HashMap<String, CraftingRecipe>();
            for (final Map.Entry<String, CraftingRecipe> e : this.recipes.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.recipes = m;
        }
        copy.removedRecipes = (String[])((this.removedRecipes != null) ? ((String[])Arrays.copyOf(this.removedRecipes, this.removedRecipes.length)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateRecipes other) {
            return Objects.equals(this.type, other.type) && Objects.equals(this.recipes, other.recipes) && Arrays.equals(this.removedRecipes, other.removedRecipes);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.type);
        result = 31 * result + Objects.hashCode(this.recipes);
        result = 31 * result + Arrays.hashCode(this.removedRecipes);
        return result;
    }
}
