/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet
 *  net.minecraft.class_151
 *  net.minecraft.class_2378
 *  net.minecraft.class_2960
 *  net.minecraft.class_310
 *  net.minecraft.class_5321
 *  net.minecraft.class_7225$class_7226
 *  net.minecraft.class_7887
 */
package meteordevelopment.meteorclient.gui.screens.settings.base;

import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.screens.settings.base.CollectionListSettingScreen;
import meteordevelopment.meteorclient.gui.widgets.containers.WHorizontalList;
import meteordevelopment.meteorclient.gui.widgets.containers.WTable;
import meteordevelopment.meteorclient.gui.widgets.input.WTextBox;
import meteordevelopment.meteorclient.settings.Setting;
import net.minecraft.class_151;
import net.minecraft.class_2378;
import net.minecraft.class_2960;
import net.minecraft.class_310;
import net.minecraft.class_5321;
import net.minecraft.class_7225;
import net.minecraft.class_7887;

public abstract class DynamicRegistryListSettingScreen<T>
extends CollectionListSettingScreen<class_5321<T>> {
    protected final class_5321<class_2378<T>> registryKey;

    public DynamicRegistryListSettingScreen(GuiTheme theme, String title, Setting<?> setting, Collection<class_5321<T>> collection, class_5321<class_2378<T>> registryKey) {
        super(theme, title, setting, collection, DynamicRegistryListSettingScreen.createUniverse(collection, registryKey));
        this.registryKey = registryKey;
    }

    private static <T> Iterable<class_5321<T>> createUniverse(Collection<class_5321<T>> collection, class_5321<class_2378<T>> registryKey) {
        ReferenceOpenHashSet set = new ReferenceOpenHashSet(collection);
        Optional.ofNullable(class_310.method_1551().method_1562()).map(networkHandler -> networkHandler.method_29091()).orElseGet(class_7887::method_46817).method_46759(registryKey).ifPresent(arg_0 -> DynamicRegistryListSettingScreen.lambda$createUniverse$1((Set)set, arg_0));
        return set;
    }

    @Override
    protected void postWidgets(WTable left, WTable right) {
        if (!left.cells.isEmpty()) {
            left.add(this.theme.horizontalSeparator()).expandX();
            left.row();
        }
        WHorizontalList manualEntry = left.add(this.theme.horizontalList()).expandX().widget();
        WTextBox textBox = manualEntry.add(this.theme.textBox("minecraft:")).expandX().minWidth(120.0).widget();
        manualEntry.add(this.theme.plus()).expandCellX().right().widget().action = () -> {
            String entry = textBox.get().trim();
            try {
                class_2960 id = entry.contains(":") ? class_2960.method_60654((String)entry) : class_2960.method_60656((String)entry);
                this.addValue(class_5321.method_29179(this.registryKey, (class_2960)id));
            }
            catch (class_151 class_1512) {
                // empty catch block
            }
        };
    }

    private static /* synthetic */ void lambda$createUniverse$1(Set set, class_7225.class_7226 registry) {
        registry.method_46754().forEach(set::add);
    }
}

