/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minecraft.class_1011
 *  net.minecraft.class_1043
 *  net.minecraft.class_1044
 *  net.minecraft.class_2960
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import java.io.InputStream;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.settings.EnumSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.settings.StringSetting;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.network.Http;
import meteordevelopment.meteorclient.utils.network.MeteorExecutor;
import net.minecraft.class_1011;
import net.minecraft.class_1043;
import net.minecraft.class_1044;
import net.minecraft.class_2960;

public class CapeChanger
extends Module {
    private static int idCounter = 0;
    private final SettingGroup sgGeneral;
    public final Setting<CapeType> capeType;
    public final Setting<String> customUrl;
    private volatile class_2960 capeId;
    private volatile boolean loading;

    public CapeChanger() {
        super(Categories.Donut, "cape-changer", "Equip any cape visually. Only visible to you (client-side only).");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.capeType = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("cape")).description("The cape to equip. Visual only \u2014 only you can see it.")).defaultValue(CapeType.Migrator)).onChanged(t -> this.reload())).build());
        this.customUrl = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("custom-url")).description("URL for a custom 64x32 cape PNG texture.")).defaultValue("https://example.com/cape.png")).visible(() -> this.capeType.get() == CapeType.Custom)).onChanged(u -> this.reload())).build());
        this.capeId = null;
        this.loading = false;
    }

    @Override
    public void onActivate() {
        this.reload();
    }

    @Override
    public void onDeactivate() {
        this.capeId = null;
    }

    private void reload() {
        String url;
        if (!this.isActive()) {
            return;
        }
        CapeType type = this.capeType.get();
        if (type == CapeType.None) {
            this.capeId = null;
            return;
        }
        String string = url = type == CapeType.Custom ? this.customUrl.get() : type.url;
        if (url == null || url.isBlank()) {
            this.capeId = null;
            return;
        }
        if (this.loading) {
            return;
        }
        this.loading = true;
        this.capeId = null;
        class_2960 newId = MeteorClient.identifier("cape-changer/" + idCounter++);
        String finalUrl = url;
        MeteorExecutor.execute(() -> {
            try {
                InputStream stream = Http.get(finalUrl).sendInputStream();
                if (stream == null) {
                    this.loading = false;
                    this.error("Could not download cape texture from URL.", new Object[0]);
                    return;
                }
                class_1011 img = class_1011.method_4309((InputStream)stream);
                this.mc.execute(() -> {
                    this.mc.method_1531().method_4616(newId, (class_1044)new class_1043(null, img));
                    this.capeId = newId;
                    this.loading = false;
                    this.info("Cape loaded: (highlight)%s(default).", type.displayName);
                });
            }
            catch (Exception e) {
                this.loading = false;
                this.error("Failed to load cape: " + e.getMessage(), new Object[0]);
            }
        });
    }

    public class_2960 getCapeIdentifier() {
        return this.capeId;
    }

    public static enum CapeType {
        None("None", null),
        Migrator("Migrator", "https://textures.minecraft.net/texture/2340c0e03dd24a11b15a8b33c2a7e9e32abb2051b2481d0ba7defd635ca7a933"),
        Minecon2011("Minecon 2011", "https://textures.minecraft.net/texture/1bf91499701404e21bd46b0f4e6e1f7f59bdbce7564d2b0f8c358c7b39e1f2e"),
        Minecon2012("Minecon 2012", "https://textures.minecraft.net/texture/a2e8d97ec79100e90a75d369d1b3ba81273c4f82bc1b737e934eed4a854be1b4"),
        Minecon2013("Minecon 2013", "https://textures.minecraft.net/texture/153b1a0dfcbae953cdeb6f2c2bf6bf79943239b1372780da44bcbb29273131da"),
        Minecon2015("Minecon 2015", "https://textures.minecraft.net/texture/b0cc08840700447322d953a02b965f1d65a13a603bf64b17c803c21446fe1635"),
        Minecon2016("Minecon 2016", "https://textures.minecraft.net/texture/9e507afc56359978a3eb3e32367042b853cddd0995d17d0da995662913fb00f7"),
        Vanilla("Vanilla/Default", "https://textures.minecraft.net/texture/be8c548574011038f9aeaff7d4f1f8af52a4b7e9b33e3a6e3ac0ccad1c7be0d"),
        Optifine("OptiFine", "https://textures.minecraft.net/texture/ae70403f9b07a0a9d3f1a75c3f3e4d4c78b7396d826e4f4c1e3a9a4f3f3e4d4c"),
        TwitchStreamer("Twitch Prime", "https://textures.minecraft.net/texture/7c85f1a56e2e849e6c7765ab4f04c46f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7"),
        Lunar("Lunar Client", "https://textures.minecraft.net/texture/1afe3de8f3d16a3843773d8a5db77c8aa2ef1426dad9c61c1cc2e4edd3fb0a4"),
        Custom("Custom URL", null);

        public final String displayName;
        public final String url;

        private CapeType(String displayName, String url) {
            this.displayName = displayName;
            this.url = url;
        }

        public String toString() {
            return this.displayName;
        }
    }
}

