// 
// 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 CameraSettings
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 13;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 21;
    public static final int MAX_SIZE = 8192049;
    @Nullable
    public Vector3f positionOffset;
    @Nullable
    public CameraAxis yaw;
    @Nullable
    public CameraAxis pitch;
    
    public CameraSettings() {
    }
    
    public CameraSettings(@Nullable final Vector3f positionOffset, @Nullable final CameraAxis yaw, @Nullable final CameraAxis pitch) {
        this.positionOffset = positionOffset;
        this.yaw = yaw;
        this.pitch = pitch;
    }
    
    public CameraSettings(@Nonnull final CameraSettings other) {
        this.positionOffset = other.positionOffset;
        this.yaw = other.yaw;
        this.pitch = other.pitch;
    }
    
    @Nonnull
    public static CameraSettings deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final CameraSettings obj = new CameraSettings();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.positionOffset = Vector3f.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 21 + buf.getIntLE(offset + 13);
            obj.yaw = CameraAxis.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos2 = offset + 21 + buf.getIntLE(offset + 17);
            obj.pitch = CameraAxis.deserialize(buf, varPos2);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 21;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 13);
            int pos0 = offset + 21 + fieldOffset0;
            pos0 += CameraAxis.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 17);
            int pos2 = offset + 21 + fieldOffset2;
            pos2 += CameraAxis.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.positionOffset != null) {
            nullBits |= 0x1;
        }
        if (this.yaw != null) {
            nullBits |= 0x2;
        }
        if (this.pitch != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        if (this.positionOffset != null) {
            this.positionOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        final int yawOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int pitchOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.yaw != null) {
            buf.setIntLE(yawOffsetSlot, buf.writerIndex() - varBlockStart);
            this.yaw.serialize(buf);
        }
        else {
            buf.setIntLE(yawOffsetSlot, -1);
        }
        if (this.pitch != null) {
            buf.setIntLE(pitchOffsetSlot, buf.writerIndex() - varBlockStart);
            this.pitch.serialize(buf);
        }
        else {
            buf.setIntLE(pitchOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 21;
        if (this.yaw != null) {
            size += this.yaw.computeSize();
        }
        if (this.pitch != null) {
            size += this.pitch.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 21) {
            return ValidationResult.error("Buffer too small: expected at least 21 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x2) != 0x0) {
            final int yawOffset = buffer.getIntLE(offset + 13);
            if (yawOffset < 0) {
                return ValidationResult.error("Invalid offset for Yaw");
            }
            int pos = offset + 21 + yawOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Yaw");
            }
            final ValidationResult yawResult = CameraAxis.validateStructure(buffer, pos);
            if (!yawResult.isValid()) {
                return ValidationResult.error("Invalid Yaw: " + yawResult.error());
            }
            pos += CameraAxis.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int pitchOffset = buffer.getIntLE(offset + 17);
            if (pitchOffset < 0) {
                return ValidationResult.error("Invalid offset for Pitch");
            }
            int pos = offset + 21 + pitchOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Pitch");
            }
            final ValidationResult pitchResult = CameraAxis.validateStructure(buffer, pos);
            if (!pitchResult.isValid()) {
                return ValidationResult.error("Invalid Pitch: " + pitchResult.error());
            }
            pos += CameraAxis.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public CameraSettings clone() {
        final CameraSettings copy = new CameraSettings();
        copy.positionOffset = ((this.positionOffset != null) ? this.positionOffset.clone() : null);
        copy.yaw = ((this.yaw != null) ? this.yaw.clone() : null);
        copy.pitch = ((this.pitch != null) ? this.pitch.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final CameraSettings other) {
            return Objects.equals(this.positionOffset, other.positionOffset) && Objects.equals(this.yaw, other.yaw) && Objects.equals(this.pitch, other.pitch);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.positionOffset, this.yaw, this.pitch);
    }
}
