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

package com.hypixel.hytale.protocol;

import java.util.Objects;
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 FluidFX
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 61;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 69;
    public static final int MAX_SIZE = 32768087;
    @Nullable
    public String id;
    @Nonnull
    public ShaderType shader;
    @Nonnull
    public FluidFog fogMode;
    @Nullable
    public Color fogColor;
    @Nullable
    public NearFar fogDistance;
    public float fogDepthStart;
    public float fogDepthFalloff;
    @Nullable
    public Color colorFilter;
    public float colorSaturation;
    public float distortionAmplitude;
    public float distortionFrequency;
    @Nullable
    public FluidParticle particle;
    @Nullable
    public FluidFXMovementSettings movementSettings;
    
    public FluidFX() {
        this.shader = ShaderType.None;
        this.fogMode = FluidFog.Color;
    }
    
    public FluidFX(@Nullable final String id, @Nonnull final ShaderType shader, @Nonnull final FluidFog fogMode, @Nullable final Color fogColor, @Nullable final NearFar fogDistance, final float fogDepthStart, final float fogDepthFalloff, @Nullable final Color colorFilter, final float colorSaturation, final float distortionAmplitude, final float distortionFrequency, @Nullable final FluidParticle particle, @Nullable final FluidFXMovementSettings movementSettings) {
        this.shader = ShaderType.None;
        this.fogMode = FluidFog.Color;
        this.id = id;
        this.shader = shader;
        this.fogMode = fogMode;
        this.fogColor = fogColor;
        this.fogDistance = fogDistance;
        this.fogDepthStart = fogDepthStart;
        this.fogDepthFalloff = fogDepthFalloff;
        this.colorFilter = colorFilter;
        this.colorSaturation = colorSaturation;
        this.distortionAmplitude = distortionAmplitude;
        this.distortionFrequency = distortionFrequency;
        this.particle = particle;
        this.movementSettings = movementSettings;
    }
    
    public FluidFX(@Nonnull final FluidFX other) {
        this.shader = ShaderType.None;
        this.fogMode = FluidFog.Color;
        this.id = other.id;
        this.shader = other.shader;
        this.fogMode = other.fogMode;
        this.fogColor = other.fogColor;
        this.fogDistance = other.fogDistance;
        this.fogDepthStart = other.fogDepthStart;
        this.fogDepthFalloff = other.fogDepthFalloff;
        this.colorFilter = other.colorFilter;
        this.colorSaturation = other.colorSaturation;
        this.distortionAmplitude = other.distortionAmplitude;
        this.distortionFrequency = other.distortionFrequency;
        this.particle = other.particle;
        this.movementSettings = other.movementSettings;
    }
    
    @Nonnull
    public static FluidFX deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final FluidFX obj = new FluidFX();
        final byte nullBits = buf.getByte(offset);
        obj.shader = ShaderType.fromValue(buf.getByte(offset + 1));
        obj.fogMode = FluidFog.fromValue(buf.getByte(offset + 2));
        if ((nullBits & 0x1) != 0x0) {
            obj.fogColor = Color.deserialize(buf, offset + 3);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.fogDistance = NearFar.deserialize(buf, offset + 6);
        }
        obj.fogDepthStart = buf.getFloatLE(offset + 14);
        obj.fogDepthFalloff = buf.getFloatLE(offset + 18);
        if ((nullBits & 0x4) != 0x0) {
            obj.colorFilter = Color.deserialize(buf, offset + 22);
        }
        obj.colorSaturation = buf.getFloatLE(offset + 25);
        obj.distortionAmplitude = buf.getFloatLE(offset + 29);
        obj.distortionFrequency = buf.getFloatLE(offset + 33);
        if ((nullBits & 0x8) != 0x0) {
            obj.movementSettings = FluidFXMovementSettings.deserialize(buf, offset + 37);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos0 = offset + 69 + buf.getIntLE(offset + 61);
            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 & 0x20) != 0x0) {
            final int varPos2 = offset + 69 + buf.getIntLE(offset + 65);
            obj.particle = FluidParticle.deserialize(buf, varPos2);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 69;
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 61);
            int pos0 = offset + 69 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 65);
            int pos2 = offset + 69 + fieldOffset2;
            pos2 += FluidParticle.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.fogColor != null) {
            nullBits |= 0x1;
        }
        if (this.fogDistance != null) {
            nullBits |= 0x2;
        }
        if (this.colorFilter != null) {
            nullBits |= 0x4;
        }
        if (this.movementSettings != null) {
            nullBits |= 0x8;
        }
        if (this.id != null) {
            nullBits |= 0x10;
        }
        if (this.particle != null) {
            nullBits |= 0x20;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.shader.getValue());
        buf.writeByte(this.fogMode.getValue());
        if (this.fogColor != null) {
            this.fogColor.serialize(buf);
        }
        else {
            buf.writeZero(3);
        }
        if (this.fogDistance != null) {
            this.fogDistance.serialize(buf);
        }
        else {
            buf.writeZero(8);
        }
        buf.writeFloatLE(this.fogDepthStart);
        buf.writeFloatLE(this.fogDepthFalloff);
        if (this.colorFilter != null) {
            this.colorFilter.serialize(buf);
        }
        else {
            buf.writeZero(3);
        }
        buf.writeFloatLE(this.colorSaturation);
        buf.writeFloatLE(this.distortionAmplitude);
        buf.writeFloatLE(this.distortionFrequency);
        if (this.movementSettings != null) {
            this.movementSettings.serialize(buf);
        }
        else {
            buf.writeZero(24);
        }
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int particleOffsetSlot = 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.particle != null) {
            buf.setIntLE(particleOffsetSlot, buf.writerIndex() - varBlockStart);
            this.particle.serialize(buf);
        }
        else {
            buf.setIntLE(particleOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 69;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.particle != null) {
            size += this.particle.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 69) {
            return ValidationResult.error("Buffer too small: expected at least 69 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x10) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 61);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 69 + 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 & 0x20) != 0x0) {
            final int particleOffset = buffer.getIntLE(offset + 65);
            if (particleOffset < 0) {
                return ValidationResult.error("Invalid offset for Particle");
            }
            int pos = offset + 69 + particleOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Particle");
            }
            final ValidationResult particleResult = FluidParticle.validateStructure(buffer, pos);
            if (!particleResult.isValid()) {
                return ValidationResult.error("Invalid Particle: " + particleResult.error());
            }
            pos += FluidParticle.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public FluidFX clone() {
        final FluidFX copy = new FluidFX();
        copy.id = this.id;
        copy.shader = this.shader;
        copy.fogMode = this.fogMode;
        copy.fogColor = ((this.fogColor != null) ? this.fogColor.clone() : null);
        copy.fogDistance = ((this.fogDistance != null) ? this.fogDistance.clone() : null);
        copy.fogDepthStart = this.fogDepthStart;
        copy.fogDepthFalloff = this.fogDepthFalloff;
        copy.colorFilter = ((this.colorFilter != null) ? this.colorFilter.clone() : null);
        copy.colorSaturation = this.colorSaturation;
        copy.distortionAmplitude = this.distortionAmplitude;
        copy.distortionFrequency = this.distortionFrequency;
        copy.particle = ((this.particle != null) ? this.particle.clone() : null);
        copy.movementSettings = ((this.movementSettings != null) ? this.movementSettings.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final FluidFX other) {
            return Objects.equals(this.id, other.id) && Objects.equals(this.shader, other.shader) && Objects.equals(this.fogMode, other.fogMode) && Objects.equals(this.fogColor, other.fogColor) && Objects.equals(this.fogDistance, other.fogDistance) && this.fogDepthStart == other.fogDepthStart && this.fogDepthFalloff == other.fogDepthFalloff && Objects.equals(this.colorFilter, other.colorFilter) && this.colorSaturation == other.colorSaturation && this.distortionAmplitude == other.distortionAmplitude && this.distortionFrequency == other.distortionFrequency && Objects.equals(this.particle, other.particle) && Objects.equals(this.movementSettings, other.movementSettings);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.shader, this.fogMode, this.fogColor, this.fogDistance, this.fogDepthStart, this.fogDepthFalloff, this.colorFilter, this.colorSaturation, this.distortionAmplitude, this.distortionFrequency, this.particle, this.movementSettings);
    }
}
