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

public class BlockMount
{
    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;
    @Nonnull
    public BlockMountType type;
    @Nullable
    public Vector3f position;
    @Nullable
    public Vector3f orientation;
    public int blockTypeId;
    
    public BlockMount() {
        this.type = BlockMountType.Seat;
    }
    
    public BlockMount(@Nonnull final BlockMountType type, @Nullable final Vector3f position, @Nullable final Vector3f orientation, final int blockTypeId) {
        this.type = BlockMountType.Seat;
        this.type = type;
        this.position = position;
        this.orientation = orientation;
        this.blockTypeId = blockTypeId;
    }
    
    public BlockMount(@Nonnull final BlockMount other) {
        this.type = BlockMountType.Seat;
        this.type = other.type;
        this.position = other.position;
        this.orientation = other.orientation;
        this.blockTypeId = other.blockTypeId;
    }
    
    @Nonnull
    public static BlockMount deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockMount obj = new BlockMount();
        final byte nullBits = buf.getByte(offset);
        obj.type = BlockMountType.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            obj.position = Vector3f.deserialize(buf, offset + 2);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.orientation = Vector3f.deserialize(buf, offset + 14);
        }
        obj.blockTypeId = buf.getIntLE(offset + 26);
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 30;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.position != null) {
            nullBits |= 0x1;
        }
        if (this.orientation != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        if (this.position != null) {
            this.position.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.orientation != null) {
            this.orientation.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        buf.writeIntLE(this.blockTypeId);
    }
    
    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 BlockMount clone() {
        final BlockMount copy = new BlockMount();
        copy.type = this.type;
        copy.position = ((this.position != null) ? this.position.clone() : null);
        copy.orientation = ((this.orientation != null) ? this.orientation.clone() : null);
        copy.blockTypeId = this.blockTypeId;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockMount other) {
            return Objects.equals(this.type, other.type) && Objects.equals(this.position, other.position) && Objects.equals(this.orientation, other.orientation) && this.blockTypeId == other.blockTypeId;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.type, this.position, this.orientation, this.blockTypeId);
    }
}
