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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import meteordevelopment.meteorclient.systems.System;
import meteordevelopment.meteorclient.systems.Systems;
import meteordevelopment.meteorclient.systems.accounts.Account;
import meteordevelopment.meteorclient.systems.accounts.AccountType;
import meteordevelopment.meteorclient.systems.accounts.types.CrackedAccount;
import meteordevelopment.meteorclient.systems.accounts.types.MicrosoftAccount;
import meteordevelopment.meteorclient.systems.accounts.types.SessionAccount;
import meteordevelopment.meteorclient.systems.accounts.types.TheAlteningAccount;
import meteordevelopment.meteorclient.utils.misc.NbtException;
import meteordevelopment.meteorclient.utils.misc.NbtUtils;
import meteordevelopment.meteorclient.utils.network.MeteorExecutor;
import net.minecraft.class_2487;
import net.minecraft.class_2520;
import org.jetbrains.annotations.NotNull;

public class Accounts
extends System<Accounts>
implements Iterable<Account<?>> {
    private List<Account<?>> accounts = new ArrayList();

    public Accounts() {
        super("accounts");
    }

    public static Accounts get() {
        return Systems.get(Accounts.class);
    }

    public void add(Account<?> account) {
        this.accounts.add(account);
        this.save();
    }

    public boolean exists(Account<?> account) {
        return this.accounts.contains(account);
    }

    public void remove(Account<?> account) {
        if (this.accounts.remove(account)) {
            this.save();
        }
    }

    public int size() {
        return this.accounts.size();
    }

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

    @Override
    public class_2487 toTag() {
        class_2487 tag = new class_2487();
        tag.method_10566("accounts", (class_2520)NbtUtils.listToTag(this.accounts));
        return tag;
    }

    @Override
    public Accounts fromTag(class_2487 tag) {
        MeteorExecutor.execute(() -> {
            this.accounts = NbtUtils.listFromTag(tag.method_68569("accounts"), tag1 -> {
                class_2487 t = (class_2487)tag1;
                if (!t.method_10545("type")) {
                    return null;
                }
                AccountType type = AccountType.valueOf(t.method_68564("type", ""));
                try {
                    return switch (type) {
                        default -> throw new MatchException(null, null);
                        case AccountType.Cracked -> (CrackedAccount)new CrackedAccount(null).fromTag(t);
                        case AccountType.Microsoft -> (MicrosoftAccount)new MicrosoftAccount(null).fromTag(t);
                        case AccountType.TheAltening -> new TheAlteningAccount(null).fromTag(t);
                        case AccountType.Session -> new SessionAccount(null).fromTag(t);
                    };
                }
                catch (NbtException e) {
                    return null;
                }
            });
        });
        return this;
    }
}

