/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minecraft.class_2487
 *  net.minecraft.class_2499
 *  net.minecraft.class_2520
 *  org.jetbrains.annotations.NotNull
 */
package meteordevelopment.meteorclient.settings;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.utils.misc.ISerializable;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2520;
import org.jetbrains.annotations.NotNull;

public class SettingGroup
implements ISerializable<SettingGroup>,
Iterable<Setting<?>> {
    public final String name;
    public boolean sectionExpanded;
    final List<Setting<?>> settings = new ArrayList(1);

    SettingGroup(String name, boolean sectionExpanded) {
        this.name = name;
        this.sectionExpanded = sectionExpanded;
    }

    public Setting<?> get(String name) {
        for (Setting<?> setting : this) {
            if (!setting.name.equals(name)) continue;
            return setting;
        }
        return null;
    }

    public <T extends Setting<?>> T add(T setting) {
        this.settings.add(setting);
        return setting;
    }

    public Setting<?> getByIndex(int index) {
        return this.settings.get(index);
    }

    public boolean wasChanged() {
        for (Setting<?> setting : this.settings) {
            if (!setting.wasChanged()) continue;
            return true;
        }
        return false;
    }

    @Override
    @NotNull
    public Iterator<Setting<?>> iterator() {
        return this.settings.iterator();
    }

    @Override
    public class_2487 toTag() {
        class_2487 tag = new class_2487();
        tag.method_10582("name", this.name);
        tag.method_10556("sectionExpanded", this.sectionExpanded);
        class_2499 settingsTag = new class_2499();
        for (Setting<?> setting : this) {
            if (!setting.wasChanged()) continue;
            settingsTag.add((Object)setting.toTag());
        }
        if (!settingsTag.isEmpty()) {
            tag.method_10566("settings", (class_2520)settingsTag);
        }
        return tag;
    }

    @Override
    public SettingGroup fromTag(class_2487 tag) {
        this.sectionExpanded = tag.method_68566("sectionExpanded", false);
        class_2499 settingsTag = tag.method_68569("settings");
        for (class_2520 t : settingsTag) {
            class_2487 settingTag = (class_2487)t;
            Setting<?> setting = this.get(settingTag.method_68564("name", ""));
            if (setting == null) continue;
            setting.fromTag(settingTag);
        }
        return this;
    }
}

