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

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

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.DamageCause;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.Vector3d;
import com.hypixel.hytale.protocol.Packet;

public class DamageInfo implements Packet
{
    public static final int PACKET_ID = 112;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 29;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 29;
    public static final int MAX_SIZE = 32768048;
    @Nullable
    public Vector3d damageSourcePosition;
    public float damageAmount;
    @Nullable
    public DamageCause damageCause;
    
    @Override
    public int getId() {
        return 112;
    }
    
    public DamageInfo() {
    }
    
    public DamageInfo(@Nullable final Vector3d damageSourcePosition, final float damageAmount, @Nullable final DamageCause damageCause) {
        this.damageSourcePosition = damageSourcePosition;
        this.damageAmount = damageAmount;
        this.damageCause = damageCause;
    }
    
    public DamageInfo(@Nonnull final DamageInfo other) {
        this.damageSourcePosition = other.damageSourcePosition;
        this.damageAmount = other.damageAmount;
        this.damageCause = other.damageCause;
    }
    
    @Nonnull
    public static DamageInfo deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final DamageInfo obj = new DamageInfo();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.damageSourcePosition = Vector3d.deserialize(buf, offset + 1);
        }
        obj.damageAmount = buf.getFloatLE(offset + 25);
        int pos = offset + 29;
        if ((nullBits & 0x2) != 0x0) {
            obj.damageCause = DamageCause.deserialize(buf, pos);
            pos += DamageCause.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 + 29;
        if ((nullBits & 0x2) != 0x0) {
            pos += DamageCause.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.damageSourcePosition != null) {
            nullBits |= 0x1;
        }
        if (this.damageCause != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        if (this.damageSourcePosition != null) {
            this.damageSourcePosition.serialize(buf);
        }
        else {
            buf.writeZero(24);
        }
        buf.writeFloatLE(this.damageAmount);
        if (this.damageCause != null) {
            this.damageCause.serialize(buf);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 29;
        if (this.damageCause != null) {
            size += this.damageCause.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 29) {
            return ValidationResult.error("Buffer too small: expected at least 29 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 29;
        if ((nullBits & 0x2) != 0x0) {
            final ValidationResult damageCauseResult = DamageCause.validateStructure(buffer, pos);
            if (!damageCauseResult.isValid()) {
                return ValidationResult.error("Invalid DamageCause: " + damageCauseResult.error());
            }
            pos += DamageCause.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public DamageInfo clone() {
        final DamageInfo copy = new DamageInfo();
        copy.damageSourcePosition = ((this.damageSourcePosition != null) ? this.damageSourcePosition.clone() : null);
        copy.damageAmount = this.damageAmount;
        copy.damageCause = ((this.damageCause != null) ? this.damageCause.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final DamageInfo other) {
            return Objects.equals(this.damageSourcePosition, other.damageSourcePosition) && this.damageAmount == other.damageAmount && Objects.equals(this.damageCause, other.damageCause);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.damageSourcePosition, this.damageAmount, this.damageCause);
    }
}
