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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import com.hypixel.hytale.protocol.io.PacketIO;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;

public class Asset
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 0;
    public static final int FIXED_BLOCK_SIZE = 64;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 64;
    public static final int MAX_SIZE = 2117;
    @Nonnull
    public String hash;
    @Nonnull
    public String name;
    
    public Asset() {
        this.hash = "";
        this.name = "";
    }
    
    public Asset(@Nonnull final String hash, @Nonnull final String name) {
        this.hash = "";
        this.name = "";
        this.hash = hash;
        this.name = name;
    }
    
    public Asset(@Nonnull final Asset other) {
        this.hash = "";
        this.name = "";
        this.hash = other.hash;
        this.name = other.name;
    }
    
    @Nonnull
    public static Asset deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Asset obj = new Asset();
        obj.hash = PacketIO.readFixedAsciiString(buf, offset + 0, 64);
        int pos = offset + 64;
        final int nameLen = VarInt.peek(buf, pos);
        if (nameLen < 0) {
            throw ProtocolException.negativeLength("Name", nameLen);
        }
        if (nameLen > 512) {
            throw ProtocolException.stringTooLong("Name", nameLen, 512);
        }
        final int nameVarLen = VarInt.length(buf, pos);
        obj.name = PacketIO.readVarString(buf, pos, PacketIO.UTF8);
        pos += nameVarLen + nameLen;
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        int pos = offset + 64;
        final int sl = VarInt.peek(buf, pos);
        pos += VarInt.length(buf, pos) + sl;
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        PacketIO.writeFixedAsciiString(buf, this.hash, 64);
        PacketIO.writeVarString(buf, this.name, 512);
    }
    
    public int computeSize() {
        int size = 64;
        size += PacketIO.stringSize(this.name);
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 64) {
            return ValidationResult.error("Buffer too small: expected at least 64 bytes");
        }
        int pos = offset + 64;
        final int nameLen = VarInt.peek(buffer, pos);
        if (nameLen < 0) {
            return ValidationResult.error("Invalid string length for Name");
        }
        if (nameLen > 512) {
            return ValidationResult.error("Name exceeds max length 512");
        }
        pos += VarInt.length(buffer, pos);
        pos += nameLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading Name");
        }
        return ValidationResult.OK;
    }
    
    public Asset clone() {
        final Asset copy = new Asset();
        copy.hash = this.hash;
        copy.name = this.name;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Asset other) {
            return Objects.equals(this.hash, other.hash) && Objects.equals(this.name, other.name);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.hash, this.name);
    }
}
