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

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

import java.util.Objects;
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 com.hypixel.hytale.protocol.Transform;
import javax.annotation.Nullable;

public class MapMarker
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 38;
    public static final int VARIABLE_FIELD_COUNT = 4;
    public static final int VARIABLE_BLOCK_START = 54;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public String name;
    @Nullable
    public String markerImage;
    @Nullable
    public Transform transform;
    @Nullable
    public ContextMenuItem[] contextMenuItems;
    
    public MapMarker() {
    }
    
    public MapMarker(@Nullable final String id, @Nullable final String name, @Nullable final String markerImage, @Nullable final Transform transform, @Nullable final ContextMenuItem[] contextMenuItems) {
        this.id = id;
        this.name = name;
        this.markerImage = markerImage;
        this.transform = transform;
        this.contextMenuItems = contextMenuItems;
    }
    
    public MapMarker(@Nonnull final MapMarker other) {
        this.id = other.id;
        this.name = other.name;
        this.markerImage = other.markerImage;
        this.transform = other.transform;
        this.contextMenuItems = other.contextMenuItems;
    }
    
    @Nonnull
    public static MapMarker deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final MapMarker obj = new MapMarker();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.transform = Transform.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 54 + buf.getIntLE(offset + 38);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos2 = offset + 54 + buf.getIntLE(offset + 42);
            final int nameLen = VarInt.peek(buf, varPos2);
            if (nameLen < 0) {
                throw ProtocolException.negativeLength("Name", nameLen);
            }
            if (nameLen > 4096000) {
                throw ProtocolException.stringTooLong("Name", nameLen, 4096000);
            }
            obj.name = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos3 = offset + 54 + buf.getIntLE(offset + 46);
            final int markerImageLen = VarInt.peek(buf, varPos3);
            if (markerImageLen < 0) {
                throw ProtocolException.negativeLength("MarkerImage", markerImageLen);
            }
            if (markerImageLen > 4096000) {
                throw ProtocolException.stringTooLong("MarkerImage", markerImageLen, 4096000);
            }
            obj.markerImage = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos4 = offset + 54 + buf.getIntLE(offset + 50);
            final int contextMenuItemsCount = VarInt.peek(buf, varPos4);
            if (contextMenuItemsCount < 0) {
                throw ProtocolException.negativeLength("ContextMenuItems", contextMenuItemsCount);
            }
            if (contextMenuItemsCount > 4096000) {
                throw ProtocolException.arrayTooLong("ContextMenuItems", contextMenuItemsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            if (varPos4 + varIntLen + contextMenuItemsCount * 0L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("ContextMenuItems", varPos4 + varIntLen + contextMenuItemsCount * 0, buf.readableBytes());
            }
            obj.contextMenuItems = new ContextMenuItem[contextMenuItemsCount];
            int elemPos = varPos4 + varIntLen;
            for (int i = 0; i < contextMenuItemsCount; ++i) {
                obj.contextMenuItems[i] = ContextMenuItem.deserialize(buf, elemPos);
                elemPos += ContextMenuItem.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 54;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 38);
            int pos0 = offset + 54 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 42);
            int pos2 = offset + 54 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 46);
            int pos3 = offset + 54 + fieldOffset3;
            final int sl = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 50);
            int pos4 = offset + 54 + fieldOffset4;
            final int arrLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4);
            for (int i = 0; i < arrLen; ++i) {
                pos4 += ContextMenuItem.computeBytesConsumed(buf, pos4);
            }
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.transform != null) {
            nullBits |= 0x1;
        }
        if (this.id != null) {
            nullBits |= 0x2;
        }
        if (this.name != null) {
            nullBits |= 0x4;
        }
        if (this.markerImage != null) {
            nullBits |= 0x8;
        }
        if (this.contextMenuItems != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        if (this.transform != null) {
            this.transform.serialize(buf);
        }
        else {
            buf.writeZero(37);
        }
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int nameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int markerImageOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int contextMenuItemsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.name != null) {
            buf.setIntLE(nameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.name, 4096000);
        }
        else {
            buf.setIntLE(nameOffsetSlot, -1);
        }
        if (this.markerImage != null) {
            buf.setIntLE(markerImageOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.markerImage, 4096000);
        }
        else {
            buf.setIntLE(markerImageOffsetSlot, -1);
        }
        if (this.contextMenuItems != null) {
            buf.setIntLE(contextMenuItemsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.contextMenuItems.length > 4096000) {
                throw ProtocolException.arrayTooLong("ContextMenuItems", this.contextMenuItems.length, 4096000);
            }
            VarInt.write(buf, this.contextMenuItems.length);
            for (final ContextMenuItem item : this.contextMenuItems) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(contextMenuItemsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 54;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.name != null) {
            size += PacketIO.stringSize(this.name);
        }
        if (this.markerImage != null) {
            size += PacketIO.stringSize(this.markerImage);
        }
        if (this.contextMenuItems != null) {
            int contextMenuItemsSize = 0;
            for (final ContextMenuItem elem : this.contextMenuItems) {
                contextMenuItemsSize += elem.computeSize();
            }
            size += VarInt.size(this.contextMenuItems.length) + contextMenuItemsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 54) {
            return ValidationResult.error("Buffer too small: expected at least 54 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x2) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 38);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 54 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int nameOffset = buffer.getIntLE(offset + 42);
            if (nameOffset < 0) {
                return ValidationResult.error("Invalid offset for Name");
            }
            int pos = offset + 54 + nameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Name");
            }
            final int nameLen = VarInt.peek(buffer, pos);
            if (nameLen < 0) {
                return ValidationResult.error("Invalid string length for Name");
            }
            if (nameLen > 4096000) {
                return ValidationResult.error("Name exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += nameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Name");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int markerImageOffset = buffer.getIntLE(offset + 46);
            if (markerImageOffset < 0) {
                return ValidationResult.error("Invalid offset for MarkerImage");
            }
            int pos = offset + 54 + markerImageOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for MarkerImage");
            }
            final int markerImageLen = VarInt.peek(buffer, pos);
            if (markerImageLen < 0) {
                return ValidationResult.error("Invalid string length for MarkerImage");
            }
            if (markerImageLen > 4096000) {
                return ValidationResult.error("MarkerImage exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += markerImageLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading MarkerImage");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int contextMenuItemsOffset = buffer.getIntLE(offset + 50);
            if (contextMenuItemsOffset < 0) {
                return ValidationResult.error("Invalid offset for ContextMenuItems");
            }
            int pos = offset + 54 + contextMenuItemsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ContextMenuItems");
            }
            final int contextMenuItemsCount = VarInt.peek(buffer, pos);
            if (contextMenuItemsCount < 0) {
                return ValidationResult.error("Invalid array count for ContextMenuItems");
            }
            if (contextMenuItemsCount > 4096000) {
                return ValidationResult.error("ContextMenuItems exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < contextMenuItemsCount; ++i) {
                final ValidationResult structResult = ContextMenuItem.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid ContextMenuItem in ContextMenuItems[" + i + "]: " + structResult.error());
                }
                pos += ContextMenuItem.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public MapMarker clone() {
        final MapMarker copy = new MapMarker();
        copy.id = this.id;
        copy.name = this.name;
        copy.markerImage = this.markerImage;
        copy.transform = ((this.transform != null) ? this.transform.clone() : null);
        copy.contextMenuItems = (ContextMenuItem[])((this.contextMenuItems != null) ? ((ContextMenuItem[])Arrays.stream(this.contextMenuItems).map(e -> e.clone()).toArray(ContextMenuItem[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final MapMarker other) {
            return Objects.equals(this.id, other.id) && Objects.equals(this.name, other.name) && Objects.equals(this.markerImage, other.markerImage) && Objects.equals(this.transform, other.transform) && Arrays.equals(this.contextMenuItems, other.contextMenuItems);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Objects.hashCode(this.name);
        result = 31 * result + Objects.hashCode(this.markerImage);
        result = 31 * result + Objects.hashCode(this.transform);
        result = 31 * result + Arrays.hashCode(this.contextMenuItems);
        return result;
    }
}
