// 
// 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.ItemBase;
import java.util.Map;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.UpdateType;
import com.hypixel.hytale.protocol.Packet;

public class UpdateItems implements Packet
{
    public static final int PACKET_ID = 54;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 4;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 12;
    public static final int MAX_SIZE = 1677721600;
    @Nonnull
    public UpdateType type;
    @Nullable
    public Map<String, ItemBase> items;
    @Nullable
    public String[] removedItems;
    public boolean updateModels;
    public boolean updateIcons;
    
    @Override
    public int getId() {
        return 54;
    }
    
    public UpdateItems() {
        this.type = UpdateType.Init;
    }
    
    public UpdateItems(@Nonnull final UpdateType type, @Nullable final Map<String, ItemBase> items, @Nullable final String[] removedItems, final boolean updateModels, final boolean updateIcons) {
        this.type = UpdateType.Init;
        this.type = type;
        this.items = items;
        this.removedItems = removedItems;
        this.updateModels = updateModels;
        this.updateIcons = updateIcons;
    }
    
    public UpdateItems(@Nonnull final UpdateItems other) {
        this.type = UpdateType.Init;
        this.type = other.type;
        this.items = other.items;
        this.removedItems = other.removedItems;
        this.updateModels = other.updateModels;
        this.updateIcons = other.updateIcons;
    }
    
    @Nonnull
    public static UpdateItems deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateItems obj = new UpdateItems();
        final byte nullBits = buf.getByte(offset);
        obj.type = UpdateType.fromValue(buf.getByte(offset + 1));
        obj.updateModels = (buf.getByte(offset + 2) != 0);
        obj.updateIcons = (buf.getByte(offset + 3) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 12 + buf.getIntLE(offset + 4);
            final int itemsCount = VarInt.peek(buf, varPos0);
            if (itemsCount < 0) {
                throw ProtocolException.negativeLength("Items", itemsCount);
            }
            if (itemsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Items", itemsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            obj.items = new HashMap<String, ItemBase>(itemsCount);
            int dictPos = varPos0 + varIntLen;
            for (int i = 0; i < itemsCount; ++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 ItemBase val = ItemBase.deserialize(buf, dictPos);
                dictPos += ItemBase.computeBytesConsumed(buf, dictPos);
                if (obj.items.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("items", key);
                }
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 12 + buf.getIntLE(offset + 8);
            final int removedItemsCount = VarInt.peek(buf, varPos2);
            if (removedItemsCount < 0) {
                throw ProtocolException.negativeLength("RemovedItems", removedItemsCount);
            }
            if (removedItemsCount > 4096000) {
                throw ProtocolException.arrayTooLong("RemovedItems", removedItemsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + removedItemsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("RemovedItems", varPos2 + varIntLen + removedItemsCount * 1, buf.readableBytes());
            }
            obj.removedItems = new String[removedItemsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < removedItemsCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("removedItems[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("removedItems[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.removedItems[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 = 12;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 4);
            int pos0 = offset + 12 + 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 += ItemBase.computeBytesConsumed(buf, pos0);
            }
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 8);
            int pos2 = offset + 12 + 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.items != null) {
            nullBits |= 0x1;
        }
        if (this.removedItems != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        buf.writeByte(this.updateModels ? 1 : 0);
        buf.writeByte(this.updateIcons ? 1 : 0);
        final int itemsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int removedItemsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.items != null) {
            buf.setIntLE(itemsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.items.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Items", this.items.size(), 4096000);
            }
            VarInt.write(buf, this.items.size());
            for (final Map.Entry<String, ItemBase> e : this.items.entrySet()) {
                PacketIO.writeVarString(buf, e.getKey(), 4096000);
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(itemsOffsetSlot, -1);
        }
        if (this.removedItems != null) {
            buf.setIntLE(removedItemsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.removedItems.length > 4096000) {
                throw ProtocolException.arrayTooLong("RemovedItems", this.removedItems.length, 4096000);
            }
            VarInt.write(buf, this.removedItems.length);
            for (final String item : this.removedItems) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(removedItemsOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 12;
        if (this.items != null) {
            int itemsSize = 0;
            for (final Map.Entry<String, ItemBase> kvp : this.items.entrySet()) {
                itemsSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.items.size()) + itemsSize;
        }
        if (this.removedItems != null) {
            int removedItemsSize = 0;
            for (final String elem : this.removedItems) {
                removedItemsSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.removedItems.length) + removedItemsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 12) {
            return ValidationResult.error("Buffer too small: expected at least 12 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int itemsOffset = buffer.getIntLE(offset + 4);
            if (itemsOffset < 0) {
                return ValidationResult.error("Invalid offset for Items");
            }
            int pos = offset + 12 + itemsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Items");
            }
            final int itemsCount = VarInt.peek(buffer, pos);
            if (itemsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Items");
            }
            if (itemsCount > 4096000) {
                return ValidationResult.error("Items exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < itemsCount; ++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 += ItemBase.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int removedItemsOffset = buffer.getIntLE(offset + 8);
            if (removedItemsOffset < 0) {
                return ValidationResult.error("Invalid offset for RemovedItems");
            }
            int pos = offset + 12 + removedItemsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for RemovedItems");
            }
            final int removedItemsCount = VarInt.peek(buffer, pos);
            if (removedItemsCount < 0) {
                return ValidationResult.error("Invalid array count for RemovedItems");
            }
            if (removedItemsCount > 4096000) {
                return ValidationResult.error("RemovedItems exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < removedItemsCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in RemovedItems");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in RemovedItems");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public UpdateItems clone() {
        final UpdateItems copy = new UpdateItems();
        copy.type = this.type;
        if (this.items != null) {
            final Map<String, ItemBase> m = new HashMap<String, ItemBase>();
            for (final Map.Entry<String, ItemBase> e : this.items.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.items = m;
        }
        copy.removedItems = (String[])((this.removedItems != null) ? ((String[])Arrays.copyOf(this.removedItems, this.removedItems.length)) : null);
        copy.updateModels = this.updateModels;
        copy.updateIcons = this.updateIcons;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateItems other) {
            return Objects.equals(this.type, other.type) && Objects.equals(this.items, other.items) && Arrays.equals(this.removedItems, other.removedItems) && this.updateModels == other.updateModels && this.updateIcons == other.updateIcons;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.type);
        result = 31 * result + Objects.hashCode(this.items);
        result = 31 * result + Arrays.hashCode(this.removedItems);
        result = 31 * result + Boolean.hashCode(this.updateModels);
        result = 31 * result + Boolean.hashCode(this.updateIcons);
        return result;
    }
}
