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

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

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

public class ClientTeleport implements Packet
{
    public static final int PACKET_ID = 109;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 52;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 52;
    public static final int MAX_SIZE = 52;
    public byte teleportId;
    @Nullable
    public ModelTransform modelTransform;
    public boolean resetVelocity;
    
    @Override
    public int getId() {
        return 109;
    }
    
    public ClientTeleport() {
    }
    
    public ClientTeleport(final byte teleportId, @Nullable final ModelTransform modelTransform, final boolean resetVelocity) {
        this.teleportId = teleportId;
        this.modelTransform = modelTransform;
        this.resetVelocity = resetVelocity;
    }
    
    public ClientTeleport(@Nonnull final ClientTeleport other) {
        this.teleportId = other.teleportId;
        this.modelTransform = other.modelTransform;
        this.resetVelocity = other.resetVelocity;
    }
    
    @Nonnull
    public static ClientTeleport deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ClientTeleport obj = new ClientTeleport();
        final byte nullBits = buf.getByte(offset);
        obj.teleportId = buf.getByte(offset + 1);
        if ((nullBits & 0x1) != 0x0) {
            obj.modelTransform = ModelTransform.deserialize(buf, offset + 2);
        }
        obj.resetVelocity = (buf.getByte(offset + 51) != 0);
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 52;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.modelTransform != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.teleportId);
        if (this.modelTransform != null) {
            this.modelTransform.serialize(buf);
        }
        else {
            buf.writeZero(49);
        }
        buf.writeByte(this.resetVelocity ? 1 : 0);
    }
    
    @Override
    public int computeSize() {
        return 52;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 52) {
            return ValidationResult.error("Buffer too small: expected at least 52 bytes");
        }
        return ValidationResult.OK;
    }
    
    public ClientTeleport clone() {
        final ClientTeleport copy = new ClientTeleport();
        copy.teleportId = this.teleportId;
        copy.modelTransform = ((this.modelTransform != null) ? this.modelTransform.clone() : null);
        copy.resetVelocity = this.resetVelocity;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ClientTeleport other) {
            return this.teleportId == other.teleportId && Objects.equals(this.modelTransform, other.modelTransform) && this.resetVelocity == other.resetVelocity;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.teleportId, this.modelTransform, this.resetVelocity);
    }
}
