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

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

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;

public class AssetPackManifest
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 6;
    public static final int VARIABLE_BLOCK_START = 25;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String name;
    @Nullable
    public String group;
    @Nullable
    public String website;
    @Nullable
    public String description;
    @Nullable
    public String version;
    @Nullable
    public AuthorInfo[] authors;
    
    public AssetPackManifest() {
    }
    
    public AssetPackManifest(@Nullable final String name, @Nullable final String group, @Nullable final String website, @Nullable final String description, @Nullable final String version, @Nullable final AuthorInfo[] authors) {
        this.name = name;
        this.group = group;
        this.website = website;
        this.description = description;
        this.version = version;
        this.authors = authors;
    }
    
    public AssetPackManifest(@Nonnull final AssetPackManifest other) {
        this.name = other.name;
        this.group = other.group;
        this.website = other.website;
        this.description = other.description;
        this.version = other.version;
        this.authors = other.authors;
    }
    
    @Nonnull
    public static AssetPackManifest deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetPackManifest obj = new AssetPackManifest();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 25 + buf.getIntLE(offset + 1);
            final int nameLen = VarInt.peek(buf, varPos0);
            if (nameLen < 0) {
                throw ProtocolException.negativeLength("Name", nameLen);
            }
            if (nameLen > 4096000) {
                throw ProtocolException.stringTooLong("Name", nameLen, 4096000);
            }
            obj.name = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 25 + buf.getIntLE(offset + 5);
            final int groupLen = VarInt.peek(buf, varPos2);
            if (groupLen < 0) {
                throw ProtocolException.negativeLength("Group", groupLen);
            }
            if (groupLen > 4096000) {
                throw ProtocolException.stringTooLong("Group", groupLen, 4096000);
            }
            obj.group = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 25 + buf.getIntLE(offset + 9);
            final int websiteLen = VarInt.peek(buf, varPos3);
            if (websiteLen < 0) {
                throw ProtocolException.negativeLength("Website", websiteLen);
            }
            if (websiteLen > 4096000) {
                throw ProtocolException.stringTooLong("Website", websiteLen, 4096000);
            }
            obj.website = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 25 + buf.getIntLE(offset + 13);
            final int descriptionLen = VarInt.peek(buf, varPos4);
            if (descriptionLen < 0) {
                throw ProtocolException.negativeLength("Description", descriptionLen);
            }
            if (descriptionLen > 4096000) {
                throw ProtocolException.stringTooLong("Description", descriptionLen, 4096000);
            }
            obj.description = PacketIO.readVarString(buf, varPos4, PacketIO.UTF8);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos5 = offset + 25 + buf.getIntLE(offset + 17);
            final int versionLen = VarInt.peek(buf, varPos5);
            if (versionLen < 0) {
                throw ProtocolException.negativeLength("Version", versionLen);
            }
            if (versionLen > 4096000) {
                throw ProtocolException.stringTooLong("Version", versionLen, 4096000);
            }
            obj.version = PacketIO.readVarString(buf, varPos5, PacketIO.UTF8);
        }
        if ((nullBits & 0x20) != 0x0) {
            final int varPos6 = offset + 25 + buf.getIntLE(offset + 21);
            final int authorsCount = VarInt.peek(buf, varPos6);
            if (authorsCount < 0) {
                throw ProtocolException.negativeLength("Authors", authorsCount);
            }
            if (authorsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Authors", authorsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos6);
            if (varPos6 + varIntLen + authorsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Authors", varPos6 + varIntLen + authorsCount * 1, buf.readableBytes());
            }
            obj.authors = new AuthorInfo[authorsCount];
            int elemPos = varPos6 + varIntLen;
            for (int i = 0; i < authorsCount; ++i) {
                obj.authors[i] = AuthorInfo.deserialize(buf, elemPos);
                elemPos += AuthorInfo.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 25;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 25 + 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 + 5);
            int pos2 = offset + 25 + 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 + 9);
            int pos3 = offset + 25 + 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 + 13);
            int pos4 = offset + 25 + fieldOffset4;
            final int sl = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4) + sl;
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 17);
            int pos5 = offset + 25 + fieldOffset5;
            final int sl = VarInt.peek(buf, pos5);
            pos5 += VarInt.length(buf, pos5) + sl;
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset6 = buf.getIntLE(offset + 21);
            int pos6 = offset + 25 + fieldOffset6;
            final int arrLen = VarInt.peek(buf, pos6);
            pos6 += VarInt.length(buf, pos6);
            for (int i = 0; i < arrLen; ++i) {
                pos6 += AuthorInfo.computeBytesConsumed(buf, pos6);
            }
            if (pos6 - offset > maxEnd) {
                maxEnd = pos6 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.name != null) {
            nullBits |= 0x1;
        }
        if (this.group != null) {
            nullBits |= 0x2;
        }
        if (this.website != null) {
            nullBits |= 0x4;
        }
        if (this.description != null) {
            nullBits |= 0x8;
        }
        if (this.version != null) {
            nullBits |= 0x10;
        }
        if (this.authors != null) {
            nullBits |= 0x20;
        }
        buf.writeByte(nullBits);
        final int nameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int groupOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int websiteOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int descriptionOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int versionOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int authorsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.name != null) {
            buf.setIntLE(nameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.name, 4096000);
        }
        else {
            buf.setIntLE(nameOffsetSlot, -1);
        }
        if (this.group != null) {
            buf.setIntLE(groupOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.group, 4096000);
        }
        else {
            buf.setIntLE(groupOffsetSlot, -1);
        }
        if (this.website != null) {
            buf.setIntLE(websiteOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.website, 4096000);
        }
        else {
            buf.setIntLE(websiteOffsetSlot, -1);
        }
        if (this.description != null) {
            buf.setIntLE(descriptionOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.description, 4096000);
        }
        else {
            buf.setIntLE(descriptionOffsetSlot, -1);
        }
        if (this.version != null) {
            buf.setIntLE(versionOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.version, 4096000);
        }
        else {
            buf.setIntLE(versionOffsetSlot, -1);
        }
        if (this.authors != null) {
            buf.setIntLE(authorsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.authors.length > 4096000) {
                throw ProtocolException.arrayTooLong("Authors", this.authors.length, 4096000);
            }
            VarInt.write(buf, this.authors.length);
            for (final AuthorInfo item : this.authors) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(authorsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 25;
        if (this.name != null) {
            size += PacketIO.stringSize(this.name);
        }
        if (this.group != null) {
            size += PacketIO.stringSize(this.group);
        }
        if (this.website != null) {
            size += PacketIO.stringSize(this.website);
        }
        if (this.description != null) {
            size += PacketIO.stringSize(this.description);
        }
        if (this.version != null) {
            size += PacketIO.stringSize(this.version);
        }
        if (this.authors != null) {
            int authorsSize = 0;
            for (final AuthorInfo elem : this.authors) {
                authorsSize += elem.computeSize();
            }
            size += VarInt.size(this.authors.length) + authorsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 25) {
            return ValidationResult.error("Buffer too small: expected at least 25 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int nameOffset = buffer.getIntLE(offset + 1);
            if (nameOffset < 0) {
                return ValidationResult.error("Invalid offset for Name");
            }
            int pos = offset + 25 + nameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Name");
            }
            final int nameLen = VarInt.peek(buffer, pos);
            if (nameLen < 0) {
                return ValidationResult.error("Invalid string length for Name");
            }
            if (nameLen > 4096000) {
                return ValidationResult.error("Name exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += nameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Name");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int groupOffset = buffer.getIntLE(offset + 5);
            if (groupOffset < 0) {
                return ValidationResult.error("Invalid offset for Group");
            }
            int pos = offset + 25 + groupOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Group");
            }
            final int groupLen = VarInt.peek(buffer, pos);
            if (groupLen < 0) {
                return ValidationResult.error("Invalid string length for Group");
            }
            if (groupLen > 4096000) {
                return ValidationResult.error("Group exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += groupLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Group");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int websiteOffset = buffer.getIntLE(offset + 9);
            if (websiteOffset < 0) {
                return ValidationResult.error("Invalid offset for Website");
            }
            int pos = offset + 25 + websiteOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Website");
            }
            final int websiteLen = VarInt.peek(buffer, pos);
            if (websiteLen < 0) {
                return ValidationResult.error("Invalid string length for Website");
            }
            if (websiteLen > 4096000) {
                return ValidationResult.error("Website exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += websiteLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Website");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int descriptionOffset = buffer.getIntLE(offset + 13);
            if (descriptionOffset < 0) {
                return ValidationResult.error("Invalid offset for Description");
            }
            int pos = offset + 25 + descriptionOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Description");
            }
            final int descriptionLen = VarInt.peek(buffer, pos);
            if (descriptionLen < 0) {
                return ValidationResult.error("Invalid string length for Description");
            }
            if (descriptionLen > 4096000) {
                return ValidationResult.error("Description exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += descriptionLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Description");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int versionOffset = buffer.getIntLE(offset + 17);
            if (versionOffset < 0) {
                return ValidationResult.error("Invalid offset for Version");
            }
            int pos = offset + 25 + versionOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Version");
            }
            final int versionLen = VarInt.peek(buffer, pos);
            if (versionLen < 0) {
                return ValidationResult.error("Invalid string length for Version");
            }
            if (versionLen > 4096000) {
                return ValidationResult.error("Version exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += versionLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Version");
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int authorsOffset = buffer.getIntLE(offset + 21);
            if (authorsOffset < 0) {
                return ValidationResult.error("Invalid offset for Authors");
            }
            int pos = offset + 25 + authorsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Authors");
            }
            final int authorsCount = VarInt.peek(buffer, pos);
            if (authorsCount < 0) {
                return ValidationResult.error("Invalid array count for Authors");
            }
            if (authorsCount > 4096000) {
                return ValidationResult.error("Authors exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < authorsCount; ++i) {
                final ValidationResult structResult = AuthorInfo.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid AuthorInfo in Authors[" + i + "]: " + structResult.error());
                }
                pos += AuthorInfo.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetPackManifest clone() {
        final AssetPackManifest copy = new AssetPackManifest();
        copy.name = this.name;
        copy.group = this.group;
        copy.website = this.website;
        copy.description = this.description;
        copy.version = this.version;
        copy.authors = (AuthorInfo[])((this.authors != null) ? ((AuthorInfo[])Arrays.stream(this.authors).map(e -> e.clone()).toArray(AuthorInfo[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetPackManifest other) {
            return Objects.equals(this.name, other.name) && Objects.equals(this.group, other.group) && Objects.equals(this.website, other.website) && Objects.equals(this.description, other.description) && Objects.equals(this.version, other.version) && Arrays.equals(this.authors, other.authors);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.name);
        result = 31 * result + Objects.hashCode(this.group);
        result = 31 * result + Objects.hashCode(this.website);
        result = 31 * result + Objects.hashCode(this.description);
        result = 31 * result + Objects.hashCode(this.version);
        result = 31 * result + Arrays.hashCode(this.authors);
        return result;
    }
}
