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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import java.util.Arrays;
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 BlockSet
{
    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 = 32768019;
    @Nullable
    public String name;
    @Nullable
    public int[] blocks;
    
    public BlockSet() {
    }
    
    public BlockSet(@Nullable final String name, @Nullable final int[] blocks) {
        this.name = name;
        this.blocks = blocks;
    }
    
    public BlockSet(@Nonnull final BlockSet other) {
        this.name = other.name;
        this.blocks = other.blocks;
    }
    
    @Nonnull
    public static BlockSet deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockSet obj = new BlockSet();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int nameLen = VarInt.peek(buf, varPos0);
            if (nameLen < 0) {
                throw ProtocolException.negativeLength("Name", nameLen);
            }
            if (nameLen > 4096000) {
                throw ProtocolException.stringTooLong("Name", nameLen, 4096000);
            }
            obj.name = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int blocksCount = VarInt.peek(buf, varPos2);
            if (blocksCount < 0) {
                throw ProtocolException.negativeLength("Blocks", blocksCount);
            }
            if (blocksCount > 4096000) {
                throw ProtocolException.arrayTooLong("Blocks", blocksCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + blocksCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Blocks", varPos2 + varIntLen + blocksCount * 4, buf.readableBytes());
            }
            obj.blocks = new int[blocksCount];
            for (int i = 0; i < blocksCount; ++i) {
                obj.blocks[i] = buf.getIntLE(varPos2 + varIntLen + i * 4);
            }
        }
        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 arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + arrLen * 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.name != null) {
            nullBits |= 0x1;
        }
        if (this.blocks != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int nameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int blocksOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.name != null) {
            buf.setIntLE(nameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.name, 4096000);
        }
        else {
            buf.setIntLE(nameOffsetSlot, -1);
        }
        if (this.blocks != null) {
            buf.setIntLE(blocksOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.blocks.length > 4096000) {
                throw ProtocolException.arrayTooLong("Blocks", this.blocks.length, 4096000);
            }
            VarInt.write(buf, this.blocks.length);
            for (final int item : this.blocks) {
                buf.writeIntLE(item);
            }
        }
        else {
            buf.setIntLE(blocksOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.name != null) {
            size += PacketIO.stringSize(this.name);
        }
        if (this.blocks != null) {
            size += VarInt.size(this.blocks.length) + this.blocks.length * 4;
        }
        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 nameOffset = buffer.getIntLE(offset + 1);
            if (nameOffset < 0) {
                return ValidationResult.error("Invalid offset for Name");
            }
            int pos = offset + 9 + nameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Name");
            }
            final int nameLen = VarInt.peek(buffer, pos);
            if (nameLen < 0) {
                return ValidationResult.error("Invalid string length for Name");
            }
            if (nameLen > 4096000) {
                return ValidationResult.error("Name exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += nameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Name");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int blocksOffset = buffer.getIntLE(offset + 5);
            if (blocksOffset < 0) {
                return ValidationResult.error("Invalid offset for Blocks");
            }
            int pos = offset + 9 + blocksOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Blocks");
            }
            final int blocksCount = VarInt.peek(buffer, pos);
            if (blocksCount < 0) {
                return ValidationResult.error("Invalid array count for Blocks");
            }
            if (blocksCount > 4096000) {
                return ValidationResult.error("Blocks exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += blocksCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Blocks");
            }
        }
        return ValidationResult.OK;
    }
    
    public BlockSet clone() {
        final BlockSet copy = new BlockSet();
        copy.name = this.name;
        copy.blocks = (int[])((this.blocks != null) ? Arrays.copyOf(this.blocks, this.blocks.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockSet other) {
            return Objects.equals(this.name, other.name) && Arrays.equals(this.blocks, other.blocks);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.name);
        result = 31 * result + Arrays.hashCode(this.blocks);
        return result;
    }
}
