// 
// 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 AmbienceFX
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 18;
    public static final int VARIABLE_FIELD_COUNT = 6;
    public static final int VARIABLE_BLOCK_START = 42;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public AmbienceFXConditions conditions;
    @Nullable
    public AmbienceFXSound[] sounds;
    @Nullable
    public AmbienceFXMusic music;
    @Nullable
    public AmbienceFXAmbientBed ambientBed;
    @Nullable
    public AmbienceFXSoundEffect soundEffect;
    public int priority;
    @Nullable
    public int[] blockedAmbienceFxIndices;
    public int audioCategoryIndex;
    
    public AmbienceFX() {
    }
    
    public AmbienceFX(@Nullable final String id, @Nullable final AmbienceFXConditions conditions, @Nullable final AmbienceFXSound[] sounds, @Nullable final AmbienceFXMusic music, @Nullable final AmbienceFXAmbientBed ambientBed, @Nullable final AmbienceFXSoundEffect soundEffect, final int priority, @Nullable final int[] blockedAmbienceFxIndices, final int audioCategoryIndex) {
        this.id = id;
        this.conditions = conditions;
        this.sounds = sounds;
        this.music = music;
        this.ambientBed = ambientBed;
        this.soundEffect = soundEffect;
        this.priority = priority;
        this.blockedAmbienceFxIndices = blockedAmbienceFxIndices;
        this.audioCategoryIndex = audioCategoryIndex;
    }
    
    public AmbienceFX(@Nonnull final AmbienceFX other) {
        this.id = other.id;
        this.conditions = other.conditions;
        this.sounds = other.sounds;
        this.music = other.music;
        this.ambientBed = other.ambientBed;
        this.soundEffect = other.soundEffect;
        this.priority = other.priority;
        this.blockedAmbienceFxIndices = other.blockedAmbienceFxIndices;
        this.audioCategoryIndex = other.audioCategoryIndex;
    }
    
    @Nonnull
    public static AmbienceFX deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AmbienceFX obj = new AmbienceFX();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.soundEffect = AmbienceFXSoundEffect.deserialize(buf, offset + 1);
        }
        obj.priority = buf.getIntLE(offset + 10);
        obj.audioCategoryIndex = buf.getIntLE(offset + 14);
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 42 + buf.getIntLE(offset + 18);
            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 & 0x4) != 0x0) {
            final int varPos2 = offset + 42 + buf.getIntLE(offset + 22);
            obj.conditions = AmbienceFXConditions.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos3 = offset + 42 + buf.getIntLE(offset + 26);
            final int soundsCount = VarInt.peek(buf, varPos3);
            if (soundsCount < 0) {
                throw ProtocolException.negativeLength("Sounds", soundsCount);
            }
            if (soundsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Sounds", soundsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + soundsCount * 27L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Sounds", varPos3 + varIntLen + soundsCount * 27, buf.readableBytes());
            }
            obj.sounds = new AmbienceFXSound[soundsCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < soundsCount; ++i) {
                obj.sounds[i] = AmbienceFXSound.deserialize(buf, elemPos);
                elemPos += AmbienceFXSound.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos4 = offset + 42 + buf.getIntLE(offset + 30);
            obj.music = AmbienceFXMusic.deserialize(buf, varPos4);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int varPos5 = offset + 42 + buf.getIntLE(offset + 34);
            obj.ambientBed = AmbienceFXAmbientBed.deserialize(buf, varPos5);
        }
        if ((nullBits & 0x40) != 0x0) {
            final int varPos6 = offset + 42 + buf.getIntLE(offset + 38);
            final int blockedAmbienceFxIndicesCount = VarInt.peek(buf, varPos6);
            if (blockedAmbienceFxIndicesCount < 0) {
                throw ProtocolException.negativeLength("BlockedAmbienceFxIndices", blockedAmbienceFxIndicesCount);
            }
            if (blockedAmbienceFxIndicesCount > 4096000) {
                throw ProtocolException.arrayTooLong("BlockedAmbienceFxIndices", blockedAmbienceFxIndicesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos6);
            if (varPos6 + varIntLen + blockedAmbienceFxIndicesCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("BlockedAmbienceFxIndices", varPos6 + varIntLen + blockedAmbienceFxIndicesCount * 4, buf.readableBytes());
            }
            obj.blockedAmbienceFxIndices = new int[blockedAmbienceFxIndicesCount];
            for (int j = 0; j < blockedAmbienceFxIndicesCount; ++j) {
                obj.blockedAmbienceFxIndices[j] = buf.getIntLE(varPos6 + varIntLen + j * 4);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 42;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 18);
            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 & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 22);
            int pos2 = offset + 42 + fieldOffset2;
            pos2 += AmbienceFXConditions.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 26);
            int pos3 = offset + 42 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < arrLen; ++i) {
                pos3 += AmbienceFXSound.computeBytesConsumed(buf, pos3);
            }
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 30);
            int pos4 = offset + 42 + fieldOffset4;
            pos4 += AmbienceFXMusic.computeBytesConsumed(buf, pos4);
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 34);
            int pos5 = offset + 42 + fieldOffset5;
            pos5 += AmbienceFXAmbientBed.computeBytesConsumed(buf, pos5);
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        if ((nullBits & 0x40) != 0x0) {
            final int fieldOffset6 = buf.getIntLE(offset + 38);
            int pos6 = offset + 42 + fieldOffset6;
            final int arrLen = VarInt.peek(buf, pos6);
            pos6 += VarInt.length(buf, pos6) + arrLen * 4;
            if (pos6 - offset > maxEnd) {
                maxEnd = pos6 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.soundEffect != null) {
            nullBits |= 0x1;
        }
        if (this.id != null) {
            nullBits |= 0x2;
        }
        if (this.conditions != null) {
            nullBits |= 0x4;
        }
        if (this.sounds != null) {
            nullBits |= 0x8;
        }
        if (this.music != null) {
            nullBits |= 0x10;
        }
        if (this.ambientBed != null) {
            nullBits |= 0x20;
        }
        if (this.blockedAmbienceFxIndices != null) {
            nullBits |= 0x40;
        }
        buf.writeByte(nullBits);
        if (this.soundEffect != null) {
            this.soundEffect.serialize(buf);
        }
        else {
            buf.writeZero(9);
        }
        buf.writeIntLE(this.priority);
        buf.writeIntLE(this.audioCategoryIndex);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int conditionsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int soundsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int musicOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int ambientBedOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int blockedAmbienceFxIndicesOffsetSlot = 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.conditions != null) {
            buf.setIntLE(conditionsOffsetSlot, buf.writerIndex() - varBlockStart);
            this.conditions.serialize(buf);
        }
        else {
            buf.setIntLE(conditionsOffsetSlot, -1);
        }
        if (this.sounds != null) {
            buf.setIntLE(soundsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.sounds.length > 4096000) {
                throw ProtocolException.arrayTooLong("Sounds", this.sounds.length, 4096000);
            }
            VarInt.write(buf, this.sounds.length);
            for (final AmbienceFXSound item : this.sounds) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(soundsOffsetSlot, -1);
        }
        if (this.music != null) {
            buf.setIntLE(musicOffsetSlot, buf.writerIndex() - varBlockStart);
            this.music.serialize(buf);
        }
        else {
            buf.setIntLE(musicOffsetSlot, -1);
        }
        if (this.ambientBed != null) {
            buf.setIntLE(ambientBedOffsetSlot, buf.writerIndex() - varBlockStart);
            this.ambientBed.serialize(buf);
        }
        else {
            buf.setIntLE(ambientBedOffsetSlot, -1);
        }
        if (this.blockedAmbienceFxIndices != null) {
            buf.setIntLE(blockedAmbienceFxIndicesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.blockedAmbienceFxIndices.length > 4096000) {
                throw ProtocolException.arrayTooLong("BlockedAmbienceFxIndices", this.blockedAmbienceFxIndices.length, 4096000);
            }
            VarInt.write(buf, this.blockedAmbienceFxIndices.length);
            for (final int item2 : this.blockedAmbienceFxIndices) {
                buf.writeIntLE(item2);
            }
        }
        else {
            buf.setIntLE(blockedAmbienceFxIndicesOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 42;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.conditions != null) {
            size += this.conditions.computeSize();
        }
        if (this.sounds != null) {
            size += VarInt.size(this.sounds.length) + this.sounds.length * 27;
        }
        if (this.music != null) {
            size += this.music.computeSize();
        }
        if (this.ambientBed != null) {
            size += this.ambientBed.computeSize();
        }
        if (this.blockedAmbienceFxIndices != null) {
            size += VarInt.size(this.blockedAmbienceFxIndices.length) + this.blockedAmbienceFxIndices.length * 4;
        }
        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 & 0x2) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 18);
            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 & 0x4) != 0x0) {
            final int conditionsOffset = buffer.getIntLE(offset + 22);
            if (conditionsOffset < 0) {
                return ValidationResult.error("Invalid offset for Conditions");
            }
            int pos = offset + 42 + conditionsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Conditions");
            }
            final ValidationResult conditionsResult = AmbienceFXConditions.validateStructure(buffer, pos);
            if (!conditionsResult.isValid()) {
                return ValidationResult.error("Invalid Conditions: " + conditionsResult.error());
            }
            pos += AmbienceFXConditions.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int soundsOffset = buffer.getIntLE(offset + 26);
            if (soundsOffset < 0) {
                return ValidationResult.error("Invalid offset for Sounds");
            }
            int pos = offset + 42 + soundsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Sounds");
            }
            final int soundsCount = VarInt.peek(buffer, pos);
            if (soundsCount < 0) {
                return ValidationResult.error("Invalid array count for Sounds");
            }
            if (soundsCount > 4096000) {
                return ValidationResult.error("Sounds exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += soundsCount * 27;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Sounds");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int musicOffset = buffer.getIntLE(offset + 30);
            if (musicOffset < 0) {
                return ValidationResult.error("Invalid offset for Music");
            }
            int pos = offset + 42 + musicOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Music");
            }
            final ValidationResult musicResult = AmbienceFXMusic.validateStructure(buffer, pos);
            if (!musicResult.isValid()) {
                return ValidationResult.error("Invalid Music: " + musicResult.error());
            }
            pos += AmbienceFXMusic.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int ambientBedOffset = buffer.getIntLE(offset + 34);
            if (ambientBedOffset < 0) {
                return ValidationResult.error("Invalid offset for AmbientBed");
            }
            int pos = offset + 42 + ambientBedOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AmbientBed");
            }
            final ValidationResult ambientBedResult = AmbienceFXAmbientBed.validateStructure(buffer, pos);
            if (!ambientBedResult.isValid()) {
                return ValidationResult.error("Invalid AmbientBed: " + ambientBedResult.error());
            }
            pos += AmbienceFXAmbientBed.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x40) != 0x0) {
            final int blockedAmbienceFxIndicesOffset = buffer.getIntLE(offset + 38);
            if (blockedAmbienceFxIndicesOffset < 0) {
                return ValidationResult.error("Invalid offset for BlockedAmbienceFxIndices");
            }
            int pos = offset + 42 + blockedAmbienceFxIndicesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for BlockedAmbienceFxIndices");
            }
            final int blockedAmbienceFxIndicesCount = VarInt.peek(buffer, pos);
            if (blockedAmbienceFxIndicesCount < 0) {
                return ValidationResult.error("Invalid array count for BlockedAmbienceFxIndices");
            }
            if (blockedAmbienceFxIndicesCount > 4096000) {
                return ValidationResult.error("BlockedAmbienceFxIndices exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += blockedAmbienceFxIndicesCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading BlockedAmbienceFxIndices");
            }
        }
        return ValidationResult.OK;
    }
    
    public AmbienceFX clone() {
        final AmbienceFX copy = new AmbienceFX();
        copy.id = this.id;
        copy.conditions = ((this.conditions != null) ? this.conditions.clone() : null);
        copy.sounds = (AmbienceFXSound[])((this.sounds != null) ? ((AmbienceFXSound[])Arrays.stream(this.sounds).map(e -> e.clone()).toArray(AmbienceFXSound[]::new)) : null);
        copy.music = ((this.music != null) ? this.music.clone() : null);
        copy.ambientBed = ((this.ambientBed != null) ? this.ambientBed.clone() : null);
        copy.soundEffect = ((this.soundEffect != null) ? this.soundEffect.clone() : null);
        copy.priority = this.priority;
        copy.blockedAmbienceFxIndices = (int[])((this.blockedAmbienceFxIndices != null) ? Arrays.copyOf(this.blockedAmbienceFxIndices, this.blockedAmbienceFxIndices.length) : null);
        copy.audioCategoryIndex = this.audioCategoryIndex;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AmbienceFX other) {
            return Objects.equals(this.id, other.id) && Objects.equals(this.conditions, other.conditions) && Arrays.equals(this.sounds, other.sounds) && Objects.equals(this.music, other.music) && Objects.equals(this.ambientBed, other.ambientBed) && Objects.equals(this.soundEffect, other.soundEffect) && this.priority == other.priority && Arrays.equals(this.blockedAmbienceFxIndices, other.blockedAmbienceFxIndices) && this.audioCategoryIndex == other.audioCategoryIndex;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Objects.hashCode(this.conditions);
        result = 31 * result + Arrays.hashCode(this.sounds);
        result = 31 * result + Objects.hashCode(this.music);
        result = 31 * result + Objects.hashCode(this.ambientBed);
        result = 31 * result + Objects.hashCode(this.soundEffect);
        result = 31 * result + Integer.hashCode(this.priority);
        result = 31 * result + Arrays.hashCode(this.blockedAmbienceFxIndices);
        result = 31 * result + Integer.hashCode(this.audioCategoryIndex);
        return result;
    }
}
