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

package com.hypixel.hytale.protocol.packets.world;

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

public class SpawnBlockParticleSystem implements Packet
{
    public static final int PACKET_ID = 153;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 30;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 30;
    public static final int MAX_SIZE = 30;
    public int blockId;
    @Nonnull
    public BlockParticleEvent particleType;
    @Nullable
    public Position position;
    
    @Override
    public int getId() {
        return 153;
    }
    
    public SpawnBlockParticleSystem() {
        this.particleType = BlockParticleEvent.Walk;
    }
    
    public SpawnBlockParticleSystem(final int blockId, @Nonnull final BlockParticleEvent particleType, @Nullable final Position position) {
        this.particleType = BlockParticleEvent.Walk;
        this.blockId = blockId;
        this.particleType = particleType;
        this.position = position;
    }
    
    public SpawnBlockParticleSystem(@Nonnull final SpawnBlockParticleSystem other) {
        this.particleType = BlockParticleEvent.Walk;
        this.blockId = other.blockId;
        this.particleType = other.particleType;
        this.position = other.position;
    }
    
    @Nonnull
    public static SpawnBlockParticleSystem deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SpawnBlockParticleSystem obj = new SpawnBlockParticleSystem();
        final byte nullBits = buf.getByte(offset);
        obj.blockId = buf.getIntLE(offset + 1);
        obj.particleType = BlockParticleEvent.fromValue(buf.getByte(offset + 5));
        if ((nullBits & 0x1) != 0x0) {
            obj.position = Position.deserialize(buf, offset + 6);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 30;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.position != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.blockId);
        buf.writeByte(this.particleType.getValue());
        if (this.position != null) {
            this.position.serialize(buf);
        }
        else {
            buf.writeZero(24);
        }
    }
    
    @Override
    public int computeSize() {
        return 30;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 30) {
            return ValidationResult.error("Buffer too small: expected at least 30 bytes");
        }
        return ValidationResult.OK;
    }
    
    public SpawnBlockParticleSystem clone() {
        final SpawnBlockParticleSystem copy = new SpawnBlockParticleSystem();
        copy.blockId = this.blockId;
        copy.particleType = this.particleType;
        copy.position = ((this.position != null) ? this.position.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SpawnBlockParticleSystem other) {
            return this.blockId == other.blockId && Objects.equals(this.particleType, other.particleType) && Objects.equals(this.position, other.position);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.blockId, this.particleType, this.position);
    }
}
