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

public class RootInteraction
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 6;
    public static final int VARIABLE_FIELD_COUNT = 6;
    public static final int VARIABLE_BLOCK_START = 30;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public int[] interactions;
    @Nullable
    public InteractionCooldown cooldown;
    @Nullable
    public Map<GameMode, RootInteractionSettings> settings;
    @Nullable
    public InteractionRules rules;
    @Nullable
    public int[] tags;
    public float clickQueuingTimeout;
    public boolean requireNewClick;
    
    public RootInteraction() {
    }
    
    public RootInteraction(@Nullable final String id, @Nullable final int[] interactions, @Nullable final InteractionCooldown cooldown, @Nullable final Map<GameMode, RootInteractionSettings> settings, @Nullable final InteractionRules rules, @Nullable final int[] tags, final float clickQueuingTimeout, final boolean requireNewClick) {
        this.id = id;
        this.interactions = interactions;
        this.cooldown = cooldown;
        this.settings = settings;
        this.rules = rules;
        this.tags = tags;
        this.clickQueuingTimeout = clickQueuingTimeout;
        this.requireNewClick = requireNewClick;
    }
    
    public RootInteraction(@Nonnull final RootInteraction other) {
        this.id = other.id;
        this.interactions = other.interactions;
        this.cooldown = other.cooldown;
        this.settings = other.settings;
        this.rules = other.rules;
        this.tags = other.tags;
        this.clickQueuingTimeout = other.clickQueuingTimeout;
        this.requireNewClick = other.requireNewClick;
    }
    
    @Nonnull
    public static RootInteraction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final RootInteraction obj = new RootInteraction();
        final byte nullBits = buf.getByte(offset);
        obj.clickQueuingTimeout = buf.getFloatLE(offset + 1);
        obj.requireNewClick = (buf.getByte(offset + 5) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 30 + buf.getIntLE(offset + 6);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 30 + buf.getIntLE(offset + 10);
            final int interactionsCount = VarInt.peek(buf, varPos2);
            if (interactionsCount < 0) {
                throw ProtocolException.negativeLength("Interactions", interactionsCount);
            }
            if (interactionsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Interactions", interactionsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + interactionsCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Interactions", varPos2 + varIntLen + interactionsCount * 4, buf.readableBytes());
            }
            obj.interactions = new int[interactionsCount];
            for (int i = 0; i < interactionsCount; ++i) {
                obj.interactions[i] = buf.getIntLE(varPos2 + varIntLen + i * 4);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 30 + buf.getIntLE(offset + 14);
            obj.cooldown = InteractionCooldown.deserialize(buf, varPos3);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 30 + buf.getIntLE(offset + 18);
            final int settingsCount = VarInt.peek(buf, varPos4);
            if (settingsCount < 0) {
                throw ProtocolException.negativeLength("Settings", settingsCount);
            }
            if (settingsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Settings", settingsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            obj.settings = new HashMap<GameMode, RootInteractionSettings>(settingsCount);
            int dictPos = varPos4 + varIntLen;
            for (int j = 0; j < settingsCount; ++j) {
                final GameMode key = GameMode.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final RootInteractionSettings val = RootInteractionSettings.deserialize(buf, dictPos);
                dictPos += RootInteractionSettings.computeBytesConsumed(buf, dictPos);
                if (obj.settings.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("settings", key);
                }
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos5 = offset + 30 + buf.getIntLE(offset + 22);
            obj.rules = InteractionRules.deserialize(buf, varPos5);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int varPos6 = offset + 30 + buf.getIntLE(offset + 26);
            final int tagsCount = VarInt.peek(buf, varPos6);
            if (tagsCount < 0) {
                throw ProtocolException.negativeLength("Tags", tagsCount);
            }
            if (tagsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Tags", tagsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos6);
            if (varPos6 + varIntLen + tagsCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Tags", varPos6 + varIntLen + tagsCount * 4, buf.readableBytes());
            }
            obj.tags = new int[tagsCount];
            for (int i = 0; i < tagsCount; ++i) {
                obj.tags[i] = buf.getIntLE(varPos6 + varIntLen + i * 4);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 30;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 6);
            int pos0 = offset + 30 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 10);
            int pos2 = offset + 30 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + arrLen * 4;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 14);
            int pos3 = offset + 30 + fieldOffset3;
            pos3 += InteractionCooldown.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 18);
            int pos4 = offset + 30 + fieldOffset4;
            final int dictLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4);
            for (int i = 0; i < dictLen; ++i) {
                pos4 = ++pos4 + RootInteractionSettings.computeBytesConsumed(buf, pos4);
            }
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 22);
            int pos5 = offset + 30 + fieldOffset5;
            pos5 += InteractionRules.computeBytesConsumed(buf, pos5);
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset6 = buf.getIntLE(offset + 26);
            int pos6 = offset + 30 + fieldOffset6;
            final int arrLen = VarInt.peek(buf, pos6);
            pos6 += VarInt.length(buf, pos6) + arrLen * 4;
            if (pos6 - offset > maxEnd) {
                maxEnd = pos6 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.id != null) {
            nullBits |= 0x1;
        }
        if (this.interactions != null) {
            nullBits |= 0x2;
        }
        if (this.cooldown != null) {
            nullBits |= 0x4;
        }
        if (this.settings != null) {
            nullBits |= 0x8;
        }
        if (this.rules != null) {
            nullBits |= 0x10;
        }
        if (this.tags != null) {
            nullBits |= 0x20;
        }
        buf.writeByte(nullBits);
        buf.writeFloatLE(this.clickQueuingTimeout);
        buf.writeByte(this.requireNewClick ? 1 : 0);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int interactionsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int cooldownOffsetSlot = 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 varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.interactions != null) {
            buf.setIntLE(interactionsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.interactions.length > 4096000) {
                throw ProtocolException.arrayTooLong("Interactions", this.interactions.length, 4096000);
            }
            VarInt.write(buf, this.interactions.length);
            for (final int item : this.interactions) {
                buf.writeIntLE(item);
            }
        }
        else {
            buf.setIntLE(interactionsOffsetSlot, -1);
        }
        if (this.cooldown != null) {
            buf.setIntLE(cooldownOffsetSlot, buf.writerIndex() - varBlockStart);
            this.cooldown.serialize(buf);
        }
        else {
            buf.setIntLE(cooldownOffsetSlot, -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, RootInteractionSettings> 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);
        }
    }
    
    public int computeSize() {
        int size = 30;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.interactions != null) {
            size += VarInt.size(this.interactions.length) + this.interactions.length * 4;
        }
        if (this.cooldown != null) {
            size += this.cooldown.computeSize();
        }
        if (this.settings != null) {
            int settingsSize = 0;
            for (final Map.Entry<GameMode, RootInteractionSettings> kvp : this.settings.entrySet()) {
                settingsSize += 1 + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.settings.size()) + settingsSize;
        }
        if (this.rules != null) {
            size += this.rules.computeSize();
        }
        if (this.tags != null) {
            size += VarInt.size(this.tags.length) + this.tags.length * 4;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 30) {
            return ValidationResult.error("Buffer too small: expected at least 30 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 6);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 30 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int interactionsOffset = buffer.getIntLE(offset + 10);
            if (interactionsOffset < 0) {
                return ValidationResult.error("Invalid offset for Interactions");
            }
            int pos = offset + 30 + interactionsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Interactions");
            }
            final int interactionsCount = VarInt.peek(buffer, pos);
            if (interactionsCount < 0) {
                return ValidationResult.error("Invalid array count for Interactions");
            }
            if (interactionsCount > 4096000) {
                return ValidationResult.error("Interactions exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += interactionsCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Interactions");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int cooldownOffset = buffer.getIntLE(offset + 14);
            if (cooldownOffset < 0) {
                return ValidationResult.error("Invalid offset for Cooldown");
            }
            int pos = offset + 30 + cooldownOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Cooldown");
            }
            final ValidationResult cooldownResult = InteractionCooldown.validateStructure(buffer, pos);
            if (!cooldownResult.isValid()) {
                return ValidationResult.error("Invalid Cooldown: " + cooldownResult.error());
            }
            pos += InteractionCooldown.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int settingsOffset = buffer.getIntLE(offset + 18);
            if (settingsOffset < 0) {
                return ValidationResult.error("Invalid offset for Settings");
            }
            int pos = offset + 30 + 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 + RootInteractionSettings.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int rulesOffset = buffer.getIntLE(offset + 22);
            if (rulesOffset < 0) {
                return ValidationResult.error("Invalid offset for Rules");
            }
            int pos = offset + 30 + 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 & 0x20) != 0x0) {
            final int tagsOffset = buffer.getIntLE(offset + 26);
            if (tagsOffset < 0) {
                return ValidationResult.error("Invalid offset for Tags");
            }
            int pos = offset + 30 + 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");
            }
        }
        return ValidationResult.OK;
    }
    
    public RootInteraction clone() {
        final RootInteraction copy = new RootInteraction();
        copy.id = this.id;
        copy.interactions = (int[])((this.interactions != null) ? Arrays.copyOf(this.interactions, this.interactions.length) : null);
        copy.cooldown = ((this.cooldown != null) ? this.cooldown.clone() : null);
        if (this.settings != null) {
            final Map<GameMode, RootInteractionSettings> m = new HashMap<GameMode, RootInteractionSettings>();
            for (final Map.Entry<GameMode, RootInteractionSettings> 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.clickQueuingTimeout = this.clickQueuingTimeout;
        copy.requireNewClick = this.requireNewClick;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final RootInteraction other) {
            return Objects.equals(this.id, other.id) && Arrays.equals(this.interactions, other.interactions) && Objects.equals(this.cooldown, other.cooldown) && Objects.equals(this.settings, other.settings) && Objects.equals(this.rules, other.rules) && Arrays.equals(this.tags, other.tags) && this.clickQueuingTimeout == other.clickQueuingTimeout && this.requireNewClick == other.requireNewClick;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Arrays.hashCode(this.interactions);
        result = 31 * result + Objects.hashCode(this.cooldown);
        result = 31 * result + Objects.hashCode(this.settings);
        result = 31 * result + Objects.hashCode(this.rules);
        result = 31 * result + Arrays.hashCode(this.tags);
        result = 31 * result + Float.hashCode(this.clickQueuingTimeout);
        result = 31 * result + Boolean.hashCode(this.requireNewClick);
        return result;
    }
}
