// 
// 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 SoftBlock
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 10;
    public static final int MAX_SIZE = 32768020;
    @Nullable
    public String itemId;
    @Nullable
    public String dropListId;
    public boolean isWeaponBreakable;
    
    public SoftBlock() {
    }
    
    public SoftBlock(@Nullable final String itemId, @Nullable final String dropListId, final boolean isWeaponBreakable) {
        this.itemId = itemId;
        this.dropListId = dropListId;
        this.isWeaponBreakable = isWeaponBreakable;
    }
    
    public SoftBlock(@Nonnull final SoftBlock other) {
        this.itemId = other.itemId;
        this.dropListId = other.dropListId;
        this.isWeaponBreakable = other.isWeaponBreakable;
    }
    
    @Nonnull
    public static SoftBlock deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SoftBlock obj = new SoftBlock();
        final byte nullBits = buf.getByte(offset);
        obj.isWeaponBreakable = (buf.getByte(offset + 1) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 10 + buf.getIntLE(offset + 2);
            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 & 0x2) != 0x0) {
            final int varPos2 = offset + 10 + buf.getIntLE(offset + 6);
            final int dropListIdLen = VarInt.peek(buf, varPos2);
            if (dropListIdLen < 0) {
                throw ProtocolException.negativeLength("DropListId", dropListIdLen);
            }
            if (dropListIdLen > 4096000) {
                throw ProtocolException.stringTooLong("DropListId", dropListIdLen, 4096000);
            }
            obj.dropListId = 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 = 10;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 2);
            int pos0 = offset + 10 + 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 + 6);
            int pos2 = offset + 10 + fieldOffset2;
            final int 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.itemId != null) {
            nullBits |= 0x1;
        }
        if (this.dropListId != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.isWeaponBreakable ? 1 : 0);
        final int itemIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int dropListIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        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 = 10;
        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 < 10) {
            return ValidationResult.error("Buffer too small: expected at least 10 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int itemIdOffset = buffer.getIntLE(offset + 2);
            if (itemIdOffset < 0) {
                return ValidationResult.error("Invalid offset for ItemId");
            }
            int pos = offset + 10 + 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 & 0x2) != 0x0) {
            final int dropListIdOffset = buffer.getIntLE(offset + 6);
            if (dropListIdOffset < 0) {
                return ValidationResult.error("Invalid offset for DropListId");
            }
            int pos = offset + 10 + 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 SoftBlock clone() {
        final SoftBlock copy = new SoftBlock();
        copy.itemId = this.itemId;
        copy.dropListId = this.dropListId;
        copy.isWeaponBreakable = this.isWeaponBreakable;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SoftBlock other) {
            return Objects.equals(this.itemId, other.itemId) && Objects.equals(this.dropListId, other.dropListId) && this.isWeaponBreakable == other.isWeaponBreakable;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.itemId, this.dropListId, this.isWeaponBreakable);
    }
}
