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

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