package net.minecraft.client.sounds;

import java.util.Iterator;
import net.minecraft.network.chat.TranslatableComponent;
import com.google.common.collect.Lists;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import java.util.Random;
import java.util.List;
import net.minecraft.client.resources.sounds.Sound;

public class WeighedSoundEvents implements Weighted<Sound> {
    private final List<Weighted<Sound>> list;
    private final Random random;
    private final ResourceLocation location;
    @Nullable
    private final Component subtitle;
    
    public WeighedSoundEvents(final ResourceLocation uh, @Nullable final String string) {
        this.list = Lists.newArrayList();
        this.random = new Random();
        this.location = uh;
        this.subtitle = ((string == null) ? null : new TranslatableComponent(string));
    }
    
    @Override
    public int getWeight() {
        int integer2 = 0;
        for (final Weighted<Sound> epp4 : this.list) {
            integer2 += epp4.getWeight();
        }
        return integer2;
    }
    
    @Override
    public Sound getSound() {
        final int integer2 = this.getWeight();
        if (this.list.isEmpty() || integer2 == 0) {
            return SoundManager.EMPTY_SOUND;
        }
        int integer3 = this.random.nextInt(integer2);
        for (final Weighted<Sound> epp5 : this.list) {
            integer3 -= epp5.getWeight();
            if (integer3 < 0) {
                return epp5.getSound();
            }
        }
        return SoundManager.EMPTY_SOUND;
    }
    
    public void addSound(final Weighted<Sound> epp) {
        this.list.add(epp);
    }
    
    @Nullable
    public Component getSubtitle() {
        return this.subtitle;
    }
    
    @Override
    public void preloadIfRequired(final SoundEngine epk) {
        for (final Weighted<Sound> epp4 : this.list) {
            epp4.preloadIfRequired(epk);
        }
    }
}
