// 
// 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 ModelOverride
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String model;
    @Nullable
    public String texture;
    @Nullable
    public Map<String, AnimationSet> animationSets;
    
    public ModelOverride() {
    }
    
    public ModelOverride(@Nullable final String model, @Nullable final String texture, @Nullable final Map<String, AnimationSet> animationSets) {
        this.model = model;
        this.texture = texture;
        this.animationSets = animationSets;
    }
    
    public ModelOverride(@Nonnull final ModelOverride other) {
        this.model = other.model;
        this.texture = other.texture;
        this.animationSets = other.animationSets;
    }
    
    @Nonnull
    public static ModelOverride deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ModelOverride obj = new ModelOverride();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int modelLen = VarInt.peek(buf, varPos0);
            if (modelLen < 0) {
                throw ProtocolException.negativeLength("Model", modelLen);
            }
            if (modelLen > 4096000) {
                throw ProtocolException.stringTooLong("Model", modelLen, 4096000);
            }
            obj.model = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            final int textureLen = VarInt.peek(buf, varPos2);
            if (textureLen < 0) {
                throw ProtocolException.negativeLength("Texture", textureLen);
            }
            if (textureLen > 4096000) {
                throw ProtocolException.stringTooLong("Texture", textureLen, 4096000);
            }
            obj.texture = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            final int animationSetsCount = VarInt.peek(buf, varPos3);
            if (animationSetsCount < 0) {
                throw ProtocolException.negativeLength("AnimationSets", animationSetsCount);
            }
            if (animationSetsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("AnimationSets", animationSetsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            obj.animationSets = new HashMap<String, AnimationSet>(animationSetsCount);
            int dictPos = varPos3 + varIntLen;
            for (int i = 0; i < animationSetsCount; ++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 AnimationSet val = AnimationSet.deserialize(buf, dictPos);
                dictPos += AnimationSet.computeBytesConsumed(buf, dictPos);
                if (obj.animationSets.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("animationSets", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 13 + 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 + 5);
            int pos2 = offset + 13 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 9);
            int pos3 = offset + 13 + fieldOffset3;
            final int dictLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < dictLen; ++i) {
                final int sl2 = VarInt.peek(buf, pos3);
                pos3 += VarInt.length(buf, pos3) + sl2;
                pos3 += AnimationSet.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.model != null) {
            nullBits |= 0x1;
        }
        if (this.texture != null) {
            nullBits |= 0x2;
        }
        if (this.animationSets != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int modelOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int textureOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int animationSetsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.model != null) {
            buf.setIntLE(modelOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.model, 4096000);
        }
        else {
            buf.setIntLE(modelOffsetSlot, -1);
        }
        if (this.texture != null) {
            buf.setIntLE(textureOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.texture, 4096000);
        }
        else {
            buf.setIntLE(textureOffsetSlot, -1);
        }
        if (this.animationSets != null) {
            buf.setIntLE(animationSetsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.animationSets.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("AnimationSets", this.animationSets.size(), 4096000);
            }
            VarInt.write(buf, this.animationSets.size());
            for (final Map.Entry<String, AnimationSet> e : this.animationSets.entrySet()) {
                PacketIO.writeVarString(buf, e.getKey(), 4096000);
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(animationSetsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 13;
        if (this.model != null) {
            size += PacketIO.stringSize(this.model);
        }
        if (this.texture != null) {
            size += PacketIO.stringSize(this.texture);
        }
        if (this.animationSets != null) {
            int animationSetsSize = 0;
            for (final Map.Entry<String, AnimationSet> kvp : this.animationSets.entrySet()) {
                animationSetsSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.animationSets.size()) + animationSetsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int modelOffset = buffer.getIntLE(offset + 1);
            if (modelOffset < 0) {
                return ValidationResult.error("Invalid offset for Model");
            }
            int pos = offset + 13 + modelOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Model");
            }
            final int modelLen = VarInt.peek(buffer, pos);
            if (modelLen < 0) {
                return ValidationResult.error("Invalid string length for Model");
            }
            if (modelLen > 4096000) {
                return ValidationResult.error("Model exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += modelLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Model");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int textureOffset = buffer.getIntLE(offset + 5);
            if (textureOffset < 0) {
                return ValidationResult.error("Invalid offset for Texture");
            }
            int pos = offset + 13 + textureOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Texture");
            }
            final int textureLen = VarInt.peek(buffer, pos);
            if (textureLen < 0) {
                return ValidationResult.error("Invalid string length for Texture");
            }
            if (textureLen > 4096000) {
                return ValidationResult.error("Texture exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += textureLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Texture");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int animationSetsOffset = buffer.getIntLE(offset + 9);
            if (animationSetsOffset < 0) {
                return ValidationResult.error("Invalid offset for AnimationSets");
            }
            int pos = offset + 13 + animationSetsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AnimationSets");
            }
            final int animationSetsCount = VarInt.peek(buffer, pos);
            if (animationSetsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for AnimationSets");
            }
            if (animationSetsCount > 4096000) {
                return ValidationResult.error("AnimationSets exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < animationSetsCount; ++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 += AnimationSet.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public ModelOverride clone() {
        final ModelOverride copy = new ModelOverride();
        copy.model = this.model;
        copy.texture = this.texture;
        if (this.animationSets != null) {
            final Map<String, AnimationSet> m = new HashMap<String, AnimationSet>();
            for (final Map.Entry<String, AnimationSet> e : this.animationSets.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.animationSets = m;
        }
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ModelOverride other) {
            return Objects.equals(this.model, other.model) && Objects.equals(this.texture, other.texture) && Objects.equals(this.animationSets, other.animationSets);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.model, this.texture, this.animationSets);
    }
}
