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

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

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

public class CustomPage implements Packet
{
    public static final int PACKET_ID = 218;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 4;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 16;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String key;
    public boolean isInitial;
    public boolean clear;
    @Nonnull
    public CustomPageLifetime lifetime;
    @Nullable
    public CustomUICommand[] commands;
    @Nullable
    public CustomUIEventBinding[] eventBindings;
    
    @Override
    public int getId() {
        return 218;
    }
    
    public CustomPage() {
        this.lifetime = CustomPageLifetime.CantClose;
    }
    
    public CustomPage(@Nullable final String key, final boolean isInitial, final boolean clear, @Nonnull final CustomPageLifetime lifetime, @Nullable final CustomUICommand[] commands, @Nullable final CustomUIEventBinding[] eventBindings) {
        this.lifetime = CustomPageLifetime.CantClose;
        this.key = key;
        this.isInitial = isInitial;
        this.clear = clear;
        this.lifetime = lifetime;
        this.commands = commands;
        this.eventBindings = eventBindings;
    }
    
    public CustomPage(@Nonnull final CustomPage other) {
        this.lifetime = CustomPageLifetime.CantClose;
        this.key = other.key;
        this.isInitial = other.isInitial;
        this.clear = other.clear;
        this.lifetime = other.lifetime;
        this.commands = other.commands;
        this.eventBindings = other.eventBindings;
    }
    
