// 
// 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 CameraShake
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 1130496177;
    @Nullable
    public CameraShakeConfig firstPerson;
    @Nullable
    public CameraShakeConfig thirdPerson;
    
    public CameraShake() {
    }
    
    public CameraShake(@Nullable final CameraShakeConfig firstPerson, @Nullable final CameraShakeConfig thirdPerson) {
        this.firstPerson = firstPerson;
        this.thirdPerson = thirdPerson;
    }
    
    public CameraShake(@Nonnull final CameraShake other) {
        this.firstPerson = other.firstPerson;
        this.thirdPerson = other.thirdPerson;
    }
    
    @Nonnull
    public static CameraShake deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final CameraShake obj = new CameraShake();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            obj.firstPerson = CameraShakeConfig.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            obj.thirdPerson = CameraShakeConfig.deserialize(buf, varPos2);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + fieldOffset0;
            pos0 += CameraShakeConfig.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 9 + fieldOffset2;
            pos2 += CameraShakeConfig.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.firstPerson != null) {
            nullBits |= 0x1;
        }
        if (this.thirdPerson != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int firstPersonOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int thirdPersonOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.firstPerson != null) {
            buf.setIntLE(firstPersonOffsetSlot, buf.writerIndex() - varBlockStart);
            this.firstPerson.serialize(buf);
        }
        else {
            buf.setIntLE(firstPersonOffsetSlot, -1);
        }
        if (this.thirdPerson != null) {
            buf.setIntLE(thirdPersonOffsetSlot, buf.writerIndex() - varBlockStart);
            this.thirdPerson.serialize(buf);
        }
        else {
            buf.setIntLE(thirdPersonOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.firstPerson != null) {
            size += this.firstPerson.computeSize();
        }
        if (this.thirdPerson != null) {
            size += this.thirdPerson.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int firstPersonOffset = buffer.getIntLE(offset + 1);
            if (firstPersonOffset < 0) {
                return ValidationResult.error("Invalid offset for FirstPerson");
            }
            int pos = offset + 9 + firstPersonOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for FirstPerson");
            }
            final ValidationResult firstPersonResult = CameraShakeConfig.validateStructure(buffer, pos);
            if (!firstPersonResult.isValid()) {
                return ValidationResult.error("Invalid FirstPerson: " + firstPersonResult.error());
            }
            pos += CameraShakeConfig.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int thirdPersonOffset = buffer.getIntLE(offset + 5);
            if (thirdPersonOffset < 0) {
                return ValidationResult.error("Invalid offset for ThirdPerson");
            }
            int pos = offset + 9 + thirdPersonOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ThirdPerson");
            }
            final ValidationResult thirdPersonResult = CameraShakeConfig.validateStructure(buffer, pos);
            if (!thirdPersonResult.isValid()) {
                return ValidationResult.error("Invalid ThirdPerson: " + thirdPersonResult.error());
            }
            pos += CameraShakeConfig.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public CameraShake clone() {
        final CameraShake copy = new CameraShake();
        copy.firstPerson = ((this.firstPerson != null) ? this.firstPerson.clone() : null);
        copy.thirdPerson = ((this.thirdPerson != null) ? this.thirdPerson.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final CameraShake other) {
            return Objects.equals(this.firstPerson, other.firstPerson) && Objects.equals(this.thirdPerson, other.thirdPerson);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.firstPerson, this.thirdPerson);
    }
}
