// 
// 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.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class MapImage
{
    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 = 16384014;
    public int width;
    public int height;
    @Nullable
    public int[] data;
    
    public MapImage() {
    }
    
    public MapImage(final int width, final int height, @Nullable final int[] data) {
        this.width = width;
        this.height = height;
        this.data = data;
    }
    
    public MapImage(@Nonnull final MapImage other) {
        this.width = other.width;
        this.height = other.height;
        this.data = other.data;
    }
    
    @Nonnull
    public static MapImage deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final MapImage obj = new MapImage();
        final byte nullBits = buf.getByte(offset);
        obj.width = buf.getIntLE(offset + 1);
        obj.height = buf.getIntLE(offset + 5);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int dataCount = VarInt.peek(buf, pos);
            if (dataCount < 0) {
                throw ProtocolException.negativeLength("Data", dataCount);
            }
            if (dataCount > 4096000) {
                throw ProtocolException.arrayTooLong("Data", dataCount, 4096000);
            }
            final int dataVarLen = VarInt.size(dataCount);
            if (pos + dataVarLen + dataCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Data", pos + dataVarLen + dataCount * 4, buf.readableBytes());
            }
            pos += dataVarLen;
            obj.data = new int[dataCount];
            for (int i = 0; i < dataCount; ++i) {
                obj.data[i] = buf.getIntLE(pos + i * 4);
            }
            pos += dataCount * 4;
        }
        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 * 4;
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.data != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.width);
        buf.writeIntLE(this.height);
        if (this.data != null) {
            if (this.data.length > 4096000) {
                throw ProtocolException.arrayTooLong("Data", this.data.length, 4096000);
            }
            VarInt.write(buf, this.data.length);
            for (final int item : this.data) {
                buf.writeIntLE(item);
            }
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.data != null) {
            size += VarInt.size(this.data.length) + this.data.length * 4;
        }
        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 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 * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Data");
            }
        }
        return ValidationResult.OK;
    }
    
    public MapImage clone() {
        final MapImage copy = new MapImage();
        copy.width = this.width;
        copy.height = this.height;
        copy.data = (int[])((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 MapImage other) {
            return this.width == other.width && this.height == other.height && Arrays.equals(this.data, other.data);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Integer.hashCode(this.width);
        result = 31 * result + Integer.hashCode(this.height);
        result = 31 * result + Arrays.hashCode(this.data);
        return result;
    }
}
