// 
// 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 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 javax.annotation.Nullable;

public class AnimationSet
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 17;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public Animation[] animations;
    @Nullable
    public Rangef nextAnimationDelay;
    
    public AnimationSet() {
    }
    
    public AnimationSet(@Nullable final String id, @Nullable final Animation[] animations, @Nullable final Rangef nextAnimationDelay) {
        this.id = id;
        this.animations = animations;
        this.nextAnimationDelay = nextAnimationDelay;
    }
    
    public AnimationSet(@Nonnull final AnimationSet other) {
        this.id = other.id;
        this.animations = other.animations;
        this.nextAnimationDelay = other.nextAnimationDelay;
    }
    
    @Nonnull
    public static AnimationSet deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AnimationSet obj = new AnimationSet();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.nextAnimationDelay = Rangef.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 17 + buf.getIntLE(offset + 9);
            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 & 0x4) != 0x0) {
            final int varPos2 = offset + 17 + buf.getIntLE(offset + 13);
            final int animationsCount = VarInt.peek(buf, varPos2);
            if (animationsCount < 0) {
                throw ProtocolException.negativeLength("Animations", animationsCount);
            }
            if (animationsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Animations", animationsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + animationsCount * 22L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Animations", varPos2 + varIntLen + animationsCount * 22, buf.readableBytes());
            }
            obj.animations = new Animation[animationsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < animationsCount; ++i) {
                obj.animations[i] = Animation.deserialize(buf, elemPos);
                elemPos += Animation.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 17;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 9);
            int pos0 = offset + 17 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 13);
            int pos2 = offset + 17 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += Animation.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.nextAnimationDelay != null) {
            nullBits |= 0x1;
        }
        if (this.id != null) {
            nullBits |= 0x2;
        }
        if (this.animations != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        if (this.nextAnimationDelay != null) {
            this.nextAnimationDelay.serialize(buf);
        }
        else {
            buf.writeZero(8);
        }
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int animationsOffsetSlot = 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.animations != null) {
            buf.setIntLE(animationsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.animations.length > 4096000) {
                throw ProtocolException.arrayTooLong("Animations", this.animations.length, 4096000);
            }
            VarInt.write(buf, this.animations.length);
            for (final Animation item : this.animations) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(animationsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 17;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.animations != null) {
            int animationsSize = 0;
            for (final Animation elem : this.animations) {
                animationsSize += elem.computeSize();
            }
            size += VarInt.size(this.animations.length) + animationsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 17) {
            return ValidationResult.error("Buffer too small: expected at least 17 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x2) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 9);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 17 + 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 & 0x4) != 0x0) {
            final int animationsOffset = buffer.getIntLE(offset + 13);
            if (animationsOffset < 0) {
                return ValidationResult.error("Invalid offset for Animations");
            }
            int pos = offset + 17 + animationsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Animations");
            }
            final int animationsCount = VarInt.peek(buffer, pos);
            if (animationsCount < 0) {
                return ValidationResult.error("Invalid array count for Animations");
            }
            if (animationsCount > 4096000) {
                return ValidationResult.error("Animations exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < animationsCount; ++i) {
                final ValidationResult structResult = Animation.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid Animation in Animations[" + i + "]: " + structResult.error());
                }
                pos += Animation.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AnimationSet clone() {
        final AnimationSet copy = new AnimationSet();
        copy.id = this.id;
        copy.animations = (Animation[])((this.animations != null) ? ((Animation[])Arrays.stream(this.animations).map(e -> e.clone()).toArray(Animation[]::new)) : null);
        copy.nextAnimationDelay = ((this.nextAnimationDelay != null) ? this.nextAnimationDelay.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AnimationSet other) {
            return Objects.equals(this.id, other.id) && Arrays.equals(this.animations, other.animations) && Objects.equals(this.nextAnimationDelay, other.nextAnimationDelay);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Arrays.hashCode(this.animations);
        result = 31 * result + Objects.hashCode(this.nextAnimationDelay);
        return result;
    }
}
