// 
// 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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

public class MemoriesConditionInteraction extends Interaction
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 15;
    public static final int VARIABLE_FIELD_COUNT = 6;
    public static final int VARIABLE_BLOCK_START = 39;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public Map<Integer, Integer> memoriesNext;
    public int failed;
    
    public MemoriesConditionInteraction() {
        this.failed = Integer.MIN_VALUE;
    }
    
    public MemoriesConditionInteraction(@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, @Nullable final Map<Integer, Integer> memoriesNext, final int failed) {
        this.failed = Integer.MIN_VALUE;
        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.memoriesNext = memoriesNext;
        this.failed = failed;
    }
    
    public MemoriesConditionInteraction(@Nonnull final MemoriesConditionInteraction other) {
        this.failed = Integer.MIN_VALUE;
        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.memoriesNext = other.memoriesNext;
        this.failed = other.failed;
    }
    
    @Nonnull
    public static MemoriesConditionInteraction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final MemoriesConditionInteraction obj = new MemoriesConditionInteraction();
        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.failed = buf.getIntLE(offset + 11);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 39 + buf.getIntLE(offset + 15);
            obj.effects = InteractionEffects.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 39 + buf.getIntLE(offset + 19);
            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 + 39 + buf.getIntLE(offset + 23);
            obj.rules = InteractionRules.deserialize(buf, varPos3);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 39 + buf.getIntLE(offset + 27);
            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 + 39 + buf.getIntLE(offset + 31);
            obj.camera = InteractionCameraSettings.deserialize(buf, varPos5);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int varPos6 = offset + 39 + buf.getIntLE(offset + 35);
            final int memoriesNextCount = VarInt.peek(buf, varPos6);
            if (memoriesNextCount < 0) {
                throw ProtocolException.negativeLength("MemoriesNext", memoriesNextCount);
            }
            if (memoriesNextCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("MemoriesNext", memoriesNextCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos6);
            obj.memoriesNext = new HashMap<Integer, Integer>(memoriesNextCount);
            int dictPos = varPos6 + varIntLen;
            for (int i = 0; i < memoriesNextCount; ++i) {
                final int key2 = buf.getIntLE(dictPos);
                dictPos += 4;
                final int val2 = buf.getIntLE(dictPos);
                dictPos += 4;
                if (obj.memoriesNext.put(key2, val2) != null) {
                    throw ProtocolException.duplicateKey("memoriesNext", key2);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 39;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 15);
            int pos0 = offset + 39 + fieldOffset0;
            pos0 += InteractionEffects.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 19);
            int pos2 = offset + 39 + 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 + 23);
            int pos3 = offset + 39 + fieldOffset3;
            pos3 += InteractionRules.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 27);
            int pos4 = offset + 39 + 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 + 31);
            int pos5 = offset + 39 + fieldOffset5;
            pos5 += InteractionCameraSettings.computeBytesConsumed(buf, pos5);
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset6 = buf.getIntLE(offset + 35);
            int pos6 = offset + 39 + fieldOffset6;
            final int dictLen = VarInt.peek(buf, pos6);
            pos6 += VarInt.length(buf, pos6);
            for (int i = 0; i < dictLen; ++i) {
                pos6 += 4;
                pos6 += 4;
            }
            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.memoriesNext != 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.failed);
        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 memoriesNextOffsetSlot = 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.memoriesNext != null) {
            buf.setIntLE(memoriesNextOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.memoriesNext.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("MemoriesNext", this.memoriesNext.size(), 4096000);
            }
            VarInt.write(buf, this.memoriesNext.size());
            for (final Map.Entry<Integer, Integer> e2 : this.memoriesNext.entrySet()) {
                buf.writeIntLE(e2.getKey());
                buf.writeIntLE(e2.getValue());
            }
        }
        else {
            buf.setIntLE(memoriesNextOffsetSlot, -1);
        }
        return buf.writerIndex() - startPos;
    }
    
    @Override
    public int computeSize() {
        int size = 39;
        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.memoriesNext != null) {
            size += VarInt.size(this.memoriesNext.size()) + this.memoriesNext.size() * 8;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 39) {
            return ValidationResult.error("Buffer too small: expected at least 39 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int effectsOffset = buffer.getIntLE(offset + 15);
            if (effectsOffset < 0) {
                return ValidationResult.error("Invalid offset for Effects");
            }
            int pos = offset + 39 + 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 + 19);
            if (settingsOffset < 0) {
                return ValidationResult.error("Invalid offset for Settings");
            }
            int pos = offset + 39 + 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 + 23);
            if (rulesOffset < 0) {
                return ValidationResult.error("Invalid offset for Rules");
            }
            int pos = offset + 39 + 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 + 27);
            if (tagsOffset < 0) {
                return ValidationResult.error("Invalid offset for Tags");
            }
            int pos = offset + 39 + 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 + 31);
            if (cameraOffset < 0) {
                return ValidationResult.error("Invalid offset for Camera");
            }
            int pos = offset + 39 + 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 memoriesNextOffset = buffer.getIntLE(offset + 35);
            if (memoriesNextOffset < 0) {
                return ValidationResult.error("Invalid offset for MemoriesNext");
            }
            int pos = offset + 39 + memoriesNextOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for MemoriesNext");
            }
            final int memoriesNextCount = VarInt.peek(buffer, pos);
            if (memoriesNextCount < 0) {
                return ValidationResult.error("Invalid dictionary count for MemoriesNext");
            }
            if (memoriesNextCount > 4096000) {
                return ValidationResult.error("MemoriesNext exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < memoriesNextCount; ++i) {
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading value");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public MemoriesConditionInteraction clone() {
        final MemoriesConditionInteraction copy = new MemoriesConditionInteraction();
        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.memoriesNext = ((this.memoriesNext != null) ? new HashMap<Integer, Integer>(this.memoriesNext) : null);
        copy.failed = this.failed;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final MemoriesConditionInteraction 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) && Objects.equals(this.memoriesNext, other.memoriesNext) && this.failed == other.failed;
        }
        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 + Objects.hashCode(this.memoriesNext);
        result = 31 * result + Integer.hashCode(this.failed);
        return result;
    }
}
