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

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

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 javax.annotation.Nullable;

public class TimestampedAssetReference
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 49152033;
    @Nullable
    public AssetPath path;
    @Nullable
    public String timestamp;
    
    public TimestampedAssetReference() {
    }
    
    public TimestampedAssetReference(@Nullable final AssetPath path, @Nullable final String timestamp) {
        this.path = path;
        this.timestamp = timestamp;
    }
    
    public TimestampedAssetReference(@Nonnull final TimestampedAssetReference other) {
        this.path = other.path;
        this.timestamp = other.timestamp;
    }
    
    @Nonnull
    public static TimestampedAssetReference deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final TimestampedAssetReference obj = new TimestampedAssetReference();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            obj.path = AssetPath.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int timestampLen = VarInt.peek(buf, varPos2);
            if (timestampLen < 0) {
                throw ProtocolException.negativeLength("Timestamp", timestampLen);
            }
            if (timestampLen > 4096000) {
                throw ProtocolException.stringTooLong("Timestamp", timestampLen, 4096000);
            }
            obj.timestamp = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + fieldOffset0;
            pos0 += AssetPath.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 9 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.path != null) {
            nullBits |= 0x1;
        }
        if (this.timestamp != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int pathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int timestampOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.path != null) {
            buf.setIntLE(pathOffsetSlot, buf.writerIndex() - varBlockStart);
            this.path.serialize(buf);
        }
        else {
            buf.setIntLE(pathOffsetSlot, -1);
        }
        if (this.timestamp != null) {
            buf.setIntLE(timestampOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.timestamp, 4096000);
        }
        else {
            buf.setIntLE(timestampOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.path != null) {
            size += this.path.computeSize();
        }
        if (this.timestamp != null) {
            size += PacketIO.stringSize(this.timestamp);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int pathOffset = buffer.getIntLE(offset + 1);
            if (pathOffset < 0) {
                return ValidationResult.error("Invalid offset for Path");
            }
            int pos = offset + 9 + pathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Path");
            }
            final ValidationResult pathResult = AssetPath.validateStructure(buffer, pos);
            if (!pathResult.isValid()) {
                return ValidationResult.error("Invalid Path: " + pathResult.error());
            }
            pos += AssetPath.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int timestampOffset = buffer.getIntLE(offset + 5);
            if (timestampOffset < 0) {
                return ValidationResult.error("Invalid offset for Timestamp");
            }
            int pos = offset + 9 + timestampOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Timestamp");
            }
            final int timestampLen = VarInt.peek(buffer, pos);
            if (timestampLen < 0) {
                return ValidationResult.error("Invalid string length for Timestamp");
            }
            if (timestampLen > 4096000) {
                return ValidationResult.error("Timestamp exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += timestampLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Timestamp");
            }
        }
        return ValidationResult.OK;
    }
    
    public TimestampedAssetReference clone() {
        final TimestampedAssetReference copy = new TimestampedAssetReference();
        copy.path = ((this.path != null) ? this.path.clone() : null);
        copy.timestamp = this.timestamp;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final TimestampedAssetReference other) {
            return Objects.equals(this.path, other.path) && Objects.equals(this.timestamp, other.timestamp);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.path, this.timestamp);
    }
}
