package net.minecraft.client.sounds;

import net.minecraft.client.resources.sounds.Sound;
import java.util.Collection;
import java.util.function.Consumer;
import java.nio.ByteBuffer;
import java.io.InputStream;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.Util;
import java.io.IOException;
import java.util.concurrent.CompletionException;
import com.mojang.blaze3d.audio.OggAudioStream;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.audio.SoundBuffer;
import java.util.concurrent.CompletableFuture;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import net.minecraft.server.packs.resources.ResourceManager;

public class SoundBufferLibrary {
    private final ResourceManager resourceManager;
    private final Map<ResourceLocation, CompletableFuture<SoundBuffer>> cache;
    
    public SoundBufferLibrary(final ResourceManager zb) {
        this.cache = Maps.newHashMap();
        this.resourceManager = zb;
    }
    
    public CompletableFuture<SoundBuffer> getCompleteBuffer(final ResourceLocation sm) {
        Resource za3;
        InputStream inputStream5;
        AudioStream eds7;
        ByteBuffer byteBuffer9;
        SoundBuffer soundBuffer;
        final Throwable t2;
        final Throwable t4;
        final Throwable t6;
        return this.cache.computeIfAbsent(sm, sm -> CompletableFuture.<SoundBuffer>supplyAsync(() -> {
            try {
                za3 = this.resourceManager.getResource(sm);
                try {
                    inputStream5 = za3.getInputStream();
                    try {
                        eds7 = new OggAudioStream(inputStream5);
                        try {
                            byteBuffer9 = eds7.readAll();
                            soundBuffer = new SoundBuffer(byteBuffer9, eds7.getFormat());
                            return soundBuffer;
                        }
                        catch (Throwable t) {
                            throw t;
                        }
                        finally {
                            if (eds7 != null) {
                                if (t2 != null) {
                                    try {
                                        eds7.close();
                                    }
                                    catch (Throwable exception) {
                                        t2.addSuppressed(exception);
                                    }
                                }
                                else {
                                    eds7.close();
                                }
                            }
                        }
                    }
                    catch (Throwable t3) {
                        throw t3;
                    }
                    finally {
                        if (inputStream5 != null) {
                            if (t4 != null) {
                                try {
                                    inputStream5.close();
                                }
                                catch (Throwable exception2) {
                                    t4.addSuppressed(exception2);
                                }
                            }
                            else {
                                inputStream5.close();
                            }
                        }
                    }
                }
                catch (Throwable t5) {
                    throw t5;
                }
                finally {
                    if (za3 != null) {
                        if (t6 != null) {
                            try {
                                za3.close();
                            }
                            catch (Throwable exception3) {
                                t6.addSuppressed(exception3);
                            }
                        }
                        else {
                            za3.close();
                        }
                    }
                }
            }
            catch (IOException iOException3) {
                throw new CompletionException(iOException3);
            }
        }, Util.backgroundExecutor()));
    }
    
    public CompletableFuture<AudioStream> getStream(final ResourceLocation sm) {
        Resource za3;
        InputStream inputStream4;
        return CompletableFuture.supplyAsync(() -> {
            try {
                za3 = this.resourceManager.getResource(sm);
                inputStream4 = za3.getInputStream();
                return new OggAudioStream(inputStream4);
            }
            catch (IOException iOException3) {
                throw new CompletionException(iOException3);
            }
        }, Util.backgroundExecutor());
    }
    
    public void clear() {
        this.cache.values().forEach(completableFuture -> completableFuture.thenAccept((Consumer)SoundBuffer::discardAlBuffer));
        this.cache.clear();
    }
    
    public CompletableFuture<?> preload(final Collection<Sound> collection) {
        return CompletableFuture.allOf(collection.stream().map(ecw -> this.getCompleteBuffer(ecw.getPath())).<CompletableFuture<?>>toArray(CompletableFuture[]::new));
    }
}
