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

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

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

public class OpenWindow implements Packet
{
    public static final int PACKET_ID = 200;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 6;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 18;
    public static final int MAX_SIZE = 1677721600;
    public int id;
    @Nonnull
    public WindowType windowType;
    @Nullable
    public String windowData;
    @Nullable
    public InventorySection inventory;
    @Nullable
    public ExtraResources extraResources;
    
    @Override
    public int getId() {
        return 200;
    }
    
    public OpenWindow() {
        this.windowType = WindowType.Container;
    }
    
    public OpenWindow(final int id, @Nonnull final WindowType windowType, @Nullable final String windowData, @Nullable final InventorySection inventory, @Nullable final ExtraResources extraResources) {
        this.windowType = WindowType.Container;
        this.id = id;
        this.windowType = windowType;
        this.windowData = windowData;
        this.inventory = inventory;
        this.extraResources = extraResources;
    }
    
    public OpenWindow(@Nonnull final OpenWindow other) {
        this.windowType = WindowType.Container;
        this.id = other.id;
        this.windowType = other.windowType;
        this.windowData = other.windowData;
        this.inventory = other.inventory;
        this.extraResources = other.extraResources;
    }
    
    @Nonnull
    public static OpenWindow deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final OpenWindow obj = new OpenWindow();
        final byte nullBits = buf.getByte(offset);
        obj.id = buf.getIntLE(offset + 1);
        obj.windowType = WindowType.fromValue(buf.getByte(offset + 5));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 18 + buf.getIntLE(offset + 6);
            final int windowDataLen = VarInt.peek(buf, varPos0);
            if (windowDataLen < 0) {
                throw ProtocolException.negativeLength("WindowData", windowDataLen);
            }
            if (windowDataLen > 4096000) {
                throw ProtocolException.stringTooLong("WindowData", windowDataLen, 4096000);
            }
            obj.windowData = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 18 + buf.getIntLE(offset + 10);
            obj.inventory = InventorySection.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 18 + buf.getIntLE(offset + 14);
            obj.extraResources = ExtraResources.deserialize(buf, varPos3);
        }
        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 + 6);
            int pos0 = offset + 18 + 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 + 10);
            int pos2 = offset + 18 + fieldOffset2;
            pos2 += InventorySection.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 14);
            int pos3 = offset + 18 + fieldOffset3;
            pos3 += ExtraResources.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.windowData != null) {
            nullBits |= 0x1;
        }
        if (this.inventory != null) {
            nullBits |= 0x2;
        }
        if (this.extraResources != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.id);
        buf.writeByte(this.windowType.getValue());
        final int windowDataOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int inventoryOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int extraResourcesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.windowData != null) {
            buf.setIntLE(windowDataOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.windowData, 4096000);
        }
        else {
            buf.setIntLE(windowDataOffsetSlot, -1);
        }
        if (this.inventory != null) {
            buf.setIntLE(inventoryOffsetSlot, buf.writerIndex() - varBlockStart);
            this.inventory.serialize(buf);
        }
        else {
            buf.setIntLE(inventoryOffsetSlot, -1);
        }
        if (this.extraResources != null) {
            buf.setIntLE(extraResourcesOffsetSlot, buf.writerIndex() - varBlockStart);
            this.extraResources.serialize(buf);
        }
        else {
            buf.setIntLE(extraResourcesOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 18;
        if (this.windowData != null) {
            size += PacketIO.stringSize(this.windowData);
        }
        if (this.inventory != null) {
            size += this.inventory.computeSize();
        }
        if (this.extraResources != null) {
            size += this.extraResources.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 windowDataOffset = buffer.getIntLE(offset + 6);
            if (windowDataOffset < 0) {
                return ValidationResult.error("Invalid offset for WindowData");
            }
            int pos = offset + 18 + windowDataOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for WindowData");
            }
            final int windowDataLen = VarInt.peek(buffer, pos);
            if (windowDataLen < 0) {
                return ValidationResult.error("Invalid string length for WindowData");
            }
            if (windowDataLen > 4096000) {
                return ValidationResult.error("WindowData exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += windowDataLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading WindowData");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int inventoryOffset = buffer.getIntLE(offset + 10);
            if (inventoryOffset < 0) {
                return ValidationResult.error("Invalid offset for Inventory");
            }
            int pos = offset + 18 + inventoryOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Inventory");
            }
            final ValidationResult inventoryResult = InventorySection.validateStructure(buffer, pos);
            if (!inventoryResult.isValid()) {
                return ValidationResult.error("Invalid Inventory: " + inventoryResult.error());
            }
            pos += InventorySection.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int extraResourcesOffset = buffer.getIntLE(offset + 14);
            if (extraResourcesOffset < 0) {
                return ValidationResult.error("Invalid offset for ExtraResources");
            }
            int pos = offset + 18 + extraResourcesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ExtraResources");
            }
            final ValidationResult extraResourcesResult = ExtraResources.validateStructure(buffer, pos);
            if (!extraResourcesResult.isValid()) {
                return ValidationResult.error("Invalid ExtraResources: " + extraResourcesResult.error());
            }
            pos += ExtraResources.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public OpenWindow clone() {
        final OpenWindow copy = new OpenWindow();
        copy.id = this.id;
        copy.windowType = this.windowType;
        copy.windowData = this.windowData;
        copy.inventory = ((this.inventory != null) ? this.inventory.clone() : null);
        copy.extraResources = ((this.extraResources != null) ? this.extraResources.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final OpenWindow other) {
            return this.id == other.id && Objects.equals(this.windowType, other.windowType) && Objects.equals(this.windowData, other.windowData) && Objects.equals(this.inventory, other.inventory) && Objects.equals(this.extraResources, other.extraResources);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.windowType, this.windowData, this.inventory, this.extraResources);
    }
}
