// 
// 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 javax.annotation.Nullable;
import com.hypixel.hytale.protocol.Packet;

public class SetChunkHeightmap implements Packet
{
    public static final int PACKET_ID = 132;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 4096014;
    public int x;
    public int z;
    @Nullable
    public byte[] heightmap;
    
    @Override
    public int getId() {
        return 132;
    }
    
    public SetChunkHeightmap() {
    }
    
    public SetChunkHeightmap(final int x, final int z, @Nullable final byte[] heightmap) {
        this.x = x;
        this.z = z;
        this.heightmap = heightmap;
    }
    
    public SetChunkHeightmap(@Nonnull final SetChunkHeightmap other) {
        this.x = other.x;
        this.z = other.z;
        this.heightmap = other.heightmap;
    }
    
    @Nonnull
    public static SetChunkHeightmap deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SetChunkHeightmap obj = new SetChunkHeightmap();
        final byte nullBits = buf.getByte(offset);
        obj.x = buf.getIntLE(offset + 1);
        obj.z = buf.getIntLE(offset + 5);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int heightmapCount = VarInt.peek(buf, pos);
            if (heightmapCount < 0) {
                throw ProtocolException.negativeLength("Heightmap", heightmapCount);
            }
            if (heightmapCount > 4096000) {
                throw ProtocolException.arrayTooLong("Heightmap", heightmapCount, 4096000);
            }
            final int heightmapVarLen = VarInt.size(heightmapCount);
            if (pos + heightmapVarLen + heightmapCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Heightmap", pos + heightmapVarLen + heightmapCount * 1, buf.readableBytes());
            }
            pos += heightmapVarLen;
            obj.heightmap = new byte[heightmapCount];
            for (int i = 0; i < heightmapCount; ++i) {
                obj.heightmap[i] = buf.getByte(pos + i * 1);
            }
            pos += heightmapCount * 1;
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int arrLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos) + arrLen * 1;
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.heightmap != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.x);
        buf.writeIntLE(this.z);
        if (this.heightmap != null) {
            if (this.heightmap.length > 4096000) {
                throw ProtocolException.arrayTooLong("Heightmap", this.heightmap.length, 4096000);
            }
            VarInt.write(buf, this.heightmap.length);
            for (final byte item : this.heightmap) {
                buf.writeByte(item);
            }
        }
    }
    
    @Override
    public int computeSize() {
        int size = 9;
        if (this.heightmap != null) {
            size += VarInt.size(this.heightmap.length) + this.heightmap.length * 1;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int heightmapCount = VarInt.peek(buffer, pos);
            if (heightmapCount < 0) {
                return ValidationResult.error("Invalid array count for Heightmap");
            }
            if (heightmapCount > 4096000) {
                return ValidationResult.error("Heightmap exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += heightmapCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Heightmap");
            }
        }
        return ValidationResult.OK;
    }
    
    public SetChunkHeightmap clone() {
        final SetChunkHeightmap copy = new SetChunkHeightmap();
        copy.x = this.x;
        copy.z = this.z;
        copy.heightmap = (byte[])((this.heightmap != null) ? Arrays.copyOf(this.heightmap, this.heightmap.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SetChunkHeightmap other) {
            return this.x == other.x && this.z == other.z && Arrays.equals(this.heightmap, other.heightmap);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Integer.hashCode(this.x);
        result = 31 * result + Integer.hashCode(this.z);
        result = 31 * result + Arrays.hashCode(this.heightmap);
        return result;
    }
}
