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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class RootInteractionSettings
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 2;
    public static final int MAX_SIZE = 32768028;
    public boolean allowSkipChainOnClick;
    @Nullable
    public InteractionCooldown cooldown;
    
    public RootInteractionSettings() {
    }
    
    public RootInteractionSettings(final boolean allowSkipChainOnClick, @Nullable final InteractionCooldown cooldown) {
        this.allowSkipChainOnClick = allowSkipChainOnClick;
        this.cooldown = cooldown;
    }
    
    public RootInteractionSettings(@Nonnull final RootInteractionSettings other) {
        this.allowSkipChainOnClick = other.allowSkipChainOnClick;
        this.cooldown = other.cooldown;
    }
    
    @Nonnull
    public static RootInteractionSettings deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final RootInteractionSettings obj = new RootInteractionSettings();
        final byte nullBits = buf.getByte(offset);
        obj.allowSkipChainOnClick = (buf.getByte(offset + 1) != 0);
        int pos = offset + 2;
        if ((nullBits & 0x1) != 0x0) {
            obj.cooldown = InteractionCooldown.deserialize(buf, pos);
            pos += InteractionCooldown.computeBytesConsumed(buf, pos);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 2;
        if ((nullBits & 0x1) != 0x0) {
            pos += InteractionCooldown.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.cooldown != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.allowSkipChainOnClick ? 1 : 0);
        if (this.cooldown != null) {
            this.cooldown.serialize(buf);
        }
    }
    
    public int computeSize() {
        int size = 2;
        if (this.cooldown != null) {
            size += this.cooldown.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 2) {
            return ValidationResult.error("Buffer too small: expected at least 2 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 2;
        if ((nullBits & 0x1) != 0x0) {
            final ValidationResult cooldownResult = InteractionCooldown.validateStructure(buffer, pos);
            if (!cooldownResult.isValid()) {
                return ValidationResult.error("Invalid Cooldown: " + cooldownResult.error());
            }
            pos += InteractionCooldown.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public RootInteractionSettings clone() {
        final RootInteractionSettings copy = new RootInteractionSettings();
        copy.allowSkipChainOnClick = this.allowSkipChainOnClick;
        copy.cooldown = ((this.cooldown != null) ? this.cooldown.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final RootInteractionSettings other) {
            return this.allowSkipChainOnClick == other.allowSkipChainOnClick && Objects.equals(this.cooldown, other.cooldown);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.allowSkipChainOnClick, this.cooldown);
    }
}
