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

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

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 UpdateMachinimaScene implements Packet
{
    public static final int PACKET_ID = 262;
    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 = 36864033;
    @Nullable
    public String player;
    @Nullable
    public String sceneName;
    public float frame;
    @Nonnull
    public SceneUpdateType updateType;
    @Nullable
    public byte[] scene;
    
    @Override
    public int getId() {
        return 262;
    }
    
    public UpdateMachinimaScene() {
        this.updateType = SceneUpdateType.Update;
    }
    
    public UpdateMachinimaScene(@Nullable final String player, @Nullable final String sceneName, final float frame, @Nonnull final SceneUpdateType updateType, @Nullable final byte[] scene) {
        this.updateType = SceneUpdateType.Update;
        this.player = player;
        this.sceneName = sceneName;
        this.frame = frame;
        this.updateType = updateType;
        this.scene = scene;
    }
    
    public UpdateMachinimaScene(@Nonnull final UpdateMachinimaScene other) {
        this.updateType = SceneUpdateType.Update;
        this.player = other.player;
        this.sceneName = other.sceneName;
        this.frame = other.frame;
        this.updateType = other.updateType;
        this.scene = other.scene;
    }
    
    @Nonnull
    public static UpdateMachinimaScene deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateMachinimaScene obj = new UpdateMachinimaScene();
        final byte nullBits = buf.getByte(offset);
        obj.frame = buf.getFloatLE(offset + 1);
        obj.updateType = SceneUpdateType.fromValue(buf.getByte(offset + 5));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 18 + buf.getIntLE(offset + 6);
            final int playerLen = VarInt.peek(buf, varPos0);
            if (playerLen < 0) {
                throw ProtocolException.negativeLength("Player", playerLen);
            }
            if (playerLen > 4096000) {
                throw ProtocolException.stringTooLong("Player", playerLen, 4096000);
            }
            obj.player = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 18 + buf.getIntLE(offset + 10);
            final int sceneNameLen = VarInt.peek(buf, varPos2);
            if (sceneNameLen < 0) {
                throw ProtocolException.negativeLength("SceneName", sceneNameLen);
            }
            if (sceneNameLen > 4096000) {
                throw ProtocolException.stringTooLong("SceneName", sceneNameLen, 4096000);
            }
            obj.sceneName = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 18 + buf.getIntLE(offset + 14);
            final int sceneCount = VarInt.peek(buf, varPos3);
            if (sceneCount < 0) {
                throw ProtocolException.negativeLength("Scene", sceneCount);
            }
            if (sceneCount > 4096000) {
                throw ProtocolException.arrayTooLong("Scene", sceneCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + sceneCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Scene", varPos3 + varIntLen + sceneCount * 1, buf.readableBytes());
            }
            obj.scene = new byte[sceneCount];
            for (int i = 0; i < sceneCount; ++i) {
                obj.scene[i] = buf.getByte(varPos3 + varIntLen + i * 1);
            }
        }
        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;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 14);
            int pos3 = offset + 18 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + arrLen * 1;
            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.player != null) {
            nullBits |= 0x1;
        }
        if (this.sceneName != null) {
            nullBits |= 0x2;
        }
        if (this.scene != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeFloatLE(this.frame);
        buf.writeByte(this.updateType.getValue());
        final int playerOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int sceneNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int sceneOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.player != null) {
            buf.setIntLE(playerOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.player, 4096000);
        }
        else {
            buf.setIntLE(playerOffsetSlot, -1);
        }
        if (this.sceneName != null) {
            buf.setIntLE(sceneNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.sceneName, 4096000);
        }
        else {
            buf.setIntLE(sceneNameOffsetSlot, -1);
        }
        if (this.scene != null) {
            buf.setIntLE(sceneOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.scene.length > 4096000) {
                throw ProtocolException.arrayTooLong("Scene", this.scene.length, 4096000);
            }
            VarInt.write(buf, this.scene.length);
            for (final byte item : this.scene) {
                buf.writeByte(item);
            }
        }
        else {
            buf.setIntLE(sceneOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 18;
        if (this.player != null) {
            size += PacketIO.stringSize(this.player);
        }
        if (this.sceneName != null) {
            size += PacketIO.stringSize(this.sceneName);
        }
        if (this.scene != null) {
            size += VarInt.size(this.scene.length) + this.scene.length * 1;
        }
        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 playerOffset = buffer.getIntLE(offset + 6);
            if (playerOffset < 0) {
                return ValidationResult.error("Invalid offset for Player");
            }
            int pos = offset + 18 + playerOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Player");
            }
            final int playerLen = VarInt.peek(buffer, pos);
            if (playerLen < 0) {
                return ValidationResult.error("Invalid string length for Player");
            }
            if (playerLen > 4096000) {
                return ValidationResult.error("Player exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += playerLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Player");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int sceneNameOffset = buffer.getIntLE(offset + 10);
            if (sceneNameOffset < 0) {
                return ValidationResult.error("Invalid offset for SceneName");
            }
            int pos = offset + 18 + sceneNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for SceneName");
            }
            final int sceneNameLen = VarInt.peek(buffer, pos);
            if (sceneNameLen < 0) {
                return ValidationResult.error("Invalid string length for SceneName");
            }
            if (sceneNameLen > 4096000) {
                return ValidationResult.error("SceneName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += sceneNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading SceneName");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int sceneOffset = buffer.getIntLE(offset + 14);
            if (sceneOffset < 0) {
                return ValidationResult.error("Invalid offset for Scene");
            }
            int pos = offset + 18 + sceneOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Scene");
            }
            final int sceneCount = VarInt.peek(buffer, pos);
            if (sceneCount < 0) {
                return ValidationResult.error("Invalid array count for Scene");
            }
            if (sceneCount > 4096000) {
                return ValidationResult.error("Scene exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += sceneCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Scene");
            }
        }
        return ValidationResult.OK;
    }
    
    public UpdateMachinimaScene clone() {
        final UpdateMachinimaScene copy = new UpdateMachinimaScene();
        copy.player = this.player;
        copy.sceneName = this.sceneName;
        copy.frame = this.frame;
        copy.updateType = this.updateType;
        copy.scene = (byte[])((this.scene != null) ? Arrays.copyOf(this.scene, this.scene.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateMachinimaScene other) {
            return Objects.equals(this.player, other.player) && Objects.equals(this.sceneName, other.sceneName) && this.frame == other.frame && Objects.equals(this.updateType, other.updateType) && Arrays.equals(this.scene, other.scene);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.player);
        result = 31 * result + Objects.hashCode(this.sceneName);
        result = 31 * result + Float.hashCode(this.frame);
        result = 31 * result + Objects.hashCode(this.updateType);
        result = 31 * result + Arrays.hashCode(this.scene);
        return result;
    }
}
