// 
// 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 FluidParticle
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 8;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 8;
    public static final int MAX_SIZE = 16384013;
    @Nullable
    public String systemId;
    @Nullable
    public Color color;
    public float scale;
    
    public FluidParticle() {
    }
    
    public FluidParticle(@Nullable final String systemId, @Nullable final Color color, final float scale) {
        this.systemId = systemId;
        this.color = color;
        this.scale = scale;
    }
    
    public FluidParticle(@Nonnull final FluidParticle other) {
        this.systemId = other.systemId;
        this.color = other.color;
        this.scale = other.scale;
    }
    
    @Nonnull
    public static FluidParticle deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final FluidParticle obj = new FluidParticle();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.color = Color.deserialize(buf, offset + 1);
        }
        obj.scale = buf.getFloatLE(offset + 4);
        int pos = offset + 8;
        if ((nullBits & 0x2) != 0x0) {
            final int systemIdLen = VarInt.peek(buf, pos);
            if (systemIdLen < 0) {
                throw ProtocolException.negativeLength("SystemId", systemIdLen);
            }
            if (systemIdLen > 4096000) {
                throw ProtocolException.stringTooLong("SystemId", systemIdLen, 4096000);
            }
            final int systemIdVarLen = VarInt.length(buf, pos);
            obj.systemId = PacketIO.readVarString(buf, pos, PacketIO.UTF8);
            pos += systemIdVarLen + systemIdLen;
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 8;
        if ((nullBits & 0x2) != 0x0) {
            final int sl = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos) + sl;
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.color != null) {
            nullBits |= 0x1;
        }
        if (this.systemId != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        if (this.color != null) {
            this.color.serialize(buf);
        }
        else {
            buf.writeZero(3);
        }
        buf.writeFloatLE(this.scale);
        if (this.systemId != null) {
            PacketIO.writeVarString(buf, this.systemId, 4096000);
        }
    }
    
    public int computeSize() {
        int size = 8;
        if (this.systemId != null) {
            size += PacketIO.stringSize(this.systemId);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 8) {
            return ValidationResult.error("Buffer too small: expected at least 8 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 8;
        if ((nullBits & 0x2) != 0x0) {
            final int systemIdLen = VarInt.peek(buffer, pos);
            if (systemIdLen < 0) {
                return ValidationResult.error("Invalid string length for SystemId");
            }
            if (systemIdLen > 4096000) {
                return ValidationResult.error("SystemId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += systemIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading SystemId");
            }
        }
        return ValidationResult.OK;
    }
    
    public FluidParticle clone() {
        final FluidParticle copy = new FluidParticle();
        copy.systemId = this.systemId;
        copy.color = ((this.color != null) ? this.color.clone() : null);
        copy.scale = this.scale;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final FluidParticle other) {
            return Objects.equals(this.systemId, other.systemId) && Objects.equals(this.color, other.color) && this.scale == other.scale;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.systemId, this.color, this.scale);
    }
}
