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

package com.hypixel.hytale.protocol;

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

public class InteractionRules
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 17;
    public static final int VARIABLE_FIELD_COUNT = 4;
    public static final int VARIABLE_BLOCK_START = 33;
    public static final int MAX_SIZE = 16384053;
    @Nullable
    public InteractionType[] blockedBy;
    @Nullable
    public InteractionType[] blocking;
    @Nullable
    public InteractionType[] interruptedBy;
    @Nullable
    public InteractionType[] interrupting;
    public int blockedByBypassIndex;
    public int blockingBypassIndex;
    public int interruptedByBypassIndex;
    public int interruptingBypassIndex;
    
    public InteractionRules() {
    }
    
    public InteractionRules(@Nullable final InteractionType[] blockedBy, @Nullable final InteractionType[] blocking, @Nullable final InteractionType[] interruptedBy, @Nullable final InteractionType[] interrupting, final int blockedByBypassIndex, final int blockingBypassIndex, final int interruptedByBypassIndex, final int interruptingBypassIndex) {
        this.blockedBy = blockedBy;
        this.blocking = blocking;
        this.interruptedBy = interruptedBy;
        this.interrupting = interrupting;
        this.blockedByBypassIndex = blockedByBypassIndex;
        this.blockingBypassIndex = blockingBypassIndex;
        this.interruptedByBypassIndex = interruptedByBypassIndex;
        this.interruptingBypassIndex = interruptingBypassIndex;
    }
    
    public InteractionRules(@Nonnull final InteractionRules other) {
        this.blockedBy = other.blockedBy;
        this.blocking = other.blocking;
        this.interruptedBy = other.interruptedBy;
        this.interrupting = other.interrupting;
        this.blockedByBypassIndex = other.blockedByBypassIndex;
        this.blockingBypassIndex = other.blockingBypassIndex;
        this.interruptedByBypassIndex = other.interruptedByBypassIndex;
        this.interruptingBypassIndex = other.interruptingBypassIndex;
    }
    
    @Nonnull
    public static InteractionRules deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final InteractionRules obj = new InteractionRules();
        final byte nullBits = buf.getByte(offset);
        obj.blockedByBypassIndex = buf.getIntLE(offset + 1);
        obj.blockingBypassIndex = buf.getIntLE(offset + 5);
        obj.interruptedByBypassIndex = buf.getIntLE(offset + 9);
        obj.interruptingBypassIndex = buf.getIntLE(offset + 13);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 33 + buf.getIntLE(offset + 17);
            final int blockedByCount = VarInt.peek(buf, varPos0);
            if (blockedByCount < 0) {
                throw ProtocolException.negativeLength("BlockedBy", blockedByCount);
            }
            if (blockedByCount > 4096000) {
                throw ProtocolException.arrayTooLong("BlockedBy", blockedByCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + blockedByCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("BlockedBy", varPos0 + varIntLen + blockedByCount * 1, buf.readableBytes());
            }
            obj.blockedBy = new InteractionType[blockedByCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < blockedByCount; ++i) {
                obj.blockedBy[i] = InteractionType.fromValue(buf.getByte(elemPos));
                ++elemPos;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 33 + buf.getIntLE(offset + 21);
            final int blockingCount = VarInt.peek(buf, varPos2);
            if (blockingCount < 0) {
                throw ProtocolException.negativeLength("Blocking", blockingCount);
            }
            if (blockingCount > 4096000) {
                throw ProtocolException.arrayTooLong("Blocking", blockingCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + blockingCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Blocking", varPos2 + varIntLen + blockingCount * 1, buf.readableBytes());
            }
            obj.blocking = new InteractionType[blockingCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < blockingCount; ++i) {
                obj.blocking[i] = InteractionType.fromValue(buf.getByte(elemPos));
                ++elemPos;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 33 + buf.getIntLE(offset + 25);
            final int interruptedByCount = VarInt.peek(buf, varPos3);
            if (interruptedByCount < 0) {
                throw ProtocolException.negativeLength("InterruptedBy", interruptedByCount);
            }
            if (interruptedByCount > 4096000) {
                throw ProtocolException.arrayTooLong("InterruptedBy", interruptedByCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + interruptedByCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("InterruptedBy", varPos3 + varIntLen + interruptedByCount * 1, buf.readableBytes());
            }
            obj.interruptedBy = new InteractionType[interruptedByCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < interruptedByCount; ++i) {
                obj.interruptedBy[i] = InteractionType.fromValue(buf.getByte(elemPos));
                ++elemPos;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 33 + buf.getIntLE(offset + 29);
            final int interruptingCount = VarInt.peek(buf, varPos4);
            if (interruptingCount < 0) {
                throw ProtocolException.negativeLength("Interrupting", interruptingCount);
            }
            if (interruptingCount > 4096000) {
                throw ProtocolException.arrayTooLong("Interrupting", interruptingCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            if (varPos4 + varIntLen + interruptingCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Interrupting", varPos4 + varIntLen + interruptingCount * 1, buf.readableBytes());
            }
            obj.interrupting = new InteractionType[interruptingCount];
            int elemPos = varPos4 + varIntLen;
            for (int i = 0; i < interruptingCount; ++i) {
                obj.interrupting[i] = InteractionType.fromValue(buf.getByte(elemPos));
                ++elemPos;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 33;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 17);
            int pos0 = offset + 33 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + arrLen * 1;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 21);
            int pos2 = offset + 33 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + arrLen * 1;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 25);
            int pos3 = offset + 33 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + arrLen * 1;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 29);
            int pos4 = offset + 33 + fieldOffset4;
            final int arrLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4) + arrLen * 1;
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.blockedBy != null) {
            nullBits |= 0x1;
        }
        if (this.blocking != null) {
            nullBits |= 0x2;
        }
        if (this.interruptedBy != null) {
            nullBits |= 0x4;
        }
        if (this.interrupting != null) {
            nullBits |= 0x8;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.blockedByBypassIndex);
        buf.writeIntLE(this.blockingBypassIndex);
        buf.writeIntLE(this.interruptedByBypassIndex);
        buf.writeIntLE(this.interruptingBypassIndex);
        final int blockedByOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int blockingOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int interruptedByOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int interruptingOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.blockedBy != null) {
            buf.setIntLE(blockedByOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.blockedBy.length > 4096000) {
                throw ProtocolException.arrayTooLong("BlockedBy", this.blockedBy.length, 4096000);
            }
            VarInt.write(buf, this.blockedBy.length);
            for (final InteractionType item : this.blockedBy) {
                buf.writeByte(item.getValue());
            }
        }
        else {
            buf.setIntLE(blockedByOffsetSlot, -1);
        }
        if (this.blocking != null) {
            buf.setIntLE(blockingOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.blocking.length > 4096000) {
                throw ProtocolException.arrayTooLong("Blocking", this.blocking.length, 4096000);
            }
            VarInt.write(buf, this.blocking.length);
            for (final InteractionType item : this.blocking) {
                buf.writeByte(item.getValue());
            }
        }
        else {
            buf.setIntLE(blockingOffsetSlot, -1);
        }
        if (this.interruptedBy != null) {
            buf.setIntLE(interruptedByOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.interruptedBy.length > 4096000) {
                throw ProtocolException.arrayTooLong("InterruptedBy", this.interruptedBy.length, 4096000);
            }
            VarInt.write(buf, this.interruptedBy.length);
            for (final InteractionType item : this.interruptedBy) {
                buf.writeByte(item.getValue());
            }
        }
        else {
            buf.setIntLE(interruptedByOffsetSlot, -1);
        }
        if (this.interrupting != null) {
            buf.setIntLE(interruptingOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.interrupting.length > 4096000) {
                throw ProtocolException.arrayTooLong("Interrupting", this.interrupting.length, 4096000);
            }
            VarInt.write(buf, this.interrupting.length);
            for (final InteractionType item : this.interrupting) {
                buf.writeByte(item.getValue());
            }
        }
        else {
            buf.setIntLE(interruptingOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 33;
        if (this.blockedBy != null) {
            size += VarInt.size(this.blockedBy.length) + this.blockedBy.length * 1;
        }
        if (this.blocking != null) {
            size += VarInt.size(this.blocking.length) + this.blocking.length * 1;
        }
        if (this.interruptedBy != null) {
            size += VarInt.size(this.interruptedBy.length) + this.interruptedBy.length * 1;
        }
        if (this.interrupting != null) {
            size += VarInt.size(this.interrupting.length) + this.interrupting.length * 1;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 33) {
            return ValidationResult.error("Buffer too small: expected at least 33 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int blockedByOffset = buffer.getIntLE(offset + 17);
            if (blockedByOffset < 0) {
                return ValidationResult.error("Invalid offset for BlockedBy");
            }
            int pos = offset + 33 + blockedByOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for BlockedBy");
            }
            final int blockedByCount = VarInt.peek(buffer, pos);
            if (blockedByCount < 0) {
                return ValidationResult.error("Invalid array count for BlockedBy");
            }
            if (blockedByCount > 4096000) {
                return ValidationResult.error("BlockedBy exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += blockedByCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading BlockedBy");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int blockingOffset = buffer.getIntLE(offset + 21);
            if (blockingOffset < 0) {
                return ValidationResult.error("Invalid offset for Blocking");
            }
            int pos = offset + 33 + blockingOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Blocking");
            }
            final int blockingCount = VarInt.peek(buffer, pos);
            if (blockingCount < 0) {
                return ValidationResult.error("Invalid array count for Blocking");
            }
            if (blockingCount > 4096000) {
                return ValidationResult.error("Blocking exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += blockingCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Blocking");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int interruptedByOffset = buffer.getIntLE(offset + 25);
            if (interruptedByOffset < 0) {
                return ValidationResult.error("Invalid offset for InterruptedBy");
            }
            int pos = offset + 33 + interruptedByOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for InterruptedBy");
            }
            final int interruptedByCount = VarInt.peek(buffer, pos);
            if (interruptedByCount < 0) {
                return ValidationResult.error("Invalid array count for InterruptedBy");
            }
            if (interruptedByCount > 4096000) {
                return ValidationResult.error("InterruptedBy exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += interruptedByCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading InterruptedBy");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int interruptingOffset = buffer.getIntLE(offset + 29);
            if (interruptingOffset < 0) {
                return ValidationResult.error("Invalid offset for Interrupting");
            }
            int pos = offset + 33 + interruptingOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Interrupting");
            }
            final int interruptingCount = VarInt.peek(buffer, pos);
            if (interruptingCount < 0) {
                return ValidationResult.error("Invalid array count for Interrupting");
            }
            if (interruptingCount > 4096000) {
                return ValidationResult.error("Interrupting exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += interruptingCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Interrupting");
            }
        }
        return ValidationResult.OK;
    }
    
    public InteractionRules clone() {
        final InteractionRules copy = new InteractionRules();
        copy.blockedBy = (InteractionType[])((this.blockedBy != null) ? ((InteractionType[])Arrays.copyOf(this.blockedBy, this.blockedBy.length)) : null);
        copy.blocking = (InteractionType[])((this.blocking != null) ? ((InteractionType[])Arrays.copyOf(this.blocking, this.blocking.length)) : null);
        copy.interruptedBy = (InteractionType[])((this.interruptedBy != null) ? ((InteractionType[])Arrays.copyOf(this.interruptedBy, this.interruptedBy.length)) : null);
        copy.interrupting = (InteractionType[])((this.interrupting != null) ? ((InteractionType[])Arrays.copyOf(this.interrupting, this.interrupting.length)) : null);
        copy.blockedByBypassIndex = this.blockedByBypassIndex;
        copy.blockingBypassIndex = this.blockingBypassIndex;
        copy.interruptedByBypassIndex = this.interruptedByBypassIndex;
        copy.interruptingBypassIndex = this.interruptingBypassIndex;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final InteractionRules other) {
            return Arrays.equals(this.blockedBy, other.blockedBy) && Arrays.equals(this.blocking, other.blocking) && Arrays.equals(this.interruptedBy, other.interruptedBy) && Arrays.equals(this.interrupting, other.interrupting) && this.blockedByBypassIndex == other.blockedByBypassIndex && this.blockingBypassIndex == other.blockingBypassIndex && this.interruptedByBypassIndex == other.interruptedByBypassIndex && this.interruptingBypassIndex == other.interruptingBypassIndex;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.blockedBy);
        result = 31 * result + Arrays.hashCode(this.blocking);
        result = 31 * result + Arrays.hashCode(this.interruptedBy);
        result = 31 * result + Arrays.hashCode(this.interrupting);
        result = 31 * result + Integer.hashCode(this.blockedByBypassIndex);
        result = 31 * result + Integer.hashCode(this.blockingBypassIndex);
        result = 31 * result + Integer.hashCode(this.interruptedByBypassIndex);
        result = 31 * result + Integer.hashCode(this.interruptingBypassIndex);
        return result;
    }
}
