// 
// 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 AngledDamage
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 21;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 21;
    public static final int MAX_SIZE = 1677721600;
    public double angle;
    public double angleDistance;
    @Nullable
    public DamageEffects damageEffects;
    public int next;
    
    public AngledDamage() {
    }
    
    public AngledDamage(final double angle, final double angleDistance, @Nullable final DamageEffects damageEffects, final int next) {
        this.angle = angle;
        this.angleDistance = angleDistance;
        this.damageEffects = damageEffects;
        this.next = next;
    }
    
    public AngledDamage(@Nonnull final AngledDamage other) {
        this.angle = other.angle;
        this.angleDistance = other.angleDistance;
        this.damageEffects = other.damageEffects;
        this.next = other.next;
    }
    
    @Nonnull
    public static AngledDamage deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AngledDamage obj = new AngledDamage();
        final byte nullBits = buf.getByte(offset);
        obj.angle = buf.getDoubleLE(offset + 1);
        obj.angleDistance = buf.getDoubleLE(offset + 9);
        obj.next = buf.getIntLE(offset + 17);
        int pos = offset + 21;
        if ((nullBits & 0x1) != 0x0) {
            obj.damageEffects = DamageEffects.deserialize(buf, pos);
            pos += DamageEffects.computeBytesConsumed(buf, pos);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 21;
        if ((nullBits & 0x1) != 0x0) {
            pos += DamageEffects.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.damageEffects != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeDoubleLE(this.angle);
        buf.writeDoubleLE(this.angleDistance);
        buf.writeIntLE(this.next);
        if (this.damageEffects != null) {
            this.damageEffects.serialize(buf);
        }
    }
    
    public int computeSize() {
        int size = 21;
        if (this.damageEffects != null) {
            size += this.damageEffects.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);
        int pos = offset + 21;
        if ((nullBits & 0x1) != 0x0) {
            final ValidationResult damageEffectsResult = DamageEffects.validateStructure(buffer, pos);
            if (!damageEffectsResult.isValid()) {
                return ValidationResult.error("Invalid DamageEffects: " + damageEffectsResult.error());
            }
            pos += DamageEffects.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public AngledDamage clone() {
        final AngledDamage copy = new AngledDamage();
        copy.angle = this.angle;
        copy.angleDistance = this.angleDistance;
        copy.damageEffects = ((this.damageEffects != null) ? this.damageEffects.clone() : null);
        copy.next = this.next;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AngledDamage other) {
            return this.angle == other.angle && this.angleDistance == other.angleDistance && Objects.equals(this.damageEffects, other.damageEffects) && this.next == other.next;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.angle, this.angleDistance, this.damageEffects, this.next);
    }
}
