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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;

public class AmbienceFXSound
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 27;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 27;
    public static final int MAX_SIZE = 27;
    public int soundEventIndex;
    @Nonnull
    public AmbienceFXSoundPlay3D play3D;
    public int blockSoundSetIndex;
    @Nonnull
    public AmbienceFXAltitude altitude;
    @Nullable
    public Rangef frequency;
    @Nullable
    public Range radius;
    
    public AmbienceFXSound() {
        this.play3D = AmbienceFXSoundPlay3D.Random;
        this.altitude = AmbienceFXAltitude.Normal;
    }
    
    public AmbienceFXSound(final int soundEventIndex, @Nonnull final AmbienceFXSoundPlay3D play3D, final int blockSoundSetIndex, @Nonnull final AmbienceFXAltitude altitude, @Nullable final Rangef frequency, @Nullable final Range radius) {
        this.play3D = AmbienceFXSoundPlay3D.Random;
        this.altitude = AmbienceFXAltitude.Normal;
        this.soundEventIndex = soundEventIndex;
        this.play3D = play3D;
        this.blockSoundSetIndex = blockSoundSetIndex;
        this.altitude = altitude;
        this.frequency = frequency;
        this.radius = radius;
    }
    
    public AmbienceFXSound(@Nonnull final AmbienceFXSound other) {
        this.play3D = AmbienceFXSoundPlay3D.Random;
        this.altitude = AmbienceFXAltitude.Normal;
        this.soundEventIndex = other.soundEventIndex;
        this.play3D = other.play3D;
        this.blockSoundSetIndex = other.blockSoundSetIndex;
        this.altitude = other.altitude;
        this.frequency = other.frequency;
        this.radius = other.radius;
    }
    
    @Nonnull
    public static AmbienceFXSound deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AmbienceFXSound obj = new AmbienceFXSound();
        final byte nullBits = buf.getByte(offset);
        obj.soundEventIndex = buf.getIntLE(offset + 1);
        obj.play3D = AmbienceFXSoundPlay3D.fromValue(buf.getByte(offset + 5));
        obj.blockSoundSetIndex = buf.getIntLE(offset + 6);
        obj.altitude = AmbienceFXAltitude.fromValue(buf.getByte(offset + 10));
        if ((nullBits & 0x1) != 0x0) {
            obj.frequency = Rangef.deserialize(buf, offset + 11);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.radius = Range.deserialize(buf, offset + 19);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 27;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.frequency != null) {
            nullBits |= 0x1;
        }
        if (this.radius != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.soundEventIndex);
        buf.writeByte(this.play3D.getValue());
        buf.writeIntLE(this.blockSoundSetIndex);
        buf.writeByte(this.altitude.getValue());
        if (this.frequency != null) {
            this.frequency.serialize(buf);
        }
        else {
            buf.writeZero(8);
        }
        if (this.radius != null) {
            this.radius.serialize(buf);
        }
        else {
            buf.writeZero(8);
        }
    }
    
    public int computeSize() {
        return 27;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 27) {
            return ValidationResult.error("Buffer too small: expected at least 27 bytes");
        }
        return ValidationResult.OK;
    }
    
    public AmbienceFXSound clone() {
        final AmbienceFXSound copy = new AmbienceFXSound();
        copy.soundEventIndex = this.soundEventIndex;
        copy.play3D = this.play3D;
        copy.blockSoundSetIndex = this.blockSoundSetIndex;
        copy.altitude = this.altitude;
        copy.frequency = ((this.frequency != null) ? this.frequency.clone() : null);
        copy.radius = ((this.radius != null) ? this.radius.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AmbienceFXSound other) {
            return this.soundEventIndex == other.soundEventIndex && Objects.equals(this.play3D, other.play3D) && this.blockSoundSetIndex == other.blockSoundSetIndex && Objects.equals(this.altitude, other.altitude) && Objects.equals(this.frequency, other.frequency) && Objects.equals(this.radius, other.radius);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.soundEventIndex, this.play3D, this.blockSoundSetIndex, this.altitude, this.frequency, this.radius);
    }
}
