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

public class ConditionInteraction extends SimpleInteraction
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 2;
    public static final int FIXED_BLOCK_SIZE = 26;
    public static final int VARIABLE_FIELD_COUNT = 5;
    public static final int VARIABLE_BLOCK_START = 46;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public GameMode requiredGameMode;
    @Nullable
    public Boolean jumping;
    @Nullable
    public Boolean swimming;
    @Nullable
    public Boolean crouching;
    @Nullable
    public Boolean running;
    @Nullable
    public Boolean flying;
    
    public ConditionInteraction() {
    }
    
    public ConditionInteraction(@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, @Nullable final GameMode requiredGameMode, @Nullable final Boolean jumping, @Nullable final Boolean swimming, @Nullable final Boolean crouching, @Nullable final Boolean running, @Nullable final Boolean flying) {
        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.requiredGameMode = requiredGameMode;
        this.jumping = jumping;
        this.swimming = swimming;
        this.crouching = crouching;
        this.running = running;
        this.flying = flying;
    }
    
    public ConditionInteraction(@Nonnull final ConditionInteraction 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.requiredGameMode = other.requiredGameMode;
        this.jumping = other.jumping;
        this.swimming = other.swimming;
        this.crouching = other.crouching;
        this.running = other.running;
        this.flying = other.flying;
    }
    
    @Nonnull
    public static ConditionInteraction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ConditionInteraction obj = new ConditionInteraction();
        final byte[] nullBits = PacketIO.readBytes(buf, offset, 2);
        obj.waitForDataFrom = WaitForDataFrom.fromValue(buf.getByte(offset + 2));
        obj.horizontalSpeedMultiplier = buf.getFloatLE(offset + 3);
        obj.runTime = buf.getFloatLE(offset + 7);
        obj.cancelOnItemChange = (buf.getByte(offset + 11) != 0);
        obj.next = buf.getIntLE(offset + 12);
        obj.failed = buf.getIntLE(offset + 16);
        if ((nullBits[0] & 0x1) != 0x0) {
            obj.requiredGameMode = GameMode.fromValue(buf.getByte(offset + 20));
        }
        if ((nullBits[0] & 0x2) != 0x0) {
            obj.jumping = (buf.getByte(offset + 21) != 0);
        }
        if ((nullBits[0] & 0x4) != 0x0) {
            obj.swimming = (buf.getByte(offset + 22) != 0);
        }
        if ((nullBits[0] & 0x8) != 0x0) {
            obj.crouching = (buf.getByte(offset + 23) != 0);
        }
        if ((nullBits[0] & 0x10) != 0x0) {
            obj.running = (buf.getByte(offset + 24) != 0);
        }
        if ((nullBits[0] & 0x20) != 0x0) {
            obj.flying = (buf.getByte(offset + 25) != 0);
        }
        if ((nullBits[0] & 0x40) != 0x0) {
            final int varPos0 = offset + 46 + buf.getIntLE(offset + 26);
            obj.effects = InteractionEffects.deserialize(buf, varPos0);
        }
        if ((nullBits[0] & 0x80) != 0x0) {
            final int varPos2 = offset + 46 + buf.getIntLE(offset + 30);
            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[1] & 0x1) != 0x0) {
            final int varPos3 = offset + 46 + buf.getIntLE(offset + 34);
            obj.rules = InteractionRules.deserialize(buf, varPos3);
        }
        if ((nullBits[1] & 0x2) != 0x0) {
            final int varPos4 = offset + 46 + buf.getIntLE(offset + 38);
            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[1] & 0x4) != 0x0) {
            final int varPos5 = offset + 46 + buf.getIntLE(offset + 42);
            obj.camera = InteractionCameraSettings.deserialize(buf, varPos5);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte[] nullBits = PacketIO.readBytes(buf, offset, 2);
        int maxEnd = 46;
        if ((nullBits[0] & 0x40) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 26);
            int pos0 = offset + 46 + fieldOffset0;
            pos0 += InteractionEffects.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits[0] & 0x80) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 30);
            int pos2 = offset + 46 + 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[1] & 0x1) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 34);
            int pos3 = offset + 46 + fieldOffset3;
            pos3 += InteractionRules.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits[1] & 0x2) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 38);
            int pos4 = offset + 46 + fieldOffset4;
            final int arrLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4) + arrLen * 4;
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits[1] & 0x4) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 42);
            int pos5 = offset + 46 + fieldOffset5;
            pos5 += InteractionCameraSettings.computeBytesConsumed(buf, pos5);
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public int serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        final byte[] nullBits = new byte[2];
        if (this.requiredGameMode != null) {
            final byte[] array = nullBits;
            final int n = 0;
            array[n] |= 0x1;
        }
        if (this.jumping != null) {
            final byte[] array2 = nullBits;
            final int n2 = 0;
            array2[n2] |= 0x2;
        }
        if (this.swimming != null) {
            final byte[] array3 = nullBits;
            final int n3 = 0;
            array3[n3] |= 0x4;
        }
        if (this.crouching != null) {
            final byte[] array4 = nullBits;
            final int n4 = 0;
            array4[n4] |= 0x8;
        }
        if (this.running != null) {
            final byte[] array5 = nullBits;
            final int n5 = 0;
            array5[n5] |= 0x10;
        }
        if (this.flying != null) {
            final byte[] array6 = nullBits;
            final int n6 = 0;
            array6[n6] |= 0x20;
        }
        if (this.effects != null) {
            final byte[] array7 = nullBits;
            final int n7 = 0;
            array7[n7] |= 0x40;
        }
        if (this.settings != null) {
            final byte[] array8 = nullBits;
            final int n8 = 0;
            array8[n8] |= (byte)128;
        }
        if (this.rules != null) {
            final byte[] array9 = nullBits;
            final int n9 = 1;
            array9[n9] |= 0x1;
        }
        if (this.tags != null) {
            final byte[] array10 = nullBits;
            final int n10 = 1;
            array10[n10] |= 0x2;
        }
        if (this.camera != null) {
            final byte[] array11 = nullBits;
            final int n11 = 1;
            array11[n11] |= 0x4;
        }
        buf.writeBytes(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);
        if (this.requiredGameMode != null) {
            buf.writeByte(this.requiredGameMode.getValue());
        }
        else {
            buf.writeZero(1);
        }
        if (this.jumping != null) {
            buf.writeByte(((boolean)this.jumping) ? 1 : 0);
        }
        else {
            buf.writeZero(1);
        }
        if (this.swimming != null) {
            buf.writeByte(((boolean)this.swimming) ? 1 : 0);
        }
        else {
            buf.writeZero(1);
        }
        if (this.crouching != null) {
            buf.writeByte(((boolean)this.crouching) ? 1 : 0);
        }
        else {
            buf.writeZero(1);
        }
        if (this.running != null) {
            buf.writeByte(((boolean)this.running) ? 1 : 0);
        }
        else {
            buf.writeZero(1);
        }
        if (this.flying != null) {
            buf.writeByte(((boolean)this.flying) ? 1 : 0);
        }
        else {
            buf.writeZero(1);
        }
        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 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);
        }
        return buf.writerIndex() - startPos;
    }
    
    @Override
    public int computeSize() {
        int size = 46;
        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();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 46) {
            return ValidationResult.error("Buffer too small: expected at least 46 bytes");
        }
        final byte[] nullBits = PacketIO.readBytes(buffer, offset, 2);
        if ((nullBits[0] & 0x40) != 0x0) {
            final int effectsOffset = buffer.getIntLE(offset + 26);
            if (effectsOffset < 0) {
                return ValidationResult.error("Invalid offset for Effects");
            }
            int pos = offset + 46 + 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[0] & 0x80) != 0x0) {
            final int settingsOffset = buffer.getIntLE(offset + 30);
            if (settingsOffset < 0) {
                return ValidationResult.error("Invalid offset for Settings");
            }
            int pos = offset + 46 + 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[1] & 0x1) != 0x0) {
            final int rulesOffset = buffer.getIntLE(offset + 34);
            if (rulesOffset < 0) {
                return ValidationResult.error("Invalid offset for Rules");
            }
            int pos = offset + 46 + 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[1] & 0x2) != 0x0) {
            final int tagsOffset = buffer.getIntLE(offset + 38);
            if (tagsOffset < 0) {
                return ValidationResult.error("Invalid offset for Tags");
            }
            int pos = offset + 46 + 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[1] & 0x4) != 0x0) {
            final int cameraOffset = buffer.getIntLE(offset + 42);
            if (cameraOffset < 0) {
                return ValidationResult.error("Invalid offset for Camera");
            }
            int pos = offset + 46 + 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);
        }
        return ValidationResult.OK;
    }
    
    @Override
    public ConditionInteraction clone() {
        final ConditionInteraction copy = new ConditionInteraction();
        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;
        if (this.settings != null) {
            final Map<GameMode, InteractionSettings> m = new HashMap<GameMode, InteractionSettings>();
            for (final Map.Entry<GameMode, InteractionSettings> e : this.settings.entrySet()) {
                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.requiredGameMode = this.requiredGameMode;
        copy.jumping = this.jumping;
        copy.swimming = this.swimming;
        copy.crouching = this.crouching;
        copy.running = this.running;
        copy.flying = this.flying;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ConditionInteraction 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 && Objects.equals(this.requiredGameMode, other.requiredGameMode) && Objects.equals(this.jumping, other.jumping) && Objects.equals(this.swimming, other.swimming) && Objects.equals(this.crouching, other.crouching) && Objects.equals(this.running, other.running) && Objects.equals(this.flying, other.flying);
        }
        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 + Objects.hashCode(this.requiredGameMode);
        result = 31 * result + Objects.hashCode(this.jumping);
        result = 31 * result + Objects.hashCode(this.swimming);
        result = 31 * result + Objects.hashCode(this.crouching);
        result = 31 * result + Objects.hashCode(this.running);
        result = 31 * result + Objects.hashCode(this.flying);
        return result;
    }
}
