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

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

import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.Packet;

public class ServerSetFluids implements Packet
{
    public static final int PACKET_ID = 143;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 0;
    public static final int FIXED_BLOCK_SIZE = 12;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 12;
    public static final int MAX_SIZE = 28672017;
    public int x;
    public int y;
    public int z;
    @Nonnull
    public SetFluidCmd[] cmds;
    
    @Override
    public int getId() {
        return 143;
    }
    
    public ServerSetFluids() {
        this.cmds = new SetFluidCmd[0];
    }
    
    public ServerSetFluids(final int x, final int y, final int z, @Nonnull final SetFluidCmd[] cmds) {
        this.cmds = new SetFluidCmd[0];
        this.x = x;
        this.y = y;
        this.z = z;
        this.cmds = cmds;
    }
    
    public ServerSetFluids(@Nonnull final ServerSetFluids other) {
        this.cmds = new SetFluidCmd[0];
        this.x = other.x;
        this.y = other.y;
        this.z = other.z;
        this.cmds = other.cmds;
    }
    
    @Nonnull
    public static ServerSetFluids deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ServerSetFluids obj = new ServerSetFluids();
        obj.x = buf.getIntLE(offset + 0);
        obj.y = buf.getIntLE(offset + 4);
        obj.z = buf.getIntLE(offset + 8);
        int pos = offset + 12;
        final int cmdsCount = VarInt.peek(buf, pos);
        if (cmdsCount < 0) {
            throw ProtocolException.negativeLength("Cmds", cmdsCount);
        }
        if (cmdsCount > 4096000) {
            throw ProtocolException.arrayTooLong("Cmds", cmdsCount, 4096000);
        }
        final int cmdsVarLen = VarInt.size(cmdsCount);
        if (pos + cmdsVarLen + cmdsCount * 7L > buf.readableBytes()) {
            throw ProtocolException.bufferTooSmall("Cmds", pos + cmdsVarLen + cmdsCount * 7, buf.readableBytes());
        }
        pos += cmdsVarLen;
        obj.cmds = new SetFluidCmd[cmdsCount];
        for (int i = 0; i < cmdsCount; ++i) {
            obj.cmds[i] = SetFluidCmd.deserialize(buf, pos);
            pos += SetFluidCmd.computeBytesConsumed(buf, pos);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        int pos = offset + 12;
        final int arrLen = VarInt.peek(buf, pos);
        pos += VarInt.length(buf, pos);
        for (int i = 0; i < arrLen; ++i) {
            pos += SetFluidCmd.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        buf.writeIntLE(this.x);
        buf.writeIntLE(this.y);
        buf.writeIntLE(this.z);
        if (this.cmds.length > 4096000) {
            throw ProtocolException.arrayTooLong("Cmds", this.cmds.length, 4096000);
        }
        VarInt.write(buf, this.cmds.length);
        for (final SetFluidCmd item : this.cmds) {
            item.serialize(buf);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 12;
        size += VarInt.size(this.cmds.length) + this.cmds.length * 7;
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 12) {
            return ValidationResult.error("Buffer too small: expected at least 12 bytes");
        }
        int pos = offset + 12;
        final int cmdsCount = VarInt.peek(buffer, pos);
        if (cmdsCount < 0) {
            return ValidationResult.error("Invalid array count for Cmds");
        }
        if (cmdsCount > 4096000) {
            return ValidationResult.error("Cmds exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        pos += cmdsCount * 7;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading Cmds");
        }
        return ValidationResult.OK;
    }
    
    public ServerSetFluids clone() {
        final ServerSetFluids copy = new ServerSetFluids();
        copy.x = this.x;
        copy.y = this.y;
        copy.z = this.z;
        copy.cmds = Arrays.stream(this.cmds).map(e -> e.clone()).toArray(SetFluidCmd[]::new);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ServerSetFluids other) {
            return this.x == other.x && this.y == other.y && this.z == other.z && Arrays.equals(this.cmds, other.cmds);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Integer.hashCode(this.x);
        result = 31 * result + Integer.hashCode(this.y);
        result = 31 * result + Integer.hashCode(this.z);
        result = 31 * result + Arrays.hashCode(this.cmds);
        return result;
    }
}
