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

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

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.VelocityConfig;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.ChangeVelocityType;
import com.hypixel.hytale.protocol.Packet;

public class ChangeVelocity implements Packet
{
    public static final int PACKET_ID = 163;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 35;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 35;
    public static final int MAX_SIZE = 35;
    public float x;
    public float y;
    public float z;
    @Nonnull
    public ChangeVelocityType changeType;
    @Nullable
    public VelocityConfig config;
    
    @Override
    public int getId() {
        return 163;
    }
    
    public ChangeVelocity() {
        this.changeType = ChangeVelocityType.Add;
    }
    
    public ChangeVelocity(final float x, final float y, final float z, @Nonnull final ChangeVelocityType changeType, @Nullable final VelocityConfig config) {
        this.changeType = ChangeVelocityType.Add;
        this.x = x;
        this.y = y;
        this.z = z;
        this.changeType = changeType;
        this.config = config;
    }
    
    public ChangeVelocity(@Nonnull final ChangeVelocity other) {
        this.changeType = ChangeVelocityType.Add;
        this.x = other.x;
        this.y = other.y;
        this.z = other.z;
        this.changeType = other.changeType;
        this.config = other.config;
    }
    
    @Nonnull
    public static ChangeVelocity deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ChangeVelocity obj = new ChangeVelocity();
        final byte nullBits = buf.getByte(offset);
        obj.x = buf.getFloatLE(offset + 1);
        obj.y = buf.getFloatLE(offset + 5);
        obj.z = buf.getFloatLE(offset + 9);
        obj.changeType = ChangeVelocityType.fromValue(buf.getByte(offset + 13));
        if ((nullBits & 0x1) != 0x0) {
            obj.config = VelocityConfig.deserialize(buf, offset + 14);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 35;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.config != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeFloatLE(this.x);
        buf.writeFloatLE(this.y);
        buf.writeFloatLE(this.z);
        buf.writeByte(this.changeType.getValue());
        if (this.config != null) {
            this.config.serialize(buf);
        }
        else {
            buf.writeZero(21);
        }
    }
    
    @Override
    public int computeSize() {
        return 35;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 35) {
            return ValidationResult.error("Buffer too small: expected at least 35 bytes");
        }
        return ValidationResult.OK;
    }
    
    public ChangeVelocity clone() {
        final ChangeVelocity copy = new ChangeVelocity();
        copy.x = this.x;
        copy.y = this.y;
        copy.z = this.z;
        copy.changeType = this.changeType;
        copy.config = ((this.config != null) ? this.config.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ChangeVelocity other) {
            return this.x == other.x && this.y == other.y && this.z == other.z && Objects.equals(this.changeType, other.changeType) && Objects.equals(this.config, other.config);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.x, this.y, this.z, this.changeType, this.config);
    }
}
