// 
// 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.Nonnull;
import javax.annotation.Nullable;

public class SelectedHitEntity
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 53;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 53;
    public static final int MAX_SIZE = 53;
    public int networkId;
    @Nullable
    public Vector3f hitLocation;
    @Nullable
    public Position position;
    @Nullable
    public Direction bodyRotation;
    
    public SelectedHitEntity() {
    }
    
    public SelectedHitEntity(final int networkId, @Nullable final Vector3f hitLocation, @Nullable final Position position, @Nullable final Direction bodyRotation) {
        this.networkId = networkId;
        this.hitLocation = hitLocation;
        this.position = position;
        this.bodyRotation = bodyRotation;
    }
    
    public SelectedHitEntity(@Nonnull final SelectedHitEntity other) {
        this.networkId = other.networkId;
        this.hitLocation = other.hitLocation;
        this.position = other.position;
        this.bodyRotation = other.bodyRotation;
    }
    
    @Nonnull
    public static SelectedHitEntity deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SelectedHitEntity obj = new SelectedHitEntity();
        final byte nullBits = buf.getByte(offset);
        obj.networkId = buf.getIntLE(offset + 1);
        if ((nullBits & 0x1) != 0x0) {
            obj.hitLocation = Vector3f.deserialize(buf, offset + 5);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.position = Position.deserialize(buf, offset + 17);
        }
        if ((nullBits & 0x4) != 0x0) {
            obj.bodyRotation = Direction.deserialize(buf, offset + 41);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 53;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.hitLocation != null) {
            nullBits |= 0x1;
        }
        if (this.position != null) {
            nullBits |= 0x2;
        }
        if (this.bodyRotation != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.networkId);
        if (this.hitLocation != null) {
            this.hitLocation.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.position != null) {
            this.position.serialize(buf);
        }
        else {
            buf.writeZero(24);
        }
        if (this.bodyRotation != null) {
            this.bodyRotation.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
    }
    
    public int computeSize() {
        return 53;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 53) {
            return ValidationResult.error("Buffer too small: expected at least 53 bytes");
        }
        return ValidationResult.OK;
    }
    
    public SelectedHitEntity clone() {
        final SelectedHitEntity copy = new SelectedHitEntity();
        copy.networkId = this.networkId;
        copy.hitLocation = ((this.hitLocation != null) ? this.hitLocation.clone() : null);
        copy.position = ((this.position != null) ? this.position.clone() : null);
        copy.bodyRotation = ((this.bodyRotation != null) ? this.bodyRotation.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SelectedHitEntity other) {
            return this.networkId == other.networkId && Objects.equals(this.hitLocation, other.hitLocation) && Objects.equals(this.position, other.position) && Objects.equals(this.bodyRotation, other.bodyRotation);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.networkId, this.hitLocation, this.position, this.bodyRotation);
    }
}
