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

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

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

public class Notification implements Packet
{
    public static final int PACKET_ID = 212;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 4;
    public static final int VARIABLE_BLOCK_START = 18;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public FormattedMessage message;
    @Nullable
    public FormattedMessage secondaryMessage;
    @Nullable
    public String icon;
    @Nullable
    public ItemWithAllMetadata item;
    @Nonnull
    public NotificationStyle style;
    
    @Override
    public int getId() {
        return 212;
    }
    
    public Notification() {
        this.style = NotificationStyle.Default;
    }
    
    public Notification(@Nullable final FormattedMessage message, @Nullable final FormattedMessage secondaryMessage, @Nullable final String icon, @Nullable final ItemWithAllMetadata item, @Nonnull final NotificationStyle style) {
        this.style = NotificationStyle.Default;
        this.message = message;
        this.secondaryMessage = secondaryMessage;
        this.icon = icon;
        this.item = item;
        this.style = style;
    }
    
    public Notification(@Nonnull final Notification other) {
        this.style = NotificationStyle.Default;
        this.message = other.message;
        this.secondaryMessage = other.secondaryMessage;
        this.icon = other.icon;
        this.item = other.item;
        this.style = other.style;
    }
    
    @Nonnull
    public static Notification deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Notification obj = new Notification();
        final byte nullBits = buf.getByte(offset);
        obj.style = NotificationStyle.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 18 + buf.getIntLE(offset + 2);
            obj.message = FormattedMessage.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 18 + buf.getIntLE(offset + 6);
            obj.secondaryMessage = FormattedMessage.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 18 + buf.getIntLE(offset + 10);
            final int iconLen = VarInt.peek(buf, varPos3);
            if (iconLen < 0) {
                throw ProtocolException.negativeLength("Icon", iconLen);
            }
            if (iconLen > 4096000) {
                throw ProtocolException.stringTooLong("Icon", iconLen, 4096000);
            }
            obj.icon = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 18 + buf.getIntLE(offset + 14);
            obj.item = ItemWithAllMetadata.deserialize(buf, varPos4);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 18;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 2);
            int pos0 = offset + 18 + fieldOffset0;
            pos0 += FormattedMessage.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 6);
            int pos2 = offset + 18 + fieldOffset2;
            pos2 += FormattedMessage.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 10);
            int pos3 = offset + 18 + fieldOffset3;
            final int sl = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 14);
            int pos4 = offset + 18 + fieldOffset4;
            pos4 += ItemWithAllMetadata.computeBytesConsumed(buf, pos4);
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.message != null) {
            nullBits |= 0x1;
        }
        if (this.secondaryMessage != null) {
            nullBits |= 0x2;
        }
        if (this.icon != null) {
            nullBits |= 0x4;
        }
        if (this.item != null) {
            nullBits |= 0x8;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.style.getValue());
        final int messageOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int secondaryMessageOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int iconOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int itemOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.message != null) {
            buf.setIntLE(messageOffsetSlot, buf.writerIndex() - varBlockStart);
            this.message.serialize(buf);
        }
        else {
            buf.setIntLE(messageOffsetSlot, -1);
        }
        if (this.secondaryMessage != null) {
            buf.setIntLE(secondaryMessageOffsetSlot, buf.writerIndex() - varBlockStart);
            this.secondaryMessage.serialize(buf);
        }
        else {
            buf.setIntLE(secondaryMessageOffsetSlot, -1);
        }
        if (this.icon != null) {
            buf.setIntLE(iconOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.icon, 4096000);
        }
        else {
            buf.setIntLE(iconOffsetSlot, -1);
        }
        if (this.item != null) {
            buf.setIntLE(itemOffsetSlot, buf.writerIndex() - varBlockStart);
            this.item.serialize(buf);
        }
        else {
            buf.setIntLE(itemOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 18;
        if (this.message != null) {
            size += this.message.computeSize();
        }
        if (this.secondaryMessage != null) {
            size += this.secondaryMessage.computeSize();
        }
        if (this.icon != null) {
            size += PacketIO.stringSize(this.icon);
        }
        if (this.item != null) {
            size += this.item.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 18) {
            return ValidationResult.error("Buffer too small: expected at least 18 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int messageOffset = buffer.getIntLE(offset + 2);
            if (messageOffset < 0) {
                return ValidationResult.error("Invalid offset for Message");
            }
            int pos = offset + 18 + messageOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Message");
            }
            final ValidationResult messageResult = FormattedMessage.validateStructure(buffer, pos);
            if (!messageResult.isValid()) {
                return ValidationResult.error("Invalid Message: " + messageResult.error());
            }
            pos += FormattedMessage.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int secondaryMessageOffset = buffer.getIntLE(offset + 6);
            if (secondaryMessageOffset < 0) {
                return ValidationResult.error("Invalid offset for SecondaryMessage");
            }
            int pos = offset + 18 + secondaryMessageOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for SecondaryMessage");
            }
            final ValidationResult secondaryMessageResult = FormattedMessage.validateStructure(buffer, pos);
            if (!secondaryMessageResult.isValid()) {
                return ValidationResult.error("Invalid SecondaryMessage: " + secondaryMessageResult.error());
            }
            pos += FormattedMessage.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int iconOffset = buffer.getIntLE(offset + 10);
            if (iconOffset < 0) {
                return ValidationResult.error("Invalid offset for Icon");
            }
            int pos = offset + 18 + iconOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Icon");
            }
            final int iconLen = VarInt.peek(buffer, pos);
            if (iconLen < 0) {
                return ValidationResult.error("Invalid string length for Icon");
            }
            if (iconLen > 4096000) {
                return ValidationResult.error("Icon exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += iconLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Icon");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int itemOffset = buffer.getIntLE(offset + 14);
            if (itemOffset < 0) {
                return ValidationResult.error("Invalid offset for Item");
            }
            int pos = offset + 18 + itemOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Item");
            }
            final ValidationResult itemResult = ItemWithAllMetadata.validateStructure(buffer, pos);
            if (!itemResult.isValid()) {
                return ValidationResult.error("Invalid Item: " + itemResult.error());
            }
            pos += ItemWithAllMetadata.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public Notification clone() {
        final Notification copy = new Notification();
        copy.message = ((this.message != null) ? this.message.clone() : null);
        copy.secondaryMessage = ((this.secondaryMessage != null) ? this.secondaryMessage.clone() : null);
        copy.icon = this.icon;
        copy.item = ((this.item != null) ? this.item.clone() : null);
        copy.style = this.style;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Notification other) {
            return Objects.equals(this.message, other.message) && Objects.equals(this.secondaryMessage, other.secondaryMessage) && Objects.equals(this.icon, other.icon) && Objects.equals(this.item, other.item) && Objects.equals(this.style, other.style);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.message, this.secondaryMessage, this.icon, this.item, this.style);
    }
}
