// 
// 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 java.util.Iterator;
import java.util.HashMap;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class BlockConditionInteraction extends SimpleBlockInteraction
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 20;
    public static final int VARIABLE_FIELD_COUNT = 6;
    public static final int VARIABLE_BLOCK_START = 44;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public BlockMatcher[] matchers;
    
    public BlockConditionInteraction() {
    }
    
    public BlockConditionInteraction(@Nonnull final WaitForDataFrom waitForDataFrom, @Nullable final InteractionEffects effects, final float horizontalSpeedMultiplier, final float runTime, final boolean cancelOnItemChange, @Nullable final Map<GameMode, InteractionSettings> settings, @Nullable final InteractionRules rules, @Nullable final int[] tags, @Nullable final InteractionCameraSettings camera, final int next, final int failed, final boolean useLatestTarget, @Nullable final BlockMatcher[] matchers) {
        this.waitForDataFrom = waitForDataFrom;
        this.effects = effects;
        this.horizontalSpeedMultiplier = horizontalSpeedMultiplier;
        this.runTime = runTime;
        this.cancelOnItemChange = cancelOnItemChange;
        this.settings = settings;
        this.rules = rules;
        this.tags = tags;
        this.camera = camera;
        this.next = next;
        this.failed = failed;
        this.useLatestTarget = useLatestTarget;
        this.matchers = matchers;
    }
    
    public BlockConditionInteraction(@Nonnull final BlockConditionInteraction other) {
        this.waitForDataFrom = other.waitForDataFrom;
        this.effects = other.effects;
        this.horizontalSpeedMultiplier = other.horizontalSpeedMultiplier;
        this.runTime = other.runTime;
        this.cancelOnItemChange = other.cancelOnItemChange;
        this.settings = other.settings;
        this.rules = other.rules;
        this.tags = other.tags;
        this.camera = other.camera;
        this.next = other.next;
        this.failed = other.failed;
        this.useLatestTarget = other.useLatestTarget;
        this.matchers = other.matchers;
    }
    
    @Nonnull
    public static BlockConditionInteraction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockConditionInteraction obj = new BlockConditionInteraction();
        final byte nullBits = buf.getByte(offset);
        obj.waitForDataFrom = WaitForDataFrom.fromValue(buf.getByte(offset + 1));
        obj.horizontalSpeedMultiplier = buf.getFloatLE(offset + 2);
        obj.runTime = buf.getFloatLE(offset + 6);
        obj.cancelOnItemChange = (buf.getByte(offset + 10) != 0);
        obj.next = buf.getIntLE(offset + 11);
        obj.failed = buf.getIntLE(offset + 15);
        obj.useLatestTarget = (buf.getByte(offset + 19) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 44 + buf.getIntLE(offset + 20);
            obj.effects = InteractionEffects.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 44 + buf.getIntLE(offset + 24);
            final int settingsCount = VarInt.peek(buf, varPos2);
            if (settingsCount < 0) {
                throw ProtocolException.negativeLength("Settings", settingsCount);
            }
            if (settingsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Settings", settingsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.settings = new HashMap<GameMode, InteractionSettings>(settingsCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < settingsCount; ++i) {
                final GameMode key = GameMode.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final InteractionSettings val = InteractionSettings.deserialize(buf, dictPos);
                dictPos += InteractionSettings.computeBytesConsumed(buf, dictPos);
                if (obj.settings.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("settings", key);
                }
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 44 + buf.getIntLE(offset + 28);
            obj.rules = InteractionRules.deserialize(buf, varPos3);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 44 + buf.getIntLE(offset + 32);
            final int tagsCount = VarInt.peek(buf, varPos4);
            if (tagsCount < 0) {
                throw ProtocolException.negativeLength("Tags", tagsCount);
            }
            if (tagsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Tags", tagsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            if (varPos4 + varIntLen + tagsCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Tags", varPos4 + varIntLen + tagsCount * 4, buf.readableBytes());
            }
            obj.tags = new int[tagsCount];
            for (int j = 0; j < tagsCount; ++j) {
                obj.tags[j] = buf.getIntLE(varPos4 + varIntLen + j * 4);
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos5 = offset + 44 + buf.getIntLE(offset + 36);
            obj.camera = InteractionCameraSettings.deserialize(buf, varPos5);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int varPos6 = offset + 44 + buf.getIntLE(offset + 40);
            final int matchersCount = VarInt.peek(buf, varPos6);
            if (matchersCount < 0) {
                throw ProtocolException.negativeLength("Matchers", matchersCount);
            }
            if (matchersCount > 4096000) {
                throw ProtocolException.arrayTooLong("Matchers", matchersCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos6);
            if (varPos6 + varIntLen + matchersCount * 3L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Matchers", varPos6 + varIntLen + matchersCount * 3, buf.readableBytes());
            }
            obj.matchers = new BlockMatcher[matchersCount];
            int elemPos = varPos6 + varIntLen;
            for (int i = 0; i < matchersCount; ++i) {
                obj.matchers[i] = BlockMatcher.deserialize(buf, elemPos);
                elemPos += BlockMatcher.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 44;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 20);
            int pos0 = offset + 44 + fieldOffset0;
            pos0 += InteractionEffects.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 24);
            int pos2 = offset + 44 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                pos2 = ++pos2 + InteractionSettings.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 28);
            int pos3 = offset + 44 + fieldOffset3;
            pos3 += InteractionRules.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 32);
            int pos4 = offset + 44 + fieldOffset4;
            final int arrLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4) + arrLen * 4;
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 36);
            int pos5 = offset + 44 + fieldOffset5;
            pos5 += InteractionCameraSettings.computeBytesConsumed(buf, pos5);
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset6 = buf.getIntLE(offset + 40);
            int pos6 = offset + 44 + fieldOffset6;
            final int arrLen = VarInt.peek(buf, pos6);
            pos6 += VarInt.length(buf, pos6);
            for (int i = 0; i < arrLen; ++i) {
                pos6 += BlockMatcher.computeBytesConsumed(buf, pos6);
            }
            if (pos6 - offset > maxEnd) {
                maxEnd = pos6 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public int serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.effects != null) {
            nullBits |= 0x1;
        }
        if (this.settings != null) {
            nullBits |= 0x2;
        }
        if (this.rules != null) {
            nullBits |= 0x4;
        }
        if (this.tags != null) {
            nullBits |= 0x8;
        }
        if (this.camera != null) {
            nullBits |= 0x10;
        }
        if (this.matchers != null) {
            nullBits |= 0x20;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.waitForDataFrom.getValue());
        buf.writeFloatLE(this.horizontalSpeedMultiplier);
        buf.writeFloatLE(this.runTime);
        buf.writeByte(this.cancelOnItemChange ? 1 : 0);
        buf.writeIntLE(this.next);
        buf.writeIntLE(this.failed);
        buf.writeByte(this.useLatestTarget ? 1 : 0);
        final int effectsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int settingsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int rulesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int tagsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int cameraOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int matchersOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.effects != null) {
            buf.setIntLE(effectsOffsetSlot, buf.writerIndex() - varBlockStart);
            this.effects.serialize(buf);
        }
        else {
            buf.setIntLE(effectsOffsetSlot, -1);
        }
        if (this.settings != null) {
            buf.setIntLE(settingsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.settings.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Settings", this.settings.size(), 4096000);
            }
            VarInt.write(buf, this.settings.size());
            for (final Map.Entry<GameMode, InteractionSettings> e : this.settings.entrySet()) {
                buf.writeByte(e.getKey().getValue());
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(settingsOffsetSlot, -1);
        }
        if (this.rules != null) {
            buf.setIntLE(rulesOffsetSlot, buf.writerIndex() - varBlockStart);
            this.rules.serialize(buf);
        }
        else {
            buf.setIntLE(rulesOffsetSlot, -1);
        }
        if (this.tags != null) {
            buf.setIntLE(tagsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.tags.length > 4096000) {
                throw ProtocolException.arrayTooLong("Tags", this.tags.length, 4096000);
            }
            VarInt.write(buf, this.tags.length);
            for (final int item : this.tags) {
                buf.writeIntLE(item);
            }
        }
        else {
            buf.setIntLE(tagsOffsetSlot, -1);
        }
        if (this.camera != null) {
            buf.setIntLE(cameraOffsetSlot, buf.writerIndex() - varBlockStart);
            this.camera.serialize(buf);
        }
        else {
            buf.setIntLE(cameraOffsetSlot, -1);
        }
        if (this.matchers != null) {
            buf.setIntLE(matchersOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.matchers.length > 4096000) {
                throw ProtocolException.arrayTooLong("Matchers", this.matchers.length, 4096000);
            }
            VarInt.write(buf, this.matchers.length);
            for (final BlockMatcher item2 : this.matchers) {
                item2.serialize(buf);
            }
        }
        else {
            buf.setIntLE(matchersOffsetSlot, -1);
        }
        return buf.writerIndex() - startPos;
    }
    
    @Override
    public int computeSize() {
        int size = 44;
        if (this.effects != null) {
            size += this.effects.computeSize();
        }
        if (this.settings != null) {
            size += VarInt.size(this.settings.size()) + this.settings.size() * 2;
        }
        if (this.rules != null) {
            size += this.rules.computeSize();
        }
        if (this.tags != null) {
            size += VarInt.size(this.tags.length) + this.tags.length * 4;
        }
        if (this.camera != null) {
            size += this.camera.computeSize();
        }
        if (this.matchers != null) {
            int matchersSize = 0;
            for (final BlockMatcher elem : this.matchers) {
                matchersSize += elem.computeSize();
            }
            size += VarInt.size(this.matchers.length) + matchersSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 44) {
            return ValidationResult.error("Buffer too small: expected at least 44 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int effectsOffset = buffer.getIntLE(offset + 20);
            if (effectsOffset < 0) {
                return ValidationResult.error("Invalid offset for Effects");
            }
            int pos = offset + 44 + effectsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Effects");
            }
            final ValidationResult effectsResult = InteractionEffects.validateStructure(buffer, pos);
            if (!effectsResult.isValid()) {
                return ValidationResult.error("Invalid Effects: " + effectsResult.error());
            }
            pos += InteractionEffects.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int settingsOffset = buffer.getIntLE(offset + 24);
            if (settingsOffset < 0) {
                return ValidationResult.error("Invalid offset for Settings");
            }
            int pos = offset + 44 + settingsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Settings");
            }
            final int settingsCount = VarInt.peek(buffer, pos);
            if (settingsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Settings");
            }
            if (settingsCount > 4096000) {
                return ValidationResult.error("Settings exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < settingsCount; ++i) {
                ++pos;
                ++pos;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int rulesOffset = buffer.getIntLE(offset + 28);
            if (rulesOffset < 0) {
                return ValidationResult.error("Invalid offset for Rules");
            }
            int pos = offset + 44 + rulesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Rules");
            }
            final ValidationResult rulesResult = InteractionRules.validateStructure(buffer, pos);
            if (!rulesResult.isValid()) {
                return ValidationResult.error("Invalid Rules: " + rulesResult.error());
            }
            pos += InteractionRules.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int tagsOffset = buffer.getIntLE(offset + 32);
            if (tagsOffset < 0) {
                return ValidationResult.error("Invalid offset for Tags");
            }
            int pos = offset + 44 + tagsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Tags");
            }
            final int tagsCount = VarInt.peek(buffer, pos);
            if (tagsCount < 0) {
                return ValidationResult.error("Invalid array count for Tags");
            }
            if (tagsCount > 4096000) {
                return ValidationResult.error("Tags exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += tagsCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Tags");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int cameraOffset = buffer.getIntLE(offset + 36);
            if (cameraOffset < 0) {
                return ValidationResult.error("Invalid offset for Camera");
            }
            int pos = offset + 44 + cameraOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Camera");
            }
            final ValidationResult cameraResult = InteractionCameraSettings.validateStructure(buffer, pos);
            if (!cameraResult.isValid()) {
                return ValidationResult.error("Invalid Camera: " + cameraResult.error());
            }
            pos += InteractionCameraSettings.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int matchersOffset = buffer.getIntLE(offset + 40);
            if (matchersOffset < 0) {
                return ValidationResult.error("Invalid offset for Matchers");
            }
            int pos = offset + 44 + matchersOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Matchers");
            }
            final int matchersCount = VarInt.peek(buffer, pos);
            if (matchersCount < 0) {
                return ValidationResult.error("Invalid array count for Matchers");
            }
            if (matchersCount > 4096000) {
                return ValidationResult.error("Matchers exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < matchersCount; ++i) {
                final ValidationResult structResult = BlockMatcher.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid BlockMatcher in Matchers[" + i + "]: " + structResult.error());
                }
                pos += BlockMatcher.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    @Override
    public BlockConditionInteraction clone() {
        final BlockConditionInteraction copy = new BlockConditionInteraction();
        copy.waitForDataFrom = this.waitForDataFrom;
        copy.effects = ((this.effects != null) ? this.effects.clone() : null);
        copy.horizontalSpeedMultiplier = this.horizontalSpeedMultiplier;
        copy.runTime = this.runTime;
        copy.cancelOnItemChange = this.cancelOnItemChange;
        Map.Entry<GameMode, InteractionSettings> e = null;
        if (this.settings != null) {
            final Map<GameMode, InteractionSettings> m = new HashMap<GameMode, InteractionSettings>();
            final Iterator<Map.Entry<GameMode, InteractionSettings>> iterator = this.settings.entrySet().iterator();
            while (iterator.hasNext()) {
                e = iterator.next();
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.settings = m;
        }
        copy.rules = ((this.rules != null) ? this.rules.clone() : null);
        copy.tags = (int[])((this.tags != null) ? Arrays.copyOf(this.tags, this.tags.length) : null);
        copy.camera = ((this.camera != null) ? this.camera.clone() : null);
        copy.next = this.next;
        copy.failed = this.failed;
        copy.useLatestTarget = this.useLatestTarget;
        copy.matchers = (BlockMatcher[])((this.matchers != null) ? ((BlockMatcher[])Arrays.stream(this.matchers).map(e -> e.clone()).toArray(BlockMatcher[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockConditionInteraction other) {
            return Objects.equals(this.waitForDataFrom, other.waitForDataFrom) && Objects.equals(this.effects, other.effects) && this.horizontalSpeedMultiplier == other.horizontalSpeedMultiplier && this.runTime == other.runTime && this.cancelOnItemChange == other.cancelOnItemChange && Objects.equals(this.settings, other.settings) && Objects.equals(this.rules, other.rules) && Arrays.equals(this.tags, other.tags) && Objects.equals(this.camera, other.camera) && this.next == other.next && this.failed == other.failed && this.useLatestTarget == other.useLatestTarget && Arrays.equals(this.matchers, other.matchers);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.waitForDataFrom);
        result = 31 * result + Objects.hashCode(this.effects);
        result = 31 * result + Float.hashCode(this.horizontalSpeedMultiplier);
        result = 31 * result + Float.hashCode(this.runTime);
        result = 31 * result + Boolean.hashCode(this.cancelOnItemChange);
        result = 31 * result + Objects.hashCode(this.settings);
        result = 31 * result + Objects.hashCode(this.rules);
        result = 31 * result + Arrays.hashCode(this.tags);
        result = 31 * result + Objects.hashCode(this.camera);
        result = 31 * result + Integer.hashCode(this.next);
        result = 31 * result + Integer.hashCode(this.failed);
        result = 31 * result + Boolean.hashCode(this.useLatestTarget);
        result = 31 * result + Arrays.hashCode(this.matchers);
        return result;
    }
}
