/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.mojang.util.UndashedUuid
 *  net.minecraft.class_2487
 */
package meteordevelopment.meteorclient.systems.accounts;

import com.mojang.util.UndashedUuid;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.utils.misc.ISerializable;
import meteordevelopment.meteorclient.utils.misc.NbtException;
import meteordevelopment.meteorclient.utils.network.MeteorExecutor;
import meteordevelopment.meteorclient.utils.render.PlayerHeadTexture;
import meteordevelopment.meteorclient.utils.render.PlayerHeadUtils;
import net.minecraft.class_2487;

public class AccountCache
implements ISerializable<AccountCache> {
    public String username = "";
    public String uuid = "";
    private PlayerHeadTexture headTexture;
    private volatile boolean loadingHead;

    public PlayerHeadTexture getHeadTexture() {
        return this.headTexture != null ? this.headTexture : PlayerHeadUtils.STEVE_HEAD;
    }

    public void loadHead() {
        this.loadHead(null);
    }

    public void loadHead(Runnable callback) {
        if (this.headTexture != null || this.uuid == null || this.uuid.isBlank()) {
            if (callback != null) {
                MeteorClient.mc.execute(callback);
            }
            return;
        }
        if (this.loadingHead) {
            return;
        }
        this.loadingHead = true;
        MeteorExecutor.execute(() -> {
            byte[] head = PlayerHeadUtils.fetchHead(UndashedUuid.fromStringLenient((String)this.uuid));
            MeteorClient.mc.execute(() -> {
                if (head != null) {
                    this.headTexture = new PlayerHeadTexture(head, true);
                }
                this.loadingHead = false;
                if (callback != null) {
                    callback.run();
                }
            });
        });
    }

    @Override
    public class_2487 toTag() {
        class_2487 tag = new class_2487();
        tag.method_10582("username", this.username);
        tag.method_10582("uuid", this.uuid);
        return tag;
    }

    @Override
    public AccountCache fromTag(class_2487 tag) {
        if (tag.method_10558("username").isEmpty() || tag.method_10558("uuid").isEmpty()) {
            throw new NbtException();
        }
        this.username = (String)tag.method_10558("username").get();
        this.uuid = (String)tag.method_10558("uuid").get();
        this.loadHead();
        return this;
    }
}

