// 
// 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 java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Particle
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 133;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 141;
    public static final int MAX_SIZE = 270336151;
    @Nullable
    public String texturePath;
    @Nullable
    public Size frameSize;
    @Nonnull
    public ParticleUVOption uvOption;
    @Nonnull
    public ParticleScaleRatioConstraint scaleRatioConstraint;
    @Nonnull
    public SoftParticle softParticles;
    public float softParticlesFadeFactor;
    public boolean useSpriteBlending;
    @Nullable
    public ParticleAnimationFrame initialAnimationFrame;
    @Nullable
    public ParticleAnimationFrame collisionAnimationFrame;
    @Nullable
    public Map<Integer, ParticleAnimationFrame> animationFrames;
    
    public Particle() {
        this.uvOption = ParticleUVOption.None;
        this.scaleRatioConstraint = ParticleScaleRatioConstraint.OneToOne;
        this.softParticles = SoftParticle.Enable;
    }
    
    public Particle(@Nullable final String texturePath, @Nullable final Size frameSize, @Nonnull final ParticleUVOption uvOption, @Nonnull final ParticleScaleRatioConstraint scaleRatioConstraint, @Nonnull final SoftParticle softParticles, final float softParticlesFadeFactor, final boolean useSpriteBlending, @Nullable final ParticleAnimationFrame initialAnimationFrame, @Nullable final ParticleAnimationFrame collisionAnimationFrame, @Nullable final Map<Integer, ParticleAnimationFrame> animationFrames) {
        this.uvOption = ParticleUVOption.None;
        this.scaleRatioConstraint = ParticleScaleRatioConstraint.OneToOne;
        this.softParticles = SoftParticle.Enable;
        this.texturePath = texturePath;
        this.frameSize = frameSize;
        this.uvOption = uvOption;
        this.scaleRatioConstraint = scaleRatioConstraint;
        this.softParticles = softParticles;
        this.softParticlesFadeFactor = softParticlesFadeFactor;
        this.useSpriteBlending = useSpriteBlending;
        this.initialAnimationFrame = initialAnimationFrame;
        this.collisionAnimationFrame = collisionAnimationFrame;
        this.animationFrames = animationFrames;
    }
    
    public Particle(@Nonnull final Particle other) {
        this.uvOption = ParticleUVOption.None;
        this.scaleRatioConstraint = ParticleScaleRatioConstraint.OneToOne;
        this.softParticles = SoftParticle.Enable;
        this.texturePath = other.texturePath;
        this.frameSize = other.frameSize;
        this.uvOption = other.uvOption;
        this.scaleRatioConstraint = other.scaleRatioConstraint;
        this.softParticles = other.softParticles;
        this.softParticlesFadeFactor = other.softParticlesFadeFactor;
        this.useSpriteBlending = other.useSpriteBlending;
        this.initialAnimationFrame = other.initialAnimationFrame;
        this.collisionAnimationFrame = other.collisionAnimationFrame;
        this.animationFrames = other.animationFrames;
    }
    
    @Nonnull
    public static Particle deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Particle obj = new Particle();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.frameSize = Size.deserialize(buf, offset + 1);
        }
        obj.uvOption = ParticleUVOption.fromValue(buf.getByte(offset + 9));
        obj.scaleRatioConstraint = ParticleScaleRatioConstraint.fromValue(buf.getByte(offset + 10));
        obj.softParticles = SoftParticle.fromValue(buf.getByte(offset + 11));
        obj.softParticlesFadeFactor = buf.getFloatLE(offset + 12);
        obj.useSpriteBlending = (buf.getByte(offset + 16) != 0);
        if ((nullBits & 0x2) != 0x0) {
            obj.initialAnimationFrame = ParticleAnimationFrame.deserialize(buf, offset + 17);
        }
        if ((nullBits & 0x4) != 0x0) {
            obj.collisionAnimationFrame = ParticleAnimationFrame.deserialize(buf, offset + 75);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos0 = offset + 141 + buf.getIntLE(offset + 133);
            final int texturePathLen = VarInt.peek(buf, varPos0);
            if (texturePathLen < 0) {
                throw ProtocolException.negativeLength("TexturePath", texturePathLen);
            }
            if (texturePathLen > 4096000) {
                throw ProtocolException.stringTooLong("TexturePath", texturePathLen, 4096000);
            }
            obj.texturePath = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos2 = offset + 141 + buf.getIntLE(offset + 137);
            final int animationFramesCount = VarInt.peek(buf, varPos2);
            if (animationFramesCount < 0) {
                throw ProtocolException.negativeLength("AnimationFrames", animationFramesCount);
            }
            if (animationFramesCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("AnimationFrames", animationFramesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.animationFrames = new HashMap<Integer, ParticleAnimationFrame>(animationFramesCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < animationFramesCount; ++i) {
                final int key = buf.getIntLE(dictPos);
                dictPos += 4;
                final ParticleAnimationFrame val = ParticleAnimationFrame.deserialize(buf, dictPos);
                dictPos += ParticleAnimationFrame.computeBytesConsumed(buf, dictPos);
                if (obj.animationFrames.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("animationFrames", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 141;
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 133);
            int pos0 = offset + 141 + 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 + 137);
            int pos2 = offset + 141 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                pos2 += 4;
                pos2 += ParticleAnimationFrame.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.frameSize != null) {
            nullBits |= 0x1;
        }
        if (this.initialAnimationFrame != null) {
            nullBits |= 0x2;
        }
        if (this.collisionAnimationFrame != null) {
            nullBits |= 0x4;
        }
        if (this.texturePath != null) {
            nullBits |= 0x8;
        }
        if (this.animationFrames != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        if (this.frameSize != null) {
            this.frameSize.serialize(buf);
        }
        else {
            buf.writeZero(8);
        }
        buf.writeByte(this.uvOption.getValue());
        buf.writeByte(this.scaleRatioConstraint.getValue());
        buf.writeByte(this.softParticles.getValue());
        buf.writeFloatLE(this.softParticlesFadeFactor);
        buf.writeByte(this.useSpriteBlending ? 1 : 0);
        if (this.initialAnimationFrame != null) {
            this.initialAnimationFrame.serialize(buf);
        }
        else {
            buf.writeZero(58);
        }
        if (this.collisionAnimationFrame != null) {
            this.collisionAnimationFrame.serialize(buf);
        }
        else {
            buf.writeZero(58);
        }
        final int texturePathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int animationFramesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.texturePath != null) {
            buf.setIntLE(texturePathOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.texturePath, 4096000);
        }
        else {
            buf.setIntLE(texturePathOffsetSlot, -1);
        }
        if (this.animationFrames != null) {
            buf.setIntLE(animationFramesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.animationFrames.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("AnimationFrames", this.animationFrames.size(), 4096000);
            }
            VarInt.write(buf, this.animationFrames.size());
            for (final Map.Entry<Integer, ParticleAnimationFrame> e : this.animationFrames.entrySet()) {
                buf.writeIntLE(e.getKey());
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(animationFramesOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 141;
        if (this.texturePath != null) {
            size += PacketIO.stringSize(this.texturePath);
        }
        if (this.animationFrames != null) {
            size += VarInt.size(this.animationFrames.size()) + this.animationFrames.size() * 62;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 141) {
            return ValidationResult.error("Buffer too small: expected at least 141 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x8) != 0x0) {
            final int texturePathOffset = buffer.getIntLE(offset + 133);
            if (texturePathOffset < 0) {
                return ValidationResult.error("Invalid offset for TexturePath");
            }
            int pos = offset + 141 + texturePathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for TexturePath");
            }
            final int texturePathLen = VarInt.peek(buffer, pos);
            if (texturePathLen < 0) {
                return ValidationResult.error("Invalid string length for TexturePath");
            }
            if (texturePathLen > 4096000) {
                return ValidationResult.error("TexturePath exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += texturePathLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading TexturePath");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int animationFramesOffset = buffer.getIntLE(offset + 137);
            if (animationFramesOffset < 0) {
                return ValidationResult.error("Invalid offset for AnimationFrames");
            }
            int pos = offset + 141 + animationFramesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AnimationFrames");
            }
            final int animationFramesCount = VarInt.peek(buffer, pos);
            if (animationFramesCount < 0) {
                return ValidationResult.error("Invalid dictionary count for AnimationFrames");
            }
            if (animationFramesCount > 4096000) {
                return ValidationResult.error("AnimationFrames exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < animationFramesCount; ++i) {
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += 58;
            }
        }
        return ValidationResult.OK;
    }
    
    public Particle clone() {
        final Particle copy = new Particle();
        copy.texturePath = this.texturePath;
        copy.frameSize = ((this.frameSize != null) ? this.frameSize.clone() : null);
        copy.uvOption = this.uvOption;
        copy.scaleRatioConstraint = this.scaleRatioConstraint;
        copy.softParticles = this.softParticles;
        copy.softParticlesFadeFactor = this.softParticlesFadeFactor;
        copy.useSpriteBlending = this.useSpriteBlending;
        copy.initialAnimationFrame = ((this.initialAnimationFrame != null) ? this.initialAnimationFrame.clone() : null);
        copy.collisionAnimationFrame = ((this.collisionAnimationFrame != null) ? this.collisionAnimationFrame.clone() : null);
        if (this.animationFrames != null) {
            final Map<Integer, ParticleAnimationFrame> m = new HashMap<Integer, ParticleAnimationFrame>();
            for (final Map.Entry<Integer, ParticleAnimationFrame> e : this.animationFrames.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.animationFrames = m;
        }
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Particle other) {
            return Objects.equals(this.texturePath, other.texturePath) && Objects.equals(this.frameSize, other.frameSize) && Objects.equals(this.uvOption, other.uvOption) && Objects.equals(this.scaleRatioConstraint, other.scaleRatioConstraint) && Objects.equals(this.softParticles, other.softParticles) && this.softParticlesFadeFactor == other.softParticlesFadeFactor && this.useSpriteBlending == other.useSpriteBlending && Objects.equals(this.initialAnimationFrame, other.initialAnimationFrame) && Objects.equals(this.collisionAnimationFrame, other.collisionAnimationFrame) && Objects.equals(this.animationFrames, other.animationFrames);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.texturePath, this.frameSize, this.uvOption, this.scaleRatioConstraint, this.softParticles, this.softParticlesFadeFactor, this.useSpriteBlending, this.initialAnimationFrame, this.collisionAnimationFrame, this.animationFrames);
    }
}
