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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.PacketIO;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class EntityStatType
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 14;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 26;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    public float value;
    public float min;
    public float max;
    @Nullable
    public EntityStatEffects minValueEffects;
    @Nullable
    public EntityStatEffects maxValueEffects;
    @Nonnull
    public EntityStatResetBehavior resetBehavior;
    
    public EntityStatType() {
        this.resetBehavior = EntityStatResetBehavior.InitialValue;
    }
    
    public EntityStatType(@Nullable final String id, final float value, final float min, final float max, @Nullable final EntityStatEffects minValueEffects, @Nullable final EntityStatEffects maxValueEffects, @Nonnull final EntityStatResetBehavior resetBehavior) {
        this.resetBehavior = EntityStatResetBehavior.InitialValue;
        this.id = id;
        this.value = value;
        this.min = min;
        this.max = max;
        this.minValueEffects = minValueEffects;
        this.maxValueEffects = maxValueEffects;
        this.resetBehavior = resetBehavior;
    }
    
    public EntityStatType(@Nonnull final EntityStatType other) {
        this.resetBehavior = EntityStatResetBehavior.InitialValue;
        this.id = other.id;
        this.value = other.value;
        this.min = other.min;
        this.max = other.max;
        this.minValueEffects = other.minValueEffects;
        this.maxValueEffects = other.maxValueEffects;
        this.resetBehavior = other.resetBehavior;
    }
    
    @Nonnull
    public static EntityStatType deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final EntityStatType obj = new EntityStatType();
        final byte nullBits = buf.getByte(offset);
        obj.value = buf.getFloatLE(offset + 1);
        obj.min = buf.getFloatLE(offset + 5);
        obj.max = buf.getFloatLE(offset + 9);
        obj.resetBehavior = EntityStatResetBehavior.fromValue(buf.getByte(offset + 13));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 26 + buf.getIntLE(offset + 14);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 26 + buf.getIntLE(offset + 18);
            obj.minValueEffects = EntityStatEffects.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 26 + buf.getIntLE(offset + 22);
            obj.maxValueEffects = EntityStatEffects.deserialize(buf, varPos3);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 26;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 14);
            int pos0 = offset + 26 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 18);
            int pos2 = offset + 26 + fieldOffset2;
            pos2 += EntityStatEffects.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 22);
            int pos3 = offset + 26 + fieldOffset3;
            pos3 += EntityStatEffects.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.id != null) {
            nullBits |= 0x1;
        }
        if (this.minValueEffects != null) {
            nullBits |= 0x2;
        }
        if (this.maxValueEffects != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeFloatLE(this.value);
        buf.writeFloatLE(this.min);
        buf.writeFloatLE(this.max);
        buf.writeByte(this.resetBehavior.getValue());
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int minValueEffectsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int maxValueEffectsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.minValueEffects != null) {
            buf.setIntLE(minValueEffectsOffsetSlot, buf.writerIndex() - varBlockStart);
            this.minValueEffects.serialize(buf);
        }
        else {
            buf.setIntLE(minValueEffectsOffsetSlot, -1);
        }
        if (this.maxValueEffects != null) {
            buf.setIntLE(maxValueEffectsOffsetSlot, buf.writerIndex() - varBlockStart);
            this.maxValueEffects.serialize(buf);
        }
        else {
            buf.setIntLE(maxValueEffectsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 26;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.minValueEffects != null) {
            size += this.minValueEffects.computeSize();
        }
        if (this.maxValueEffects != null) {
            size += this.maxValueEffects.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 26) {
            return ValidationResult.error("Buffer too small: expected at least 26 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 14);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 26 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int minValueEffectsOffset = buffer.getIntLE(offset + 18);
            if (minValueEffectsOffset < 0) {
                return ValidationResult.error("Invalid offset for MinValueEffects");
            }
            int pos = offset + 26 + minValueEffectsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for MinValueEffects");
            }
            final ValidationResult minValueEffectsResult = EntityStatEffects.validateStructure(buffer, pos);
            if (!minValueEffectsResult.isValid()) {
                return ValidationResult.error("Invalid MinValueEffects: " + minValueEffectsResult.error());
            }
            pos += EntityStatEffects.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int maxValueEffectsOffset = buffer.getIntLE(offset + 22);
            if (maxValueEffectsOffset < 0) {
                return ValidationResult.error("Invalid offset for MaxValueEffects");
            }
            int pos = offset + 26 + maxValueEffectsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for MaxValueEffects");
            }
            final ValidationResult maxValueEffectsResult = EntityStatEffects.validateStructure(buffer, pos);
            if (!maxValueEffectsResult.isValid()) {
                return ValidationResult.error("Invalid MaxValueEffects: " + maxValueEffectsResult.error());
            }
            pos += EntityStatEffects.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public EntityStatType clone() {
        final EntityStatType copy = new EntityStatType();
        copy.id = this.id;
        copy.value = this.value;
        copy.min = this.min;
        copy.max = this.max;
        copy.minValueEffects = ((this.minValueEffects != null) ? this.minValueEffects.clone() : null);
        copy.maxValueEffects = ((this.maxValueEffects != null) ? this.maxValueEffects.clone() : null);
        copy.resetBehavior = this.resetBehavior;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final EntityStatType other) {
            return Objects.equals(this.id, other.id) && this.value == other.value && this.min == other.min && this.max == other.max && Objects.equals(this.minValueEffects, other.minValueEffects) && Objects.equals(this.maxValueEffects, other.maxValueEffects) && Objects.equals(this.resetBehavior, other.resetBehavior);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.value, this.min, this.max, this.minValueEffects, this.maxValueEffects, this.resetBehavior);
    }
}
