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 abc) {
        this.cache = Maps.newHashMap();
        this.resourceManager = abc;
    }
    
    public CompletableFuture<SoundBuffer> getCompleteBuffer(final ResourceLocation uh) {
        Resource abb3;
        InputStream inputStream5;
        OggAudioStream dfw7;
        ByteBuffer byteBuffer9;
        SoundBuffer soundBuffer;
        final Throwable t2;
        final Throwable t4;
        final Throwable t6;
        return this.cache.computeIfAbsent(uh, uh -> CompletableFuture.<SoundBuffer>supplyAsync(() -> {
            try {
                abb3 = this.resourceManager.getResource(uh);
                try {
                    inputStream5 = abb3.getInputStream();
                    try {
                        dfw7 = new OggAudioStream(inputStream5);
                        try {
                            byteBuffer9 = dfw7.readAll();
                            soundBuffer = new SoundBuffer(byteBuffer9, dfw7.getFormat());
                            return soundBuffer;
                        }
                        catch (Throwable t) {
                            throw t;
                        }
                        finally {
                            if (dfw7 != null) {
                                if (t2 != null) {
                                    try {
                                        dfw7.close();
                                    }
                                    catch (Throwable exception) {
                                        t2.addSuppressed(exception);
                                    }
                                }
                                else {
                                    dfw7.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 (abb3 != null) {
                        if (t6 != null) {
                            try {
                                abb3.close();
                            }
                            catch (Throwable exception3) {
                                t6.addSuppressed(exception3);
                            }
                        }
                        else {
                            abb3.close();
                        }
                    }
                }
            }
            catch (IOException iOException3) {
                throw new CompletionException(iOException3);
            }
        }, Util.backgroundExecutor()));
    }
    
    public CompletableFuture<AudioStream> getStream(final ResourceLocation uh, final boolean boolean2) {
        Resource abb4;
        InputStream inputStream5;
        final Object o3;
        return CompletableFuture.<AudioStream>supplyAsync(() -> {
            try {
                abb4 = this.resourceManager.getResource(uh);
                inputStream5 = abb4.getInputStream();
                if (boolean2) {
                    // new(net.minecraft.client.sounds.LoopingAudioStream.class)
                    new LoopingAudioStream(OggAudioStream::new, inputStream5);
                }
                else {
                    // new(com.mojang.blaze3d.audio.OggAudioStream.class)
                    new OggAudioStream(inputStream5);
                }
                return o3;
            }
            catch (IOException iOException4) {
                throw new CompletionException(iOException4);
            }
        }, 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(eoj -> this.getCompleteBuffer(eoj.getPath())).<CompletableFuture<?>>toArray(CompletableFuture[]::new));
    }
}
