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

package com.hypixel.hytale.assetstore;

import com.hypixel.hytale.assetstore.map.JsonAssetWithMap;
import java.io.IOException;
import com.hypixel.hytale.codec.util.RawJsonReader;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import com.hypixel.hytale.assetstore.codec.ContainedAssetCodec;
import javax.annotation.Nullable;
import java.nio.file.Path;
import javax.annotation.ParametersAreNullableByDefault;

@ParametersAreNullableByDefault
public class RawAsset<K> implements AssetHolder<K>
{
    private final Path parentPath;
    @Nullable
    private final K key;
    private final int lineOffset;
    private final boolean parentKeyResolved;
    @Nullable
    private final K parentKey;
    @Nullable
    private final Path path;
    @Nullable
    private final char[] buffer;
    @Nullable
    private final AssetExtraInfo.Data containerData;
    @Nonnull
    private final ContainedAssetCodec.Mode containedAssetMode;
    
    public RawAsset(final K key, final Path path) {
        this.key = key;
        this.lineOffset = 0;
        this.parentKeyResolved = false;
        this.parentKey = null;
        this.path = path;
        this.parentPath = null;
        this.buffer = null;
        this.containerData = null;
        this.containedAssetMode = ContainedAssetCodec.Mode.NONE;
    }
    
    public RawAsset(final Path parentPath, final K key, final K parentKey, final int lineOffset, final char[] buffer, final AssetExtraInfo.Data containerData, @Nonnull final ContainedAssetCodec.Mode containedAssetMode) {
        this.key = key;
        this.lineOffset = lineOffset;
        this.parentKeyResolved = true;
        this.parentKey = parentKey;
        this.path = null;
        this.parentPath = parentPath;
        this.buffer = buffer;
        this.containerData = containerData;
        this.containedAssetMode = containedAssetMode;
    }
    
    private RawAsset(final K key, final boolean parentKeyResolved, final K parentKey, final Path path, final char[] buffer, final AssetExtraInfo.Data containerData, @Nonnull final ContainedAssetCodec.Mode containedAssetMode) {
        this.key = key;
        this.lineOffset = 0;
        this.parentKeyResolved = parentKeyResolved;
        this.parentKey = parentKey;
        this.path = path;
        this.parentPath = null;
        this.buffer = buffer;
        this.containerData = containerData;
        this.containedAssetMode = containedAssetMode;
    }
    
    @Nullable
    public K getKey() {
        return this.key;
    }
    
    public boolean isParentKeyResolved() {
        return this.parentKeyResolved;
    }
    
    @Nullable
    public K getParentKey() {
        return this.parentKey;
    }
    
    @Nullable
    public Path getPath() {
        return this.path;
    }
    
    public Path getParentPath() {
        return this.parentPath;
    }
    
    public int getLineOffset() {
        return this.lineOffset;
    }
    
    public char[] getBuffer() {
        return this.buffer;
    }
    
    @Nonnull
    public ContainedAssetCodec.Mode getContainedAssetMode() {
        return this.containedAssetMode;
    }
    
    @Nonnull
    public RawJsonReader toRawJsonReader(@Nonnull final Supplier<char[]> bufferSupplier) throws IOException {
        if (this.path != null) {
            return RawJsonReader.fromPath(this.path, bufferSupplier.get());
        }
        return RawJsonReader.fromBuffer(this.buffer);
    }
    
    @Nonnull
    public AssetExtraInfo.Data makeData(final Class<? extends JsonAssetWithMap<K, ?>> aClass, final K key, final K parentKey) {
        final boolean inheritTags = switch (this.containedAssetMode) {
            default -> throw new MatchException(null, null);
            case INHERIT_ID,  INHERIT_ID_AND_PARENT,  INJECT_PARENT -> true;
            case NONE,  GENERATE_ID -> false;
        };
        return new AssetExtraInfo.Data(this.containerData, (Class<? extends JsonAsset<K>>)aClass, (K)key, (K)parentKey, inheritTags);
    }
    
    @Nonnull
    public RawAsset<K> withResolveKeys(final K key, final K parentKey) {
        return new RawAsset<K>(key, true, parentKey, this.path, this.buffer, this.containerData, this.containedAssetMode);
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "RawAsset{key=" + String.valueOf(this.key) + ", parentKeyResolved=" + this.parentKeyResolved + ", parentKey=" + String.valueOf(this.parentKey) + ", path=" + String.valueOf(this.path) + ", buffer.length=" + ((this.buffer != null) ? this.buffer.length : -1);
    }
}
