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

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

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

public class SetServerCamera implements Packet
{
    public static final int PACKET_ID = 280;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 157;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 157;
    public static final int MAX_SIZE = 157;
    @Nonnull
    public ClientCameraView clientCameraView;
    public boolean isLocked;
    @Nullable
    public ServerCameraSettings cameraSettings;
    
    @Override
    public int getId() {
        return 280;
    }
    
    public SetServerCamera() {
        this.clientCameraView = ClientCameraView.FirstPerson;
    }
    
    public SetServerCamera(@Nonnull final ClientCameraView clientCameraView, final boolean isLocked, @Nullable final ServerCameraSettings cameraSettings) {
        this.clientCameraView = ClientCameraView.FirstPerson;
        this.clientCameraView = clientCameraView;
        this.isLocked = isLocked;
        this.cameraSettings = cameraSettings;
    }
    
    public SetServerCamera(@Nonnull final SetServerCamera other) {
        this.clientCameraView = ClientCameraView.FirstPerson;
        this.clientCameraView = other.clientCameraView;
        this.isLocked = other.isLocked;
        this.cameraSettings = other.cameraSettings;
    }
    
    @Nonnull
    public static SetServerCamera deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SetServerCamera obj = new SetServerCamera();
        final byte nullBits = buf.getByte(offset);
        obj.clientCameraView = ClientCameraView.fromValue(buf.getByte(offset + 1));
        obj.isLocked = (buf.getByte(offset + 2) != 0);
        if ((nullBits & 0x1) != 0x0) {
            obj.cameraSettings = ServerCameraSettings.deserialize(buf, offset + 3);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 157;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.cameraSettings != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.clientCameraView.getValue());
        buf.writeByte(this.isLocked ? 1 : 0);
        if (this.cameraSettings != null) {
            this.cameraSettings.serialize(buf);
        }
        else {
            buf.writeZero(154);
        }
    }
    
    @Override
    public int computeSize() {
        return 157;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 157) {
            return ValidationResult.error("Buffer too small: expected at least 157 bytes");
        }
        return ValidationResult.OK;
    }
    
    public SetServerCamera clone() {
        final SetServerCamera copy = new SetServerCamera();
        copy.clientCameraView = this.clientCameraView;
        copy.isLocked = this.isLocked;
        copy.cameraSettings = ((this.cameraSettings != null) ? this.cameraSettings.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SetServerCamera other) {
            return Objects.equals(this.clientCameraView, other.clientCameraView) && this.isLocked == other.isLocked && Objects.equals(this.cameraSettings, other.cameraSettings);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.clientCameraView, this.isLocked, this.cameraSettings);
    }
}
