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

package com.hypixel.hytale.protocol;

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;

public class ItemReticle
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 6;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 6;
    public static final int MAX_SIZE = 1677721600;
    public boolean hideBase;
    @Nullable
    public String[] parts;
    public float duration;
    
    public ItemReticle() {
    }
    
    public ItemReticle(final boolean hideBase, @Nullable final String[] parts, final float duration) {
        this.hideBase = hideBase;
        this.parts = parts;
        this.duration = duration;
    }
    
    public ItemReticle(@Nonnull final ItemReticle other) {
        this.hideBase = other.hideBase;
        this.parts = other.parts;
        this.duration = other.duration;
    }
    
    @Nonnull
    public static ItemReticle deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemReticle obj = new ItemReticle();
        final byte nullBits = buf.getByte(offset);
        obj.hideBase = (buf.getByte(offset + 1) != 0);
        obj.duration = buf.getFloatLE(offset + 2);
        int pos = offset + 6;
        if ((nullBits & 0x1) != 0x0) {
            final int partsCount = VarInt.peek(buf, pos);
            if (partsCount < 0) {
                throw ProtocolException.negativeLength("Parts", partsCount);
            }
            if (partsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Parts", partsCount, 4096000);
            }
            final int partsVarLen = VarInt.size(partsCount);
            if (pos + partsVarLen + partsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Parts", pos + partsVarLen + partsCount * 1, buf.readableBytes());
            }
            pos += partsVarLen;
            obj.parts = new String[partsCount];
            for (int i = 0; i < partsCount; ++i) {
                final int strLen = VarInt.peek(buf, pos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("parts[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("parts[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, pos);
                obj.parts[i] = PacketIO.readVarString(buf, pos);
                pos += strVarLen + strLen;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 6;
        if ((nullBits & 0x1) != 0x0) {
            final int arrLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos);
            for (int i = 0; i < arrLen; ++i) {
                final int sl = VarInt.peek(buf, pos);
                pos += VarInt.length(buf, pos) + sl;
            }
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.parts != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.hideBase ? 1 : 0);
        buf.writeFloatLE(this.duration);
        if (this.parts != null) {
            if (this.parts.length > 4096000) {
                throw ProtocolException.arrayTooLong("Parts", this.parts.length, 4096000);
            }
            VarInt.write(buf, this.parts.length);
            for (final String item : this.parts) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
    }
    
    public int computeSize() {
        int size = 6;
        if (this.parts != null) {
            int partsSize = 0;
            for (final String elem : this.parts) {
                partsSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.parts.length) + partsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 6) {
            return ValidationResult.error("Buffer too small: expected at least 6 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 6;
        if ((nullBits & 0x1) != 0x0) {
            final int partsCount = VarInt.peek(buffer, pos);
            if (partsCount < 0) {
                return ValidationResult.error("Invalid array count for Parts");
            }
            if (partsCount > 4096000) {
                return ValidationResult.error("Parts exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < partsCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Parts");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Parts");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemReticle clone() {
        final ItemReticle copy = new ItemReticle();
        copy.hideBase = this.hideBase;
        copy.parts = (String[])((this.parts != null) ? ((String[])Arrays.copyOf(this.parts, this.parts.length)) : null);
        copy.duration = this.duration;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemReticle other) {
            return this.hideBase == other.hideBase && Arrays.equals(this.parts, other.parts) && this.duration == other.duration;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Boolean.hashCode(this.hideBase);
        result = 31 * result + Arrays.hashCode(this.parts);
        result = 31 * result + Float.hashCode(this.duration);
        return result;
    }
}
