// 
// 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.Nonnull;
import javax.annotation.Nullable;

public class BlockBreaking
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 13;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 25;
    public static final int MAX_SIZE = 49152040;
    @Nullable
    public String gatherType;
    public float health;
    public int quantity;
    public int quality;
    @Nullable
    public String itemId;
    @Nullable
    public String dropListId;
    
    public BlockBreaking() {
        this.quantity = 1;
    }
    
    public BlockBreaking(@Nullable final String gatherType, final float health, final int quantity, final int quality, @Nullable final String itemId, @Nullable final String dropListId) {
        this.quantity = 1;
        this.gatherType = gatherType;
        this.health = health;
        this.quantity = quantity;
        this.quality = quality;
        this.itemId = itemId;
        this.dropListId = dropListId;
    }
    
    public BlockBreaking(@Nonnull final BlockBreaking other) {
        this.quantity = 1;
        this.gatherType = other.gatherType;
        this.health = other.health;
        this.quantity = other.quantity;
        this.quality = other.quality;
        this.itemId = other.itemId;
        this.dropListId = other.dropListId;
    }
    
    @Nonnull
    public static BlockBreaking deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockBreaking obj = new BlockBreaking();
        final byte nullBits = buf.getByte(offset);
        obj.health = buf.getFloatLE(offset + 1);
        obj.quantity = buf.getIntLE(offset + 5);
        obj.quality = buf.getIntLE(offset + 9);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 25 + buf.getIntLE(offset + 13);
            final int gatherTypeLen = VarInt.peek(buf, varPos0);
            if (gatherTypeLen < 0) {
                throw ProtocolException.negativeLength("GatherType", gatherTypeLen);
            }
            if (gatherTypeLen > 4096000) {
                throw ProtocolException.stringTooLong("GatherType", gatherTypeLen, 4096000);
            }
            obj.gatherType = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 25 + buf.getIntLE(offset + 17);
            final int itemIdLen = VarInt.peek(buf, varPos2);
            if (itemIdLen < 0) {
                throw ProtocolException.negativeLength("ItemId", itemIdLen);
            }
            if (itemIdLen > 4096000) {
                throw ProtocolException.stringTooLong("ItemId", itemIdLen, 4096000);
            }
            obj.itemId = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 25 + buf.getIntLE(offset + 21);
            final int dropListIdLen = VarInt.peek(buf, varPos3);
            if (dropListIdLen < 0) {
                throw ProtocolException.negativeLength("DropListId", dropListIdLen);
            }
            if (dropListIdLen > 4096000) {
                throw ProtocolException.stringTooLong("DropListId", dropListIdLen, 4096000);
            }
            obj.dropListId = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 25;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 13);
            int pos0 = offset + 25 + 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 + 17);
            int pos2 = offset + 25 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 21);
            int pos3 = offset + 25 + fieldOffset3;
            final int sl = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.gatherType != null) {
            nullBits |= 0x1;
        }
        if (this.itemId != null) {
            nullBits |= 0x2;
        }
        if (this.dropListId != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeFloatLE(this.health);
        buf.writeIntLE(this.quantity);
        buf.writeIntLE(this.quality);
        final int gatherTypeOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int itemIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int dropListIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.gatherType != null) {
            buf.setIntLE(gatherTypeOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.gatherType, 4096000);
        }
        else {
            buf.setIntLE(gatherTypeOffsetSlot, -1);
        }
        if (this.itemId != null) {
            buf.setIntLE(itemIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.itemId, 4096000);
        }
        else {
            buf.setIntLE(itemIdOffsetSlot, -1);
        }
        if (this.dropListId != null) {
            buf.setIntLE(dropListIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.dropListId, 4096000);
        }
        else {
            buf.setIntLE(dropListIdOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 25;
        if (this.gatherType != null) {
            size += PacketIO.stringSize(this.gatherType);
        }
        if (this.itemId != null) {
            size += PacketIO.stringSize(this.itemId);
        }
        if (this.dropListId != null) {
            size += PacketIO.stringSize(this.dropListId);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 25) {
            return ValidationResult.error("Buffer too small: expected at least 25 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int gatherTypeOffset = buffer.getIntLE(offset + 13);
            if (gatherTypeOffset < 0) {
                return ValidationResult.error("Invalid offset for GatherType");
            }
            int pos = offset + 25 + gatherTypeOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for GatherType");
            }
            final int gatherTypeLen = VarInt.peek(buffer, pos);
            if (gatherTypeLen < 0) {
                return ValidationResult.error("Invalid string length for GatherType");
            }
            if (gatherTypeLen > 4096000) {
                return ValidationResult.error("GatherType exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += gatherTypeLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading GatherType");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int itemIdOffset = buffer.getIntLE(offset + 17);
            if (itemIdOffset < 0) {
                return ValidationResult.error("Invalid offset for ItemId");
            }
            int pos = offset + 25 + 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 & 0x4) != 0x0) {
            final int dropListIdOffset = buffer.getIntLE(offset + 21);
            if (dropListIdOffset < 0) {
                return ValidationResult.error("Invalid offset for DropListId");
            }
            int pos = offset + 25 + dropListIdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for DropListId");
            }
            final int dropListIdLen = VarInt.peek(buffer, pos);
            if (dropListIdLen < 0) {
                return ValidationResult.error("Invalid string length for DropListId");
            }
            if (dropListIdLen > 4096000) {
                return ValidationResult.error("DropListId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += dropListIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading DropListId");
            }
        }
        return ValidationResult.OK;
    }
    
    public BlockBreaking clone() {
        final BlockBreaking copy = new BlockBreaking();
        copy.gatherType = this.gatherType;
        copy.health = this.health;
        copy.quantity = this.quantity;
        copy.quality = this.quality;
        copy.itemId = this.itemId;
        copy.dropListId = this.dropListId;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockBreaking other) {
            return Objects.equals(this.gatherType, other.gatherType) && this.health == other.health && this.quantity == other.quantity && this.quality == other.quality && Objects.equals(this.itemId, other.itemId) && Objects.equals(this.dropListId, other.dropListId);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.gatherType, this.health, this.quantity, this.quality, this.itemId, this.dropListId);
    }
}
