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

package com.hypixel.hytale.protocol;

import java.util.Objects;
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 ItemPlayerAnimations
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 91;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 103;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public Map<String, ItemAnimation> animations;
    @Nullable
    public WiggleWeights wiggleWeights;
    @Nullable
    public CameraSettings camera;
    @Nullable
    public ItemPullbackConfiguration pullbackConfig;
    public boolean useFirstPersonOverride;
    
    public ItemPlayerAnimations() {
    }
    
    public ItemPlayerAnimations(@Nullable final String id, @Nullable final Map<String, ItemAnimation> animations, @Nullable final WiggleWeights wiggleWeights, @Nullable final CameraSettings camera, @Nullable final ItemPullbackConfiguration pullbackConfig, final boolean useFirstPersonOverride) {
        this.id = id;
        this.animations = animations;
        this.wiggleWeights = wiggleWeights;
        this.camera = camera;
        this.pullbackConfig = pullbackConfig;
        this.useFirstPersonOverride = useFirstPersonOverride;
    }
    
    public ItemPlayerAnimations(@Nonnull final ItemPlayerAnimations other) {
        this.id = other.id;
        this.animations = other.animations;
        this.wiggleWeights = other.wiggleWeights;
        this.camera = other.camera;
        this.pullbackConfig = other.pullbackConfig;
        this.useFirstPersonOverride = other.useFirstPersonOverride;
    }
    
    @Nonnull
    public static ItemPlayerAnimations deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemPlayerAnimations obj = new ItemPlayerAnimations();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.wiggleWeights = WiggleWeights.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.pullbackConfig = ItemPullbackConfiguration.deserialize(buf, offset + 41);
        }
        obj.useFirstPersonOverride = (buf.getByte(offset + 90) != 0);
        if ((nullBits & 0x4) != 0x0) {
            final int varPos0 = offset + 103 + buf.getIntLE(offset + 91);
            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 & 0x8) != 0x0) {
            final int varPos2 = offset + 103 + buf.getIntLE(offset + 95);
            final int animationsCount = VarInt.peek(buf, varPos2);
            if (animationsCount < 0) {
                throw ProtocolException.negativeLength("Animations", animationsCount);
            }
            if (animationsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Animations", animationsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.animations = new HashMap<String, ItemAnimation>(animationsCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < animationsCount; ++i) {
                final int keyLen = VarInt.peek(buf, dictPos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, dictPos);
                final String key = PacketIO.readVarString(buf, dictPos);
                dictPos += keyVarLen + keyLen;
                final ItemAnimation val = ItemAnimation.deserialize(buf, dictPos);
                dictPos += ItemAnimation.computeBytesConsumed(buf, dictPos);
                if (obj.animations.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("animations", key);
                }
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos3 = offset + 103 + buf.getIntLE(offset + 99);
            obj.camera = CameraSettings.deserialize(buf, varPos3);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 103;
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 91);
            int pos0 = offset + 103 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 95);
            int pos2 = offset + 103 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                final int sl2 = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2) + sl2;
                pos2 += ItemAnimation.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 99);
            int pos3 = offset + 103 + fieldOffset3;
            pos3 += CameraSettings.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.wiggleWeights != null) {
            nullBits |= 0x1;
        }
        if (this.pullbackConfig != null) {
            nullBits |= 0x2;
        }
        if (this.id != null) {
            nullBits |= 0x4;
        }
        if (this.animations != null) {
            nullBits |= 0x8;
        }
        if (this.camera != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        if (this.wiggleWeights != null) {
            this.wiggleWeights.serialize(buf);
        }
        else {
            buf.writeZero(40);
        }
        if (this.pullbackConfig != null) {
            this.pullbackConfig.serialize(buf);
        }
        else {
            buf.writeZero(49);
        }
        buf.writeByte(this.useFirstPersonOverride ? 1 : 0);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int animationsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int cameraOffsetSlot = 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.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Animations", this.animations.size(), 4096000);
            }
            VarInt.write(buf, this.animations.size());
            for (final Map.Entry<String, ItemAnimation> e : this.animations.entrySet()) {
                PacketIO.writeVarString(buf, e.getKey(), 4096000);
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(animationsOffsetSlot, -1);
        }
        if (this.camera != null) {
            buf.setIntLE(cameraOffsetSlot, buf.writerIndex() - varBlockStart);
            this.camera.serialize(buf);
        }
        else {
            buf.setIntLE(cameraOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 103;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.animations != null) {
            int animationsSize = 0;
            for (final Map.Entry<String, ItemAnimation> kvp : this.animations.entrySet()) {
                animationsSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.animations.size()) + animationsSize;
        }
        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 < 103) {
            return ValidationResult.error("Buffer too small: expected at least 103 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x4) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 91);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 103 + 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 & 0x8) != 0x0) {
            final int animationsOffset = buffer.getIntLE(offset + 95);
            if (animationsOffset < 0) {
                return ValidationResult.error("Invalid offset for Animations");
            }
            int pos = offset + 103 + 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 dictionary 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 int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += ItemAnimation.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int cameraOffset = buffer.getIntLE(offset + 99);
            if (cameraOffset < 0) {
                return ValidationResult.error("Invalid offset for Camera");
            }
            int pos = offset + 103 + cameraOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Camera");
            }
            final ValidationResult cameraResult = CameraSettings.validateStructure(buffer, pos);
            if (!cameraResult.isValid()) {
                return ValidationResult.error("Invalid Camera: " + cameraResult.error());
            }
            pos += CameraSettings.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public ItemPlayerAnimations clone() {
        final ItemPlayerAnimations copy = new ItemPlayerAnimations();
        copy.id = this.id;
        if (this.animations != null) {
            final Map<String, ItemAnimation> m = new HashMap<String, ItemAnimation>();
            for (final Map.Entry<String, ItemAnimation> e : this.animations.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.animations = m;
        }
        copy.wiggleWeights = ((this.wiggleWeights != null) ? this.wiggleWeights.clone() : null);
        copy.camera = ((this.camera != null) ? this.camera.clone() : null);
        copy.pullbackConfig = ((this.pullbackConfig != null) ? this.pullbackConfig.clone() : null);
        copy.useFirstPersonOverride = this.useFirstPersonOverride;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemPlayerAnimations other) {
            return Objects.equals(this.id, other.id) && Objects.equals(this.animations, other.animations) && Objects.equals(this.wiggleWeights, other.wiggleWeights) && Objects.equals(this.camera, other.camera) && Objects.equals(this.pullbackConfig, other.pullbackConfig) && this.useFirstPersonOverride == other.useFirstPersonOverride;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.animations, this.wiggleWeights, this.camera, this.pullbackConfig, this.useFirstPersonOverride);
    }
}
