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

package com.hypixel.hytale.protocol;

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

public class ItemWithAllMetadata
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 22;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 30;
    public static final int MAX_SIZE = 32768040;
    @Nonnull
    public String itemId;
    public int quantity;
    public double durability;
    public double maxDurability;
    public boolean overrideDroppedItemAnimation;
    @Nullable
    public String metadata;
    
    public ItemWithAllMetadata() {
        this.itemId = "";
    }
    
    public ItemWithAllMetadata(@Nonnull final String itemId, final int quantity, final double durability, final double maxDurability, final boolean overrideDroppedItemAnimation, @Nullable final String metadata) {
        this.itemId = "";
        this.itemId = itemId;
        this.quantity = quantity;
        this.durability = durability;
        this.maxDurability = maxDurability;
        this.overrideDroppedItemAnimation = overrideDroppedItemAnimation;
        this.metadata = metadata;
    }
    
    public ItemWithAllMetadata(@Nonnull final ItemWithAllMetadata other) {
        this.itemId = "";
        this.itemId = other.itemId;
        this.quantity = other.quantity;
        this.durability = other.durability;
        this.maxDurability = other.maxDurability;
        this.overrideDroppedItemAnimation = other.overrideDroppedItemAnimation;
        this.metadata = other.metadata;
    }
    
    @Nonnull
    public static ItemWithAllMetadata deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemWithAllMetadata obj = new ItemWithAllMetadata();
        final byte nullBits = buf.getByte(offset);
        obj.quantity = buf.getIntLE(offset + 1);
        obj.durability = buf.getDoubleLE(offset + 5);
        obj.maxDurability = buf.getDoubleLE(offset + 13);
        obj.overrideDroppedItemAnimation = (buf.getByte(offset + 21) != 0);
        final int varPos0 = offset + 30 + buf.getIntLE(offset + 22);
        final int itemIdLen = VarInt.peek(buf, varPos0);
        if (itemIdLen < 0) {
            throw ProtocolException.negativeLength("ItemId", itemIdLen);
        }
        if (itemIdLen > 4096000) {
            throw ProtocolException.stringTooLong("ItemId", itemIdLen, 4096000);
        }
        obj.itemId = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos2 = offset + 30 + buf.getIntLE(offset + 26);
            final int metadataLen = VarInt.peek(buf, varPos2);
            if (metadataLen < 0) {
                throw ProtocolException.negativeLength("Metadata", metadataLen);
            }
            if (metadataLen > 4096000) {
                throw ProtocolException.stringTooLong("Metadata", metadataLen, 4096000);
            }
            obj.metadata = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 30;
        final int fieldOffset0 = buf.getIntLE(offset + 22);
        int pos0 = offset + 30 + fieldOffset0;
        int sl = VarInt.peek(buf, pos0);
        pos0 += VarInt.length(buf, pos0) + sl;
        if (pos0 - offset > maxEnd) {
            maxEnd = pos0 - offset;
        }
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 26);
            int pos2 = offset + 30 + fieldOffset2;
            sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            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.metadata != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.quantity);
        buf.writeDoubleLE(this.durability);
        buf.writeDoubleLE(this.maxDurability);
        buf.writeByte(this.overrideDroppedItemAnimation ? 1 : 0);
        final int itemIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int metadataOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        buf.setIntLE(itemIdOffsetSlot, buf.writerIndex() - varBlockStart);
        PacketIO.writeVarString(buf, this.itemId, 4096000);
        if (this.metadata != null) {
            buf.setIntLE(metadataOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.metadata, 4096000);
        }
        else {
            buf.setIntLE(metadataOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 30;
        size += PacketIO.stringSize(this.itemId);
        if (this.metadata != null) {
            size += PacketIO.stringSize(this.metadata);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 30) {
            return ValidationResult.error("Buffer too small: expected at least 30 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        final int itemIdOffset = buffer.getIntLE(offset + 22);
        if (itemIdOffset < 0) {
            return ValidationResult.error("Invalid offset for ItemId");
        }
        int pos = offset + 30 + itemIdOffset;
        if (pos >= buffer.writerIndex()) {
            return ValidationResult.error("Offset out of bounds for ItemId");
        }
        final int itemIdLen = VarInt.peek(buffer, pos);
        if (itemIdLen < 0) {
            return ValidationResult.error("Invalid string length for ItemId");
        }
        if (itemIdLen > 4096000) {
            return ValidationResult.error("ItemId exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        pos += itemIdLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading ItemId");
        }
        if ((nullBits & 0x1) != 0x0) {
            final int metadataOffset = buffer.getIntLE(offset + 26);
            if (metadataOffset < 0) {
                return ValidationResult.error("Invalid offset for Metadata");
            }
            pos = offset + 30 + metadataOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Metadata");
            }
            final int metadataLen = VarInt.peek(buffer, pos);
            if (metadataLen < 0) {
                return ValidationResult.error("Invalid string length for Metadata");
            }
            if (metadataLen > 4096000) {
                return ValidationResult.error("Metadata exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += metadataLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Metadata");
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemWithAllMetadata clone() {
        final ItemWithAllMetadata copy = new ItemWithAllMetadata();
        copy.itemId = this.itemId;
        copy.quantity = this.quantity;
        copy.durability = this.durability;
        copy.maxDurability = this.maxDurability;
        copy.overrideDroppedItemAnimation = this.overrideDroppedItemAnimation;
        copy.metadata = this.metadata;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemWithAllMetadata other) {
            return Objects.equals(this.itemId, other.itemId) && this.quantity == other.quantity && this.durability == other.durability && this.maxDurability == other.maxDurability && this.overrideDroppedItemAnimation == other.overrideDroppedItemAnimation && Objects.equals(this.metadata, other.metadata);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.itemId, this.quantity, this.durability, this.maxDurability, this.overrideDroppedItemAnimation, this.metadata);
    }
}
