/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
 *  net.minecraft.class_1887
 *  net.minecraft.class_1893
 *  net.minecraft.class_2378
 *  net.minecraft.class_2487
 *  net.minecraft.class_2499
 *  net.minecraft.class_2519
 *  net.minecraft.class_2520
 *  net.minecraft.class_2960
 *  net.minecraft.class_310
 *  net.minecraft.class_5321
 *  net.minecraft.class_7924
 */
package meteordevelopment.meteorclient.settings;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import java.lang.reflect.AccessFlag;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import meteordevelopment.meteorclient.settings.IVisible;
import meteordevelopment.meteorclient.settings.Setting;
import net.minecraft.class_1887;
import net.minecraft.class_1893;
import net.minecraft.class_2378;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2519;
import net.minecraft.class_2520;
import net.minecraft.class_2960;
import net.minecraft.class_310;
import net.minecraft.class_5321;
import net.minecraft.class_7924;

public class EnchantmentListSetting
extends Setting<Set<class_5321<class_1887>>> {
    public EnchantmentListSetting(String name, String description, Set<class_5321<class_1887>> defaultValue, Consumer<Set<class_5321<class_1887>>> onChanged, Consumer<Setting<Set<class_5321<class_1887>>>> onModuleActivated, IVisible visible) {
        super(name, description, defaultValue, onChanged, onModuleActivated, visible);
    }

    @Override
    public void resetImpl() {
        this.value = new ObjectOpenHashSet((Collection)this.defaultValue);
    }

    @Override
    protected Set<class_5321<class_1887>> parseImpl(String str) {
        String[] values = str.split(",");
        ObjectOpenHashSet enchs = new ObjectOpenHashSet(values.length);
        for (String value : values) {
            String name = value.trim();
            class_2960 id = name.contains(":") ? class_2960.method_60654((String)name) : class_2960.method_60656((String)name);
            enchs.add(class_5321.method_29179((class_5321)class_7924.field_41265, (class_2960)id));
        }
        return enchs;
    }

    @Override
    protected boolean isValueValid(Set<class_5321<class_1887>> value) {
        return true;
    }

    @Override
    public Iterable<class_2960> getIdentifierSuggestions() {
        return Optional.ofNullable(class_310.method_1551().method_1562()).flatMap(networkHandler -> networkHandler.method_29091().method_46759(class_7924.field_41265)).map(class_2378::method_10235).orElse(Set.of());
    }

    @Override
    public class_2487 save(class_2487 tag) {
        class_2499 valueTag = new class_2499();
        for (class_5321 ench : (Set)this.get()) {
            valueTag.add((Object)class_2519.method_23256((String)ench.method_29177().toString()));
        }
        tag.method_10566("value", (class_2520)valueTag);
        return tag;
    }

    @Override
    public Set<class_5321<class_1887>> load(class_2487 tag) {
        ((Set)this.get()).clear();
        for (class_2520 tagI : tag.method_68569("value")) {
            ((Set)this.get()).add(class_5321.method_29179((class_5321)class_7924.field_41265, (class_2960)class_2960.method_60654((String)tagI.method_68658().orElse(""))));
        }
        return (Set)this.get();
    }

    public static class Builder
    extends Setting.SettingBuilder<Builder, Set<class_5321<class_1887>>, EnchantmentListSetting> {
        private static final Set<class_5321<class_1887>> VANILLA_DEFAULTS = Arrays.stream(class_1893.class.getDeclaredFields()).filter(field -> field.accessFlags().containsAll(List.of(AccessFlag.PUBLIC, AccessFlag.STATIC, AccessFlag.FINAL))).filter(field -> field.getType() == class_5321.class).map(field -> {
            try {
                return field.get(null);
            }
            catch (IllegalAccessException e) {
                return null;
            }
        }).filter(Objects::nonNull).map(class_5321.class::cast).filter(registryKey -> registryKey.method_58273() == class_7924.field_41265).collect(Collectors.toSet());

        public Builder() {
            super(new ObjectOpenHashSet());
        }

        public Builder vanillaDefaults() {
            return (Builder)this.defaultValue(VANILLA_DEFAULTS);
        }

        @Override
        @SafeVarargs
        public final Builder defaultValue(class_5321<class_1887> ... defaults) {
            return (Builder)this.defaultValue(defaults != null ? new ObjectOpenHashSet((Object[])defaults) : new ObjectOpenHashSet());
        }

        @Override
        public EnchantmentListSetting build() {
            return new EnchantmentListSetting(this.name, this.description, (Set)this.defaultValue, this.onChanged, this.onModuleActivated, this.visible);
        }
    }
}

