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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import java.util.Iterator;
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.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

public class InventorySection
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 3;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 3;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public Map<Integer, ItemWithAllMetadata> items;
    public short capacity;
    
    public InventorySection() {
    }
    
    public InventorySection(@Nullable final Map<Integer, ItemWithAllMetadata> items, final short capacity) {
        this.items = items;
        this.capacity = capacity;
    }
    
    public InventorySection(@Nonnull final InventorySection other) {
        this.items = other.items;
        this.capacity = other.capacity;
    }
    
    @Nonnull
    public static InventorySection deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final InventorySection obj = new InventorySection();
        final byte nullBits = buf.getByte(offset);
        obj.capacity = buf.getShortLE(offset + 1);
        int pos = offset + 3;
        if ((nullBits & 0x1) != 0x0) {
            final int itemsCount = VarInt.peek(buf, pos);
            if (itemsCount < 0) {
                throw ProtocolException.negativeLength("Items", itemsCount);
            }
            if (itemsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Items", itemsCount, 4096000);
            }
            pos += VarInt.size(itemsCount);
            obj.items = new HashMap<Integer, ItemWithAllMetadata>(itemsCount);
            for (int i = 0; i < itemsCount; ++i) {
                final int key = buf.getIntLE(pos);
                pos += 4;
                final ItemWithAllMetadata val = ItemWithAllMetadata.deserialize(buf, pos);
                pos += ItemWithAllMetadata.computeBytesConsumed(buf, pos);
                if (obj.items.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("items", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 3;
        if ((nullBits & 0x1) != 0x0) {
            final int dictLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos);
            for (int i = 0; i < dictLen; ++i) {
                pos += 4;
                pos += ItemWithAllMetadata.computeBytesConsumed(buf, pos);
            }
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.items != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeShortLE(this.capacity);
        if (this.items != null) {
            if (this.items.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Items", this.items.size(), 4096000);
            }
            VarInt.write(buf, this.items.size());
            for (final Map.Entry<Integer, ItemWithAllMetadata> e : this.items.entrySet()) {
                buf.writeIntLE(e.getKey());
                e.getValue().serialize(buf);
            }
        }
    }
    
    public int computeSize() {
        int size = 3;
        if (this.items != null) {
            int itemsSize = 0;
            for (final Map.Entry<Integer, ItemWithAllMetadata> kvp : this.items.entrySet()) {
                itemsSize += 4 + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.items.size()) + itemsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 3) {
            return ValidationResult.error("Buffer too small: expected at least 3 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 3;
        if ((nullBits & 0x1) != 0x0) {
            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) {
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += ItemWithAllMetadata.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public InventorySection clone() {
        final InventorySection copy = new InventorySection();
        if (this.items != null) {
            final Map<Integer, ItemWithAllMetadata> m = new HashMap<Integer, ItemWithAllMetadata>();
            for (final Map.Entry<Integer, ItemWithAllMetadata> e : this.items.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.items = m;
        }
        copy.capacity = this.capacity;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final InventorySection other) {
            return Objects.equals(this.items, other.items) && this.capacity == other.capacity;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.items, this.capacity);
    }
}
