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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import java.util.Arrays;
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 SoundEvent
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 34;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 42;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    public float volume;
    public float pitch;
    public float musicDuckingVolume;
    public float ambientDuckingVolume;
    public int maxInstance;
    public boolean preventSoundInterruption;
    public float startAttenuationDistance;
    public float maxDistance;
    @Nullable
    public SoundEventLayer[] layers;
    public int audioCategory;
    
    public SoundEvent() {
    }
    
    public SoundEvent(@Nullable final String id, final float volume, final float pitch, final float musicDuckingVolume, final float ambientDuckingVolume, final int maxInstance, final boolean preventSoundInterruption, final float startAttenuationDistance, final float maxDistance, @Nullable final SoundEventLayer[] layers, final int audioCategory) {
        this.id = id;
        this.volume = volume;
        this.pitch = pitch;
        this.musicDuckingVolume = musicDuckingVolume;
        this.ambientDuckingVolume = ambientDuckingVolume;
        this.maxInstance = maxInstance;
        this.preventSoundInterruption = preventSoundInterruption;
        this.startAttenuationDistance = startAttenuationDistance;
        this.maxDistance = maxDistance;
        this.layers = layers;
        this.audioCategory = audioCategory;
    }
    
    public SoundEvent(@Nonnull final SoundEvent other) {
        this.id = other.id;
        this.volume = other.volume;
        this.pitch = other.pitch;
        this.musicDuckingVolume = other.musicDuckingVolume;
        this.ambientDuckingVolume = other.ambientDuckingVolume;
        this.maxInstance = other.maxInstance;
        this.preventSoundInterruption = other.preventSoundInterruption;
        this.startAttenuationDistance = other.startAttenuationDistance;
        this.maxDistance = other.maxDistance;
        this.layers = other.layers;
        this.audioCategory = other.audioCategory;
    }
    
    @Nonnull
    public static SoundEvent deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SoundEvent obj = new SoundEvent();
        final byte nullBits = buf.getByte(offset);
        obj.volume = buf.getFloatLE(offset + 1);
        obj.pitch = buf.getFloatLE(offset + 5);
        obj.musicDuckingVolume = buf.getFloatLE(offset + 9);
        obj.ambientDuckingVolume = buf.getFloatLE(offset + 13);
        obj.maxInstance = buf.getIntLE(offset + 17);
        obj.preventSoundInterruption = (buf.getByte(offset + 21) != 0);
        obj.startAttenuationDistance = buf.getFloatLE(offset + 22);
        obj.maxDistance = buf.getFloatLE(offset + 26);
        obj.audioCategory = buf.getIntLE(offset + 30);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 42 + buf.getIntLE(offset + 34);
            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 & 0x2) != 0x0) {
            final int varPos2 = offset + 42 + buf.getIntLE(offset + 38);
            final int layersCount = VarInt.peek(buf, varPos2);
            if (layersCount < 0) {
                throw ProtocolException.negativeLength("Layers", layersCount);
            }
            if (layersCount > 4096000) {
                throw ProtocolException.arrayTooLong("Layers", layersCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + layersCount * 42L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Layers", varPos2 + varIntLen + layersCount * 42, buf.readableBytes());
            }
            obj.layers = new SoundEventLayer[layersCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < layersCount; ++i) {
                obj.layers[i] = SoundEventLayer.deserialize(buf, elemPos);
                elemPos += SoundEventLayer.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 42;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 34);
            int pos0 = offset + 42 + 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 + 38);
            int pos2 = offset + 42 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += SoundEventLayer.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.id != null) {
            nullBits |= 0x1;
        }
        if (this.layers != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeFloatLE(this.volume);
        buf.writeFloatLE(this.pitch);
        buf.writeFloatLE(this.musicDuckingVolume);
        buf.writeFloatLE(this.ambientDuckingVolume);
        buf.writeIntLE(this.maxInstance);
        buf.writeByte(this.preventSoundInterruption ? 1 : 0);
        buf.writeFloatLE(this.startAttenuationDistance);
        buf.writeFloatLE(this.maxDistance);
        buf.writeIntLE(this.audioCategory);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int layersOffsetSlot = 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.layers != null) {
            buf.setIntLE(layersOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.layers.length > 4096000) {
                throw ProtocolException.arrayTooLong("Layers", this.layers.length, 4096000);
            }
            VarInt.write(buf, this.layers.length);
            for (final SoundEventLayer item : this.layers) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(layersOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 42;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.layers != null) {
            int layersSize = 0;
            for (final SoundEventLayer elem : this.layers) {
                layersSize += elem.computeSize();
            }
            size += VarInt.size(this.layers.length) + layersSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 42) {
            return ValidationResult.error("Buffer too small: expected at least 42 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 34);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 42 + 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 & 0x2) != 0x0) {
            final int layersOffset = buffer.getIntLE(offset + 38);
            if (layersOffset < 0) {
                return ValidationResult.error("Invalid offset for Layers");
            }
            int pos = offset + 42 + layersOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Layers");
            }
            final int layersCount = VarInt.peek(buffer, pos);
            if (layersCount < 0) {
                return ValidationResult.error("Invalid array count for Layers");
            }
            if (layersCount > 4096000) {
                return ValidationResult.error("Layers exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < layersCount; ++i) {
                final ValidationResult structResult = SoundEventLayer.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid SoundEventLayer in Layers[" + i + "]: " + structResult.error());
                }
                pos += SoundEventLayer.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public SoundEvent clone() {
        final SoundEvent copy = new SoundEvent();
        copy.id = this.id;
        copy.volume = this.volume;
        copy.pitch = this.pitch;
        copy.musicDuckingVolume = this.musicDuckingVolume;
        copy.ambientDuckingVolume = this.ambientDuckingVolume;
        copy.maxInstance = this.maxInstance;
        copy.preventSoundInterruption = this.preventSoundInterruption;
        copy.startAttenuationDistance = this.startAttenuationDistance;
        copy.maxDistance = this.maxDistance;
        copy.layers = (SoundEventLayer[])((this.layers != null) ? ((SoundEventLayer[])Arrays.stream(this.layers).map(e -> e.clone()).toArray(SoundEventLayer[]::new)) : null);
        copy.audioCategory = this.audioCategory;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SoundEvent other) {
            return Objects.equals(this.id, other.id) && this.volume == other.volume && this.pitch == other.pitch && this.musicDuckingVolume == other.musicDuckingVolume && this.ambientDuckingVolume == other.ambientDuckingVolume && this.maxInstance == other.maxInstance && this.preventSoundInterruption == other.preventSoundInterruption && this.startAttenuationDistance == other.startAttenuationDistance && this.maxDistance == other.maxDistance && Arrays.equals(this.layers, other.layers) && this.audioCategory == other.audioCategory;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Float.hashCode(this.volume);
        result = 31 * result + Float.hashCode(this.pitch);
        result = 31 * result + Float.hashCode(this.musicDuckingVolume);
        result = 31 * result + Float.hashCode(this.ambientDuckingVolume);
        result = 31 * result + Integer.hashCode(this.maxInstance);
        result = 31 * result + Boolean.hashCode(this.preventSoundInterruption);
        result = 31 * result + Float.hashCode(this.startAttenuationDistance);
        result = 31 * result + Float.hashCode(this.maxDistance);
        result = 31 * result + Arrays.hashCode(this.layers);
        result = 31 * result + Integer.hashCode(this.audioCategory);
        return result;
    }
}
