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

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

import java.util.Arrays;
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;
import com.hypixel.hytale.protocol.Packet;

public class UpdateWorldMap implements Packet
{
    public static final int PACKET_ID = 241;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public MapChunk[] chunks;
    @Nullable
    public MapMarker[] addedMarkers;
    @Nullable
    public String[] removedMarkers;
    
    @Override
    public int getId() {
        return 241;
    }
    
    public UpdateWorldMap() {
    }
    
    public UpdateWorldMap(@Nullable final MapChunk[] chunks, @Nullable final MapMarker[] addedMarkers, @Nullable final String[] removedMarkers) {
        this.chunks = chunks;
        this.addedMarkers = addedMarkers;
        this.removedMarkers = removedMarkers;
    }
    
    public UpdateWorldMap(@Nonnull final UpdateWorldMap other) {
        this.chunks = other.chunks;
        this.addedMarkers = other.addedMarkers;
        this.removedMarkers = other.removedMarkers;
    }
    
    @Nonnull
    public static UpdateWorldMap deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateWorldMap obj = new UpdateWorldMap();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int chunksCount = VarInt.peek(buf, varPos0);
            if (chunksCount < 0) {
                throw ProtocolException.negativeLength("Chunks", chunksCount);
            }
            if (chunksCount > 4096000) {
                throw ProtocolException.arrayTooLong("Chunks", chunksCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + chunksCount * 9L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Chunks", varPos0 + varIntLen + chunksCount * 9, buf.readableBytes());
            }
            obj.chunks = new MapChunk[chunksCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < chunksCount; ++i) {
                obj.chunks[i] = MapChunk.deserialize(buf, elemPos);
                elemPos += MapChunk.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            final int addedMarkersCount = VarInt.peek(buf, varPos2);
            if (addedMarkersCount < 0) {
                throw ProtocolException.negativeLength("AddedMarkers", addedMarkersCount);
            }
            if (addedMarkersCount > 4096000) {
                throw ProtocolException.arrayTooLong("AddedMarkers", addedMarkersCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + addedMarkersCount * 38L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("AddedMarkers", varPos2 + varIntLen + addedMarkersCount * 38, buf.readableBytes());
            }
            obj.addedMarkers = new MapMarker[addedMarkersCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < addedMarkersCount; ++i) {
                obj.addedMarkers[i] = MapMarker.deserialize(buf, elemPos);
                elemPos += MapMarker.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            final int removedMarkersCount = VarInt.peek(buf, varPos3);
            if (removedMarkersCount < 0) {
                throw ProtocolException.negativeLength("RemovedMarkers", removedMarkersCount);
            }
            if (removedMarkersCount > 4096000) {
                throw ProtocolException.arrayTooLong("RemovedMarkers", removedMarkersCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + removedMarkersCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("RemovedMarkers", varPos3 + varIntLen + removedMarkersCount * 1, buf.readableBytes());
            }
            obj.removedMarkers = new String[removedMarkersCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < removedMarkersCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("removedMarkers[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("removedMarkers[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.removedMarkers[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 13 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0);
            for (int i = 0; i < arrLen; ++i) {
                pos0 += MapChunk.computeBytesConsumed(buf, pos0);
            }
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 13 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += MapMarker.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 9);
            int pos3 = offset + 13 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < arrLen; ++i) {
                final int sl = VarInt.peek(buf, pos3);
                pos3 += VarInt.length(buf, pos3) + sl;
            }
            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.chunks != null) {
            nullBits |= 0x1;
        }
        if (this.addedMarkers != null) {
            nullBits |= 0x2;
        }
        if (this.removedMarkers != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int chunksOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int addedMarkersOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int removedMarkersOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.chunks != null) {
            buf.setIntLE(chunksOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.chunks.length > 4096000) {
                throw ProtocolException.arrayTooLong("Chunks", this.chunks.length, 4096000);
            }
            VarInt.write(buf, this.chunks.length);
            for (final MapChunk item : this.chunks) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(chunksOffsetSlot, -1);
        }
        if (this.addedMarkers != null) {
            buf.setIntLE(addedMarkersOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.addedMarkers.length > 4096000) {
                throw ProtocolException.arrayTooLong("AddedMarkers", this.addedMarkers.length, 4096000);
            }
            VarInt.write(buf, this.addedMarkers.length);
            for (final MapMarker item2 : this.addedMarkers) {
                item2.serialize(buf);
            }
        }
        else {
            buf.setIntLE(addedMarkersOffsetSlot, -1);
        }
        if (this.removedMarkers != null) {
            buf.setIntLE(removedMarkersOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.removedMarkers.length > 4096000) {
                throw ProtocolException.arrayTooLong("RemovedMarkers", this.removedMarkers.length, 4096000);
            }
            VarInt.write(buf, this.removedMarkers.length);
            for (final String item3 : this.removedMarkers) {
                PacketIO.writeVarString(buf, item3, 4096000);
            }
        }
        else {
            buf.setIntLE(removedMarkersOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 13;
        if (this.chunks != null) {
            int chunksSize = 0;
            for (final MapChunk elem : this.chunks) {
                chunksSize += elem.computeSize();
            }
            size += VarInt.size(this.chunks.length) + chunksSize;
        }
        if (this.addedMarkers != null) {
            int addedMarkersSize = 0;
            for (final MapMarker elem2 : this.addedMarkers) {
                addedMarkersSize += elem2.computeSize();
            }
            size += VarInt.size(this.addedMarkers.length) + addedMarkersSize;
        }
        if (this.removedMarkers != null) {
            int removedMarkersSize = 0;
            for (final String elem3 : this.removedMarkers) {
                removedMarkersSize += PacketIO.stringSize(elem3);
            }
            size += VarInt.size(this.removedMarkers.length) + removedMarkersSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int chunksOffset = buffer.getIntLE(offset + 1);
            if (chunksOffset < 0) {
                return ValidationResult.error("Invalid offset for Chunks");
            }
            int pos = offset + 13 + chunksOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Chunks");
            }
            final int chunksCount = VarInt.peek(buffer, pos);
            if (chunksCount < 0) {
                return ValidationResult.error("Invalid array count for Chunks");
            }
            if (chunksCount > 4096000) {
                return ValidationResult.error("Chunks exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < chunksCount; ++i) {
                final ValidationResult structResult = MapChunk.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid MapChunk in Chunks[" + i + "]: " + structResult.error());
                }
                pos += MapChunk.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int addedMarkersOffset = buffer.getIntLE(offset + 5);
            if (addedMarkersOffset < 0) {
                return ValidationResult.error("Invalid offset for AddedMarkers");
            }
            int pos = offset + 13 + addedMarkersOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AddedMarkers");
            }
            final int addedMarkersCount = VarInt.peek(buffer, pos);
            if (addedMarkersCount < 0) {
                return ValidationResult.error("Invalid array count for AddedMarkers");
            }
            if (addedMarkersCount > 4096000) {
                return ValidationResult.error("AddedMarkers exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < addedMarkersCount; ++i) {
                final ValidationResult structResult = MapMarker.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid MapMarker in AddedMarkers[" + i + "]: " + structResult.error());
                }
                pos += MapMarker.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int removedMarkersOffset = buffer.getIntLE(offset + 9);
            if (removedMarkersOffset < 0) {
                return ValidationResult.error("Invalid offset for RemovedMarkers");
            }
            int pos = offset + 13 + removedMarkersOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for RemovedMarkers");
            }
            final int removedMarkersCount = VarInt.peek(buffer, pos);
            if (removedMarkersCount < 0) {
                return ValidationResult.error("Invalid array count for RemovedMarkers");
            }
            if (removedMarkersCount > 4096000) {
                return ValidationResult.error("RemovedMarkers exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < removedMarkersCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in RemovedMarkers");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in RemovedMarkers");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public UpdateWorldMap clone() {
        final UpdateWorldMap copy = new UpdateWorldMap();
        copy.chunks = (MapChunk[])((this.chunks != null) ? ((MapChunk[])Arrays.stream(this.chunks).map(e -> e.clone()).toArray(MapChunk[]::new)) : null);
        copy.addedMarkers = (MapMarker[])((this.addedMarkers != null) ? ((MapMarker[])Arrays.stream(this.addedMarkers).map(e -> e.clone()).toArray(MapMarker[]::new)) : null);
        copy.removedMarkers = (String[])((this.removedMarkers != null) ? ((String[])Arrays.copyOf(this.removedMarkers, this.removedMarkers.length)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateWorldMap other) {
            return Arrays.equals(this.chunks, other.chunks) && Arrays.equals(this.addedMarkers, other.addedMarkers) && Arrays.equals(this.removedMarkers, other.removedMarkers);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.chunks);
        result = 31 * result + Arrays.hashCode(this.addedMarkers);
        result = 31 * result + Arrays.hashCode(this.removedMarkers);
        return result;
    }
}
