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

package com.hypixel.hytale.protocol.packets.buildertools;

import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.packets.interface_.FluidChange;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.packets.interface_.BlockChange;
import com.hypixel.hytale.protocol.Packet;

public class BuilderToolSelectionToolReplyWithClipboard implements Packet
{
    public static final int PACKET_ID = 411;
    public static final boolean IS_COMPRESSED = true;
    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 = 139264019;
    @Nullable
    public BlockChange[] blocksChange;
    @Nullable
    public FluidChange[] fluidsChange;
    
    @Override
    public int getId() {
        return 411;
    }
    
    public BuilderToolSelectionToolReplyWithClipboard() {
    }
    
    public BuilderToolSelectionToolReplyWithClipboard(@Nullable final BlockChange[] blocksChange, @Nullable final FluidChange[] fluidsChange) {
        this.blocksChange = blocksChange;
        this.fluidsChange = fluidsChange;
    }
    
    public BuilderToolSelectionToolReplyWithClipboard(@Nonnull final BuilderToolSelectionToolReplyWithClipboard other) {
        this.blocksChange = other.blocksChange;
        this.fluidsChange = other.fluidsChange;
    }
    
    @Nonnull
    public static BuilderToolSelectionToolReplyWithClipboard deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BuilderToolSelectionToolReplyWithClipboard obj = new BuilderToolSelectionToolReplyWithClipboard();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int blocksChangeCount = VarInt.peek(buf, varPos0);
            if (blocksChangeCount < 0) {
                throw ProtocolException.negativeLength("BlocksChange", blocksChangeCount);
            }
            if (blocksChangeCount > 4096000) {
                throw ProtocolException.arrayTooLong("BlocksChange", blocksChangeCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + blocksChangeCount * 17L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("BlocksChange", varPos0 + varIntLen + blocksChangeCount * 17, buf.readableBytes());
            }
            obj.blocksChange = new BlockChange[blocksChangeCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < blocksChangeCount; ++i) {
                obj.blocksChange[i] = BlockChange.deserialize(buf, elemPos);
                elemPos += BlockChange.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int fluidsChangeCount = VarInt.peek(buf, varPos2);
            if (fluidsChangeCount < 0) {
                throw ProtocolException.negativeLength("FluidsChange", fluidsChangeCount);
            }
            if (fluidsChangeCount > 4096000) {
                throw ProtocolException.arrayTooLong("FluidsChange", fluidsChangeCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + fluidsChangeCount * 17L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("FluidsChange", varPos2 + varIntLen + fluidsChangeCount * 17, buf.readableBytes());
            }
            obj.fluidsChange = new FluidChange[fluidsChangeCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < fluidsChangeCount; ++i) {
                obj.fluidsChange[i] = FluidChange.deserialize(buf, elemPos);
                elemPos += FluidChange.computeBytesConsumed(buf, elemPos);
            }
        }
        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 arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0);
            for (int i = 0; i < arrLen; ++i) {
                pos0 += BlockChange.computeBytesConsumed(buf, pos0);
            }
            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);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += FluidChange.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.blocksChange != null) {
            nullBits |= 0x1;
        }
        if (this.fluidsChange != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int blocksChangeOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int fluidsChangeOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.blocksChange != null) {
            buf.setIntLE(blocksChangeOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.blocksChange.length > 4096000) {
                throw ProtocolException.arrayTooLong("BlocksChange", this.blocksChange.length, 4096000);
            }
            VarInt.write(buf, this.blocksChange.length);
            for (final BlockChange item : this.blocksChange) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(blocksChangeOffsetSlot, -1);
        }
        if (this.fluidsChange != null) {
            buf.setIntLE(fluidsChangeOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.fluidsChange.length > 4096000) {
                throw ProtocolException.arrayTooLong("FluidsChange", this.fluidsChange.length, 4096000);
            }
            VarInt.write(buf, this.fluidsChange.length);
            for (final FluidChange item2 : this.fluidsChange) {
                item2.serialize(buf);
            }
        }
        else {
            buf.setIntLE(fluidsChangeOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 9;
        if (this.blocksChange != null) {
            size += VarInt.size(this.blocksChange.length) + this.blocksChange.length * 17;
        }
        if (this.fluidsChange != null) {
            size += VarInt.size(this.fluidsChange.length) + this.fluidsChange.length * 17;
        }
        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 blocksChangeOffset = buffer.getIntLE(offset + 1);
            if (blocksChangeOffset < 0) {
                return ValidationResult.error("Invalid offset for BlocksChange");
            }
            int pos = offset + 9 + blocksChangeOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for BlocksChange");
            }
            final int blocksChangeCount = VarInt.peek(buffer, pos);
            if (blocksChangeCount < 0) {
                return ValidationResult.error("Invalid array count for BlocksChange");
            }
            if (blocksChangeCount > 4096000) {
                return ValidationResult.error("BlocksChange exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += blocksChangeCount * 17;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading BlocksChange");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fluidsChangeOffset = buffer.getIntLE(offset + 5);
            if (fluidsChangeOffset < 0) {
                return ValidationResult.error("Invalid offset for FluidsChange");
            }
            int pos = offset + 9 + fluidsChangeOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for FluidsChange");
            }
            final int fluidsChangeCount = VarInt.peek(buffer, pos);
            if (fluidsChangeCount < 0) {
                return ValidationResult.error("Invalid array count for FluidsChange");
            }
            if (fluidsChangeCount > 4096000) {
                return ValidationResult.error("FluidsChange exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += fluidsChangeCount * 17;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading FluidsChange");
            }
        }
        return ValidationResult.OK;
    }
    
    public BuilderToolSelectionToolReplyWithClipboard clone() {
        final BuilderToolSelectionToolReplyWithClipboard copy = new BuilderToolSelectionToolReplyWithClipboard();
        copy.blocksChange = (BlockChange[])((this.blocksChange != null) ? ((BlockChange[])Arrays.stream(this.blocksChange).map(e -> e.clone()).toArray(BlockChange[]::new)) : null);
        copy.fluidsChange = (FluidChange[])((this.fluidsChange != null) ? ((FluidChange[])Arrays.stream(this.fluidsChange).map(e -> e.clone()).toArray(FluidChange[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BuilderToolSelectionToolReplyWithClipboard other) {
            return Arrays.equals(this.blocksChange, other.blocksChange) && Arrays.equals(this.fluidsChange, other.fluidsChange);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.blocksChange);
        result = 31 * result + Arrays.hashCode(this.fluidsChange);
        return result;
    }
}
