// 
// 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 SetChunk implements Packet
{
    public static final int PACKET_ID = 131;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 13;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 25;
    public static final int MAX_SIZE = 12288040;
    public int x;
    public int y;
    public int z;
    @Nullable
    public byte[] localLight;
    @Nullable
    public byte[] globalLight;
    @Nullable
    public byte[] data;
    
    @Override
    public int getId() {
        return 131;
    }
    
    public SetChunk() {
    }
    
    public SetChunk(final int x, final int y, final int z, @Nullable final byte[] localLight, @Nullable final byte[] globalLight, @Nullable final byte[] data) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.localLight = localLight;
        this.globalLight = globalLight;
        this.data = data;
    }
    
    public SetChunk(@Nonnull final SetChunk other) {
        this.x = other.x;
        this.y = other.y;
        this.z = other.z;
        this.localLight = other.localLight;
        this.globalLight = other.globalLight;
        this.data = other.data;
    }
    
    @Nonnull
    public static SetChunk deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SetChunk obj = new SetChunk();
        final byte nullBits = buf.getByte(offset);
        obj.x = buf.getIntLE(offset + 1);
        obj.y = buf.getIntLE(offset + 5);
        obj.z = buf.getIntLE(offset + 9);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 25 + buf.getIntLE(offset + 13);
            final int localLightCount = VarInt.peek(buf, varPos0);
            if (localLightCount < 0) {
                throw ProtocolException.negativeLength("LocalLight", localLightCount);
            }
            if (localLightCount > 4096000) {
                throw ProtocolException.arrayTooLong("LocalLight", localLightCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + localLightCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("LocalLight", varPos0 + varIntLen + localLightCount * 1, buf.readableBytes());
            }
            obj.localLight = new byte[localLightCount];
            for (int i = 0; i < localLightCount; ++i) {
                obj.localLight[i] = buf.getByte(varPos0 + varIntLen + i * 1);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 25 + buf.getIntLE(offset + 17);
            final int globalLightCount = VarInt.peek(buf, varPos2);
            if (globalLightCount < 0) {
                throw ProtocolException.negativeLength("GlobalLight", globalLightCount);
            }
            if (globalLightCount > 4096000) {
                throw ProtocolException.arrayTooLong("GlobalLight", globalLightCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + globalLightCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("GlobalLight", varPos2 + varIntLen + globalLightCount * 1, buf.readableBytes());
            }
            obj.globalLight = new byte[globalLightCount];
            for (int i = 0; i < globalLightCount; ++i) {
                obj.globalLight[i] = buf.getByte(varPos2 + varIntLen + i * 1);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 25 + buf.getIntLE(offset + 21);
            final int dataCount = VarInt.peek(buf, varPos3);
            if (dataCount < 0) {
                throw ProtocolException.negativeLength("Data", dataCount);
            }
            if (dataCount > 4096000) {
                throw ProtocolException.arrayTooLong("Data", dataCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + dataCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Data", varPos3 + varIntLen + dataCount * 1, buf.readableBytes());
            }
            obj.data = new byte[dataCount];
            for (int i = 0; i < dataCount; ++i) {
                obj.data[i] = buf.getByte(varPos3 + varIntLen + i * 1);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 25;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 13);
            int pos0 = offset + 25 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + arrLen * 1;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 17);
            int pos2 = offset + 25 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + arrLen * 1;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 21);
            int pos3 = offset + 25 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + arrLen * 1;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.localLight != null) {
            nullBits |= 0x1;
        }
        if (this.globalLight != null) {
            nullBits |= 0x2;
        }
        if (this.data != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.x);
        buf.writeIntLE(this.y);
        buf.writeIntLE(this.z);
        final int localLightOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int globalLightOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int dataOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.localLight != null) {
            buf.setIntLE(localLightOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.localLight.length > 4096000) {
                throw ProtocolException.arrayTooLong("LocalLight", this.localLight.length, 4096000);
            }
            VarInt.write(buf, this.localLight.length);
            for (final byte item : this.localLight) {
                buf.writeByte(item);
            }
        }
        else {
            buf.setIntLE(localLightOffsetSlot, -1);
        }
        if (this.globalLight != null) {
            buf.setIntLE(globalLightOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.globalLight.length > 4096000) {
                throw ProtocolException.arrayTooLong("GlobalLight", this.globalLight.length, 4096000);
            }
            VarInt.write(buf, this.globalLight.length);
            for (final byte item : this.globalLight) {
                buf.writeByte(item);
            }
        }
        else {
            buf.setIntLE(globalLightOffsetSlot, -1);
        }
        if (this.data != null) {
            buf.setIntLE(dataOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.data.length > 4096000) {
                throw ProtocolException.arrayTooLong("Data", this.data.length, 4096000);
            }
            VarInt.write(buf, this.data.length);
            for (final byte item : this.data) {
                buf.writeByte(item);
            }
        }
        else {
            buf.setIntLE(dataOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 25;
        if (this.localLight != null) {
            size += VarInt.size(this.localLight.length) + this.localLight.length * 1;
        }
        if (this.globalLight != null) {
            size += VarInt.size(this.globalLight.length) + this.globalLight.length * 1;
        }
        if (this.data != null) {
            size += VarInt.size(this.data.length) + this.data.length * 1;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 25) {
            return ValidationResult.error("Buffer too small: expected at least 25 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int localLightOffset = buffer.getIntLE(offset + 13);
            if (localLightOffset < 0) {
                return ValidationResult.error("Invalid offset for LocalLight");
            }
            int pos = offset + 25 + localLightOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for LocalLight");
            }
            final int localLightCount = VarInt.peek(buffer, pos);
            if (localLightCount < 0) {
                return ValidationResult.error("Invalid array count for LocalLight");
            }
            if (localLightCount > 4096000) {
                return ValidationResult.error("LocalLight exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += localLightCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading LocalLight");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int globalLightOffset = buffer.getIntLE(offset + 17);
            if (globalLightOffset < 0) {
                return ValidationResult.error("Invalid offset for GlobalLight");
            }
            int pos = offset + 25 + globalLightOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for GlobalLight");
            }
            final int globalLightCount = VarInt.peek(buffer, pos);
            if (globalLightCount < 0) {
                return ValidationResult.error("Invalid array count for GlobalLight");
            }
            if (globalLightCount > 4096000) {
                return ValidationResult.error("GlobalLight exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += globalLightCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading GlobalLight");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int dataOffset = buffer.getIntLE(offset + 21);
            if (dataOffset < 0) {
                return ValidationResult.error("Invalid offset for Data");
            }
            int pos = offset + 25 + dataOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Data");
            }
            final int dataCount = VarInt.peek(buffer, pos);
            if (dataCount < 0) {
                return ValidationResult.error("Invalid array count for Data");
            }
            if (dataCount > 4096000) {
                return ValidationResult.error("Data exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += dataCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Data");
            }
        }
        return ValidationResult.OK;
    }
    
    public SetChunk clone() {
        final SetChunk copy = new SetChunk();
        copy.x = this.x;
        copy.y = this.y;
        copy.z = this.z;
        copy.localLight = (byte[])((this.localLight != null) ? Arrays.copyOf(this.localLight, this.localLight.length) : null);
        copy.globalLight = (byte[])((this.globalLight != null) ? Arrays.copyOf(this.globalLight, this.globalLight.length) : null);
        copy.data = (byte[])((this.data != null) ? Arrays.copyOf(this.data, this.data.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SetChunk other) {
            return this.x == other.x && this.y == other.y && this.z == other.z && Arrays.equals(this.localLight, other.localLight) && Arrays.equals(this.globalLight, other.globalLight) && Arrays.equals(this.data, other.data);
        }
        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.localLight);
        result = 31 * result + Arrays.hashCode(this.globalLight);
        result = 31 * result + Arrays.hashCode(this.data);
        return result;
    }
}
