// 
// 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 BlockParticleSet
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 32;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 40;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public Color color;
    public float scale;
    @Nullable
    public Vector3f positionOffset;
    @Nullable
    public Direction rotationOffset;
    @Nullable
    public Map<BlockParticleEvent, String> particleSystemIds;
    
    public BlockParticleSet() {
    }
    
    public BlockParticleSet(@Nullable final String id, @Nullable final Color color, final float scale, @Nullable final Vector3f positionOffset, @Nullable final Direction rotationOffset, @Nullable final Map<BlockParticleEvent, String> particleSystemIds) {
        this.id = id;
        this.color = color;
        this.scale = scale;
        this.positionOffset = positionOffset;
        this.rotationOffset = rotationOffset;
        this.particleSystemIds = particleSystemIds;
    }
    
    public BlockParticleSet(@Nonnull final BlockParticleSet other) {
        this.id = other.id;
        this.color = other.color;
        this.scale = other.scale;
        this.positionOffset = other.positionOffset;
        this.rotationOffset = other.rotationOffset;
        this.particleSystemIds = other.particleSystemIds;
    }
    
    @Nonnull
    public static BlockParticleSet deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockParticleSet obj = new BlockParticleSet();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.color = Color.deserialize(buf, offset + 1);
        }
        obj.scale = buf.getFloatLE(offset + 4);
        if ((nullBits & 0x2) != 0x0) {
            obj.positionOffset = Vector3f.deserialize(buf, offset + 8);
        }
        if ((nullBits & 0x4) != 0x0) {
            obj.rotationOffset = Direction.deserialize(buf, offset + 20);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos0 = offset + 40 + buf.getIntLE(offset + 32);
            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 & 0x10) != 0x0) {
            final int varPos2 = offset + 40 + buf.getIntLE(offset + 36);
            final int particleSystemIdsCount = VarInt.peek(buf, varPos2);
            if (particleSystemIdsCount < 0) {
                throw ProtocolException.negativeLength("ParticleSystemIds", particleSystemIdsCount);
            }
            if (particleSystemIdsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("ParticleSystemIds", particleSystemIdsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.particleSystemIds = new HashMap<BlockParticleEvent, String>(particleSystemIdsCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < particleSystemIdsCount; ++i) {
                final BlockParticleEvent key = BlockParticleEvent.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final int valLen = VarInt.peek(buf, dictPos);
                if (valLen < 0) {
                    throw ProtocolException.negativeLength("val", valLen);
                }
                if (valLen > 4096000) {
                    throw ProtocolException.stringTooLong("val", valLen, 4096000);
                }
                final int valVarLen = VarInt.length(buf, dictPos);
                final String val = PacketIO.readVarString(buf, dictPos);
                dictPos += valVarLen + valLen;
                if (obj.particleSystemIds.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("particleSystemIds", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 40;
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 32);
            int pos0 = offset + 40 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 36);
            int pos2 = offset + 40 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                ++pos2;
                final int sl2 = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2) + sl2;
            }
            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.color != null) {
            nullBits |= 0x1;
        }
        if (this.positionOffset != null) {
            nullBits |= 0x2;
        }
        if (this.rotationOffset != null) {
            nullBits |= 0x4;
        }
        if (this.id != null) {
            nullBits |= 0x8;
        }
        if (this.particleSystemIds != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        if (this.color != null) {
            this.color.serialize(buf);
        }
        else {
            buf.writeZero(3);
        }
        buf.writeFloatLE(this.scale);
        if (this.positionOffset != null) {
            this.positionOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.rotationOffset != null) {
            this.rotationOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int particleSystemIdsOffsetSlot = 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.particleSystemIds != null) {
            buf.setIntLE(particleSystemIdsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.particleSystemIds.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("ParticleSystemIds", this.particleSystemIds.size(), 4096000);
            }
            VarInt.write(buf, this.particleSystemIds.size());
            for (final Map.Entry<BlockParticleEvent, String> e : this.particleSystemIds.entrySet()) {
                buf.writeByte(e.getKey().getValue());
                PacketIO.writeVarString(buf, e.getValue(), 4096000);
            }
        }
        else {
            buf.setIntLE(particleSystemIdsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 40;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.particleSystemIds != null) {
            int particleSystemIdsSize = 0;
            for (final Map.Entry<BlockParticleEvent, String> kvp : this.particleSystemIds.entrySet()) {
                particleSystemIdsSize += 1 + PacketIO.stringSize(kvp.getValue());
            }
            size += VarInt.size(this.particleSystemIds.size()) + particleSystemIdsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 40) {
            return ValidationResult.error("Buffer too small: expected at least 40 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x8) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 32);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 40 + 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 & 0x10) != 0x0) {
            final int particleSystemIdsOffset = buffer.getIntLE(offset + 36);
            if (particleSystemIdsOffset < 0) {
                return ValidationResult.error("Invalid offset for ParticleSystemIds");
            }
            int pos = offset + 40 + particleSystemIdsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ParticleSystemIds");
            }
            final int particleSystemIdsCount = VarInt.peek(buffer, pos);
            if (particleSystemIdsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for ParticleSystemIds");
            }
            if (particleSystemIdsCount > 4096000) {
                return ValidationResult.error("ParticleSystemIds exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < particleSystemIdsCount; ++i) {
                ++pos;
                final int valueLen = VarInt.peek(buffer, pos);
                if (valueLen < 0) {
                    return ValidationResult.error("Invalid string length for value");
                }
                if (valueLen > 4096000) {
                    return ValidationResult.error("value exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += valueLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading value");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public BlockParticleSet clone() {
        final BlockParticleSet copy = new BlockParticleSet();
        copy.id = this.id;
        copy.color = ((this.color != null) ? this.color.clone() : null);
        copy.scale = this.scale;
        copy.positionOffset = ((this.positionOffset != null) ? this.positionOffset.clone() : null);
        copy.rotationOffset = ((this.rotationOffset != null) ? this.rotationOffset.clone() : null);
        copy.particleSystemIds = ((this.particleSystemIds != null) ? new HashMap<BlockParticleEvent, String>(this.particleSystemIds) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockParticleSet other) {
            return Objects.equals(this.id, other.id) && Objects.equals(this.color, other.color) && this.scale == other.scale && Objects.equals(this.positionOffset, other.positionOffset) && Objects.equals(this.rotationOffset, other.rotationOffset) && Objects.equals(this.particleSystemIds, other.particleSystemIds);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.color, this.scale, this.positionOffset, this.rotationOffset, this.particleSystemIds);
    }
}
