// 
// 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.PacketIO;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import java.util.Map;
import javax.annotation.Nullable;

public class ItemSoundSet
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 36864019;
    @Nullable
    public String id;
    @Nullable
    public Map<ItemSoundEvent, Integer> soundEventIndices;
    
    public ItemSoundSet() {
    }
    
    public ItemSoundSet(@Nullable final String id, @Nullable final Map<ItemSoundEvent, Integer> soundEventIndices) {
        this.id = id;
        this.soundEventIndices = soundEventIndices;
    }
    
    public ItemSoundSet(@Nonnull final ItemSoundSet other) {
        this.id = other.id;
        this.soundEventIndices = other.soundEventIndices;
    }
    
    @Nonnull
    public static ItemSoundSet deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemSoundSet obj = new ItemSoundSet();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int soundEventIndicesCount = VarInt.peek(buf, varPos2);
            if (soundEventIndicesCount < 0) {
                throw ProtocolException.negativeLength("SoundEventIndices", soundEventIndicesCount);
            }
            if (soundEventIndicesCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("SoundEventIndices", soundEventIndicesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.soundEventIndices = new HashMap<ItemSoundEvent, Integer>(soundEventIndicesCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < soundEventIndicesCount; ++i) {
                final ItemSoundEvent key = ItemSoundEvent.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final int val = buf.getIntLE(dictPos);
                dictPos += 4;
                if (obj.soundEventIndices.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("soundEventIndices", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 9 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                ++pos2;
                pos2 += 4;
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.id != null) {
            nullBits |= 0x1;
        }
        if (this.soundEventIndices != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int soundEventIndicesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.soundEventIndices != null) {
            buf.setIntLE(soundEventIndicesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.soundEventIndices.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("SoundEventIndices", this.soundEventIndices.size(), 4096000);
            }
            VarInt.write(buf, this.soundEventIndices.size());
            for (final Map.Entry<ItemSoundEvent, Integer> e : this.soundEventIndices.entrySet()) {
                buf.writeByte(e.getKey().getValue());
                buf.writeIntLE(e.getValue());
            }
        }
        else {
            buf.setIntLE(soundEventIndicesOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.soundEventIndices != null) {
            size += VarInt.size(this.soundEventIndices.size()) + this.soundEventIndices.size() * 5;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 1);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 9 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int soundEventIndicesOffset = buffer.getIntLE(offset + 5);
            if (soundEventIndicesOffset < 0) {
                return ValidationResult.error("Invalid offset for SoundEventIndices");
            }
            int pos = offset + 9 + soundEventIndicesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for SoundEventIndices");
            }
            final int soundEventIndicesCount = VarInt.peek(buffer, pos);
            if (soundEventIndicesCount < 0) {
                return ValidationResult.error("Invalid dictionary count for SoundEventIndices");
            }
            if (soundEventIndicesCount > 4096000) {
                return ValidationResult.error("SoundEventIndices exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < soundEventIndicesCount; ++i) {
                ++pos;
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading value");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemSoundSet clone() {
        final ItemSoundSet copy = new ItemSoundSet();
        copy.id = this.id;
        copy.soundEventIndices = ((this.soundEventIndices != null) ? new HashMap<ItemSoundEvent, Integer>(this.soundEventIndices) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemSoundSet other) {
            return Objects.equals(this.id, other.id) && Objects.equals(this.soundEventIndices, other.soundEventIndices);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.soundEventIndices);
    }
}
