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

package com.hypixel.hytale.server.core.asset.common;

import com.hypixel.hytale.server.core.util.HashUtil;
import com.hypixel.hytale.common.util.PatternUtil;
import javax.annotation.Nullable;
import java.lang.ref.SoftReference;
import java.util.concurrent.CompletableFuture;
import java.lang.ref.WeakReference;
import javax.annotation.Nonnull;
import java.util.regex.Pattern;
import com.hypixel.hytale.protocol.Asset;
import com.hypixel.hytale.server.core.io.NetworkSerializable;

public abstract class CommonAsset implements NetworkSerializable<Asset>
{
    public static final int HASH_LENGTH = 64;
    public static final Pattern HASH_PATTERN;
    @Nonnull
    private final String name;
    @Nonnull
    private final String hash;
    protected transient WeakReference<CompletableFuture<byte[]>> blob;
    protected transient SoftReference<Asset> cachedPacket;
    
    public CommonAsset(@Nonnull final String name, @Nullable final byte[] bytes) {
        this.name = PatternUtil.replaceBackslashWithForwardSlash(name);
        this.hash = hash(bytes);
        this.blob = new WeakReference<CompletableFuture<byte[]>>((bytes != null) ? CompletableFuture.completedFuture(bytes) : null);
    }
    
    public CommonAsset(@Nonnull final String name, @Nonnull final String hash, @Nullable final byte[] bytes) {
        this.name = PatternUtil.replaceBackslashWithForwardSlash(name);
        this.hash = hash.toLowerCase();
        this.blob = new WeakReference<CompletableFuture<byte[]>>((bytes != null) ? CompletableFuture.completedFuture(bytes) : null);
    }
    
    @Nonnull
    public String getName() {
        return this.name;
    }
    
    @Nonnull
    public String getHash() {
        return this.hash;
    }
    
    public CompletableFuture<byte[]> getBlob() {
        CompletableFuture<byte[]> future = this.blob.get();
        if (future == null) {
            future = this.getBlob0();
            this.blob = new WeakReference<CompletableFuture<byte[]>>(future);
        }
        return future;
    }
    
    protected abstract CompletableFuture<byte[]> getBlob0();
    
    @Nonnull
    @Override
    public Asset toPacket() {
        final Asset cached = (this.cachedPacket == null) ? null : this.cachedPacket.get();
        if (cached != null) {
            return cached;
        }
        final Asset packet = new Asset(this.hash, this.name);
        this.cachedPacket = new SoftReference<Asset>(packet);
        return packet;
    }
    
    @Override
    public boolean equals(@Nullable final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final CommonAsset asset = (CommonAsset)o;
        return this.name.equals(asset.name) && this.hash.equals(asset.hash);
    }
    
    @Override
    public int hashCode() {
        int result = this.name.hashCode();
        result = 31 * result + this.hash.hashCode();
        return result;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "CommonAsset{name='" + this.name + "', hash='" + this.hash + "'}";
    }
    
    @Nonnull
    public static String hash(final byte[] bytes) {
        return HashUtil.sha256(bytes);
    }
    
    static {
        HASH_PATTERN = Pattern.compile("^[A-Fa-f0-9]{64}$");
    }
}