    @Nonnull
    public static CustomPage deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final CustomPage obj = new CustomPage();
        final byte nullBits = buf.getByte(offset);
        obj.isInitial = (buf.getByte(offset + 1) != 0);
        obj.clear = (buf.getByte(offset + 2) != 0);
        obj.lifetime = CustomPageLifetime.fromValue(buf.getByte(offset + 3));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 16 + buf.getIntLE(offset + 4);
            final int keyLen = VarInt.peek(buf, varPos0);
            if (keyLen < 0) {
                throw ProtocolException.negativeLength("Key", keyLen);
            }
            if (keyLen > 4096000) {
                throw ProtocolException.stringTooLong("Key", keyLen, 4096000);
            }
            obj.key = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 16 + buf.getIntLE(offset + 8);
            final int commandsCount = VarInt.peek(buf, varPos2);
            if (commandsCount < 0) {
                throw ProtocolException.negativeLength("Commands", commandsCount);
            }
            if (commandsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Commands", commandsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + commandsCount * 2L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Commands", varPos2 + varIntLen + commandsCount * 2, buf.readableBytes());
            }
            obj.commands = new CustomUICommand[commandsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < commandsCount; ++i) {
                obj.commands[i] = CustomUICommand.deserialize(buf, elemPos);
                elemPos += CustomUICommand.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 16 + buf.getIntLE(offset + 12);
            final int eventBindingsCount = VarInt.peek(buf, varPos3);
            if (eventBindingsCount < 0) {
                throw ProtocolException.negativeLength("EventBindings", eventBindingsCount);
            }
            if (eventBindingsCount > 4096000) {
                throw ProtocolException.arrayTooLong("EventBindings", eventBindingsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + eventBindingsCount * 3L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("EventBindings", varPos3 + varIntLen + eventBindingsCount * 3, buf.readableBytes());
            }
            obj.eventBindings = new CustomUIEventBinding[eventBindingsCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < eventBindingsCount; ++i) {
                obj.eventBindings[i] = CustomUIEventBinding.deserialize(buf, elemPos);
                elemPos += CustomUIEventBinding.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 16;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 4);
            int pos0 = offset + 16 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 8);
            int pos2 = offset + 16 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += CustomUICommand.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 12);
            int pos3 = offset + 16 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < arrLen; ++i) {
                pos3 += CustomUIEventBinding.computeBytesConsumed(buf, pos3);
            }
            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.key != null) {
            nullBits |= 0x1;
        }
        if (this.commands != null) {
            nullBits |= 0x2;
        }
        if (this.eventBindings != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.isInitial ? 1 : 0);
        buf.writeByte(this.clear ? 1 : 0);
        buf.writeByte(this.lifetime.getValue());
        final int keyOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int commandsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int eventBindingsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.key != null) {
            buf.setIntLE(keyOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.key, 4096000);
        }
        else {
            buf.setIntLE(keyOffsetSlot, -1);
        }
        if (this.commands != null) {
            buf.setIntLE(commandsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.commands.length > 4096000) {
                throw ProtocolException.arrayTooLong("Commands", this.commands.length, 4096000);
            }
            VarInt.write(buf, this.commands.length);
            for (final CustomUICommand item : this.commands) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(commandsOffsetSlot, -1);
        }
        if (this.eventBindings != null) {
            buf.setIntLE(eventBindingsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.eventBindings.length > 4096000) {
                throw ProtocolException.arrayTooLong("EventBindings", this.eventBindings.length, 4096000);
            }
            VarInt.write(buf, this.eventBindings.length);
            for (final CustomUIEventBinding item2 : this.eventBindings) {
                item2.serialize(buf);
            }
        }
        else {
            buf.setIntLE(eventBindingsOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 16;
        if (this.key != null) {
            size += PacketIO.stringSize(this.key);
        }
        if (this.commands != null) {
            int commandsSize = 0;
            for (final CustomUICommand elem : this.commands) {
                commandsSize += elem.computeSize();
            }
            size += VarInt.size(this.commands.length) + commandsSize;
        }
        if (this.eventBindings != null) {
            int eventBindingsSize = 0;
            for (final CustomUIEventBinding elem2 : this.eventBindings) {
                eventBindingsSize += elem2.computeSize();
            }
            size += VarInt.size(this.eventBindings.length) + eventBindingsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 16) {
            return ValidationResult.error("Buffer too small: expected at least 16 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int keyOffset = buffer.getIntLE(offset + 4);
            if (keyOffset < 0) {
                return ValidationResult.error("Invalid offset for Key");
            }
            int pos = offset + 16 + keyOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Key");
            }
            final int keyLen = VarInt.peek(buffer, pos);
            if (keyLen < 0) {
                return ValidationResult.error("Invalid string length for Key");
            }
            if (keyLen > 4096000) {
                return ValidationResult.error("Key exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += keyLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Key");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int commandsOffset = buffer.getIntLE(offset + 8);
            if (commandsOffset < 0) {
                return ValidationResult.error("Invalid offset for Commands");
            }
            int pos = offset + 16 + commandsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Commands");
            }
            final int commandsCount = VarInt.peek(buffer, pos);
            if (commandsCount < 0) {
                return ValidationResult.error("Invalid array count for Commands");
            }
            if (commandsCount > 4096000) {
                return ValidationResult.error("Commands exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < commandsCount; ++i) {
                final ValidationResult structResult = CustomUICommand.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid CustomUICommand in Commands[" + i + "]: " + structResult.error());
                }
                pos += CustomUICommand.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int eventBindingsOffset = buffer.getIntLE(offset + 12);
            if (eventBindingsOffset < 0) {
                return ValidationResult.error("Invalid offset for EventBindings");
            }
            int pos = offset + 16 + eventBindingsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for EventBindings");
            }
            final int eventBindingsCount = VarInt.peek(buffer, pos);
            if (eventBindingsCount < 0) {
                return ValidationResult.error("Invalid array count for EventBindings");
            }
            if (eventBindingsCount > 4096000) {
                return ValidationResult.error("EventBindings exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < eventBindingsCount; ++i) {
                final ValidationResult structResult = CustomUIEventBinding.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid CustomUIEventBinding in EventBindings[" + i + "]: " + structResult.error());
                }
                pos += CustomUIEventBinding.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public CustomPage clone() {
        final CustomPage copy = new CustomPage();
        copy.key = this.key;
        copy.isInitial = this.isInitial;
        copy.clear = this.clear;
        copy.lifetime = this.lifetime;
        copy.commands = (CustomUICommand[])((this.commands != null) ? ((CustomUICommand[])Arrays.stream(this.commands).map(e -> e.clone()).toArray(CustomUICommand[]::new)) : null);
        copy.eventBindings = (CustomUIEventBinding[])((this.eventBindings != null) ? ((CustomUIEventBinding[])Arrays.stream(this.eventBindings).map(e -> e.clone()).toArray(CustomUIEventBinding[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final CustomPage other) {
            return Objects.equals(this.key, other.key) && this.isInitial == other.isInitial && this.clear == other.clear && Objects.equals(this.lifetime, other.lifetime) && Arrays.equals(this.commands, other.commands) && Arrays.equals(this.eventBindings, other.eventBindings);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.key);
        result = 31 * result + Boolean.hashCode(this.isInitial);
        result = 31 * result + Boolean.hashCode(this.clear);
        result = 31 * result + Objects.hashCode(this.lifetime);
        result = 31 * result + Arrays.hashCode(this.commands);
        result = 31 * result + Arrays.hashCode(this.eventBindings);
        return result;
    }
}
