/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_2561
 *  net.minecraft.class_2583
 *  net.minecraft.class_266
 *  net.minecraft.class_269
 *  net.minecraft.class_274
 *  net.minecraft.class_274$class_275
 *  net.minecraft.class_5250
 *  net.minecraft.class_5251
 *  net.minecraft.class_640
 *  net.minecraft.class_8646
 *  net.minecraft.class_9014
 *  net.minecraft.class_9015
 *  net.minecraft.class_9020
 *  net.minecraft.class_9022
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import meteordevelopment.meteorclient.events.render.Render2DEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
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.orbit.EventHandler;
import net.minecraft.class_2561;
import net.minecraft.class_2583;
import net.minecraft.class_266;
import net.minecraft.class_269;
import net.minecraft.class_274;
import net.minecraft.class_5250;
import net.minecraft.class_5251;
import net.minecraft.class_640;
import net.minecraft.class_8646;
import net.minecraft.class_9014;
import net.minecraft.class_9015;
import net.minecraft.class_9020;
import net.minecraft.class_9022;

public class FakeStats
extends Module {
    private static final String OBJECTIVE_NAME = "meteor_scoreboard";
    private final SettingGroup sgGeneral;
    private final Setting<String> money;
    private final Setting<String> shards;
    private final Setting<String> kills;
    private final Setting<String> deaths;
    private final Setting<String> keyAll;
    private final Setting<String> playtime;
    private final Setting<String> team;
    private final Setting<String> title;
    private final Setting<String> region;
    private class_266 customObjective;
    private class_266 originalObjective;
    private String realKeyAll;
    private long lastScrapeTime;
    private long lastUpdateTime;

    public FakeStats() {
        super(Categories.Donut, "fake-stats", "Custom scoreboard overlay.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.money = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("money")).defaultValue("47m")).build());
        this.shards = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("shards")).defaultValue("40")).build());
        this.kills = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("kills")).defaultValue("562")).build());
        this.deaths = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("deaths")).defaultValue("15")).build());
        this.keyAll = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("key-all")).defaultValue("17m 6s")).build());
        this.playtime = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("playtime")).defaultValue("3d 4h")).build());
        this.team = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("team")).defaultValue("meteor team")).build());
        this.title = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("title")).defaultValue("Donut SMP")).build());
        this.region = this.sgGeneral.add(((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("ping")).defaultValue("meteor")).build());
        this.realKeyAll = "";
        this.lastScrapeTime = 0L;
        this.lastUpdateTime = 0L;
    }

    @Override
    public void onActivate() {
        this.realKeyAll = "";
        this.findOriginalObjective();
    }

    @Override
    public void onDeactivate() {
        this.restoreOriginalScoreboard();
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        if (this.mc.field_1724 == null || this.mc.field_1687 == null) {
            return;
        }
        this.scrapeServerStats();
        this.updateCustomScoreboard();
    }

    @EventHandler
    private void onRender2D(Render2DEvent event) {
        if (this.mc.field_1724 == null || this.mc.field_1687 == null || this.customObjective == null) {
            return;
        }
        class_269 scoreboard = this.mc.field_1687.method_8428();
        class_266 current = scoreboard.method_1189(class_8646.field_45157);
        if (current != this.customObjective) {
            scoreboard.method_1158(class_8646.field_45157, this.customObjective);
        }
    }

    private void findOriginalObjective() {
        if (this.mc.field_1687 == null) {
            return;
        }
        class_269 scoreboard = this.mc.field_1687.method_8428();
        class_266 current = scoreboard.method_1189(class_8646.field_45157);
        if (current != null && !current.method_1113().equals(OBJECTIVE_NAME)) {
            this.originalObjective = current;
            return;
        }
        if (this.originalObjective != null && !scoreboard.method_1151().contains(this.originalObjective)) {
            this.originalObjective = null;
        }
        if (this.originalObjective == null) {
            for (class_266 obj : scoreboard.method_1151()) {
                if (obj.method_1113().equals(OBJECTIVE_NAME) || obj.method_1118() != class_274.class_275.field_1472) continue;
                this.originalObjective = obj;
                break;
            }
        }
    }

    private void scrapeServerStats() {
        if (this.mc.field_1687 == null || System.currentTimeMillis() - this.lastScrapeTime < 1000L) {
            return;
        }
        this.lastScrapeTime = System.currentTimeMillis();
        this.findOriginalObjective();
        if (this.originalObjective == null) {
            return;
        }
        class_269 scoreboard = this.mc.field_1687.method_8428();
        for (class_9015 holder : scoreboard.method_1178()) {
            String clean;
            String text;
            String lower;
            if (!scoreboard.method_1166(holder).containsKey((Object)this.originalObjective) || !(lower = (text = holder.method_5820()).toLowerCase()).contains("keyall") && !lower.contains("key all") || (clean = text.replaceAll("(?i).*(key ?all)[: ]*", "").trim()).isEmpty()) continue;
            this.realKeyAll = clean;
        }
    }

    private void updateCustomScoreboard() {
        if (this.mc.field_1687 == null || System.currentTimeMillis() - this.lastUpdateTime < 1000L) {
            return;
        }
        this.lastUpdateTime = System.currentTimeMillis();
        class_269 scoreboard = this.mc.field_1687.method_8428();
        String currentTitle = "   " + this.title.get() + "   ";
        if (this.customObjective != null) {
            scoreboard.method_1194(this.customObjective);
        }
        this.customObjective = scoreboard.method_1168(OBJECTIVE_NAME, class_274.field_1468, this.parseGradientText(currentTitle, "#007CF9", "#00C6F9"), class_274.class_275.field_1472, true, (class_9022)class_9020.field_47557);
        scoreboard.method_1158(class_8646.field_45157, this.customObjective);
        List<class_2561> lines = this.getScoreboardLines();
        for (int i = 0; i < lines.size(); ++i) {
            class_2561 lineText = lines.get(i);
            String invisibleId = "\u00a7" + i + "\u00a7r";
            class_9015 holder = class_9015.method_55422((String)invisibleId);
            class_9014 score = scoreboard.method_1180(holder, this.customObjective);
            score.method_55410(lines.size() - i);
            score.method_55411(lineText);
            score.method_55412((class_9022)class_9020.field_47557);
        }
    }

    private List<class_2561> getScoreboardLines() {
        ArrayList<class_2561> lines = new ArrayList<class_2561>();
        lines.add((class_2561)class_2561.method_43470((String)" "));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"$ ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)64512)).method_10982(Boolean.valueOf(true)))).method_10852((class_2561)class_2561.method_43470((String)"Money ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)this.money.get()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)64512)))));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"\u2605 ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)10683385)))).method_10852((class_2561)class_2561.method_43470((String)"Shards ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)this.shards.get()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)10683385)))));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"\ud83d\udde1 ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFC0000)))).method_10852((class_2561)class_2561.method_43470((String)"Kills ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)this.kills.get()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFC0000)))));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"\u2620 ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)16348675)))).method_10852((class_2561)class_2561.method_43470((String)"Deaths ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)this.deaths.get()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)16348675)))));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"\u231b ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)42236)))).method_10852((class_2561)class_2561.method_43470((String)"Key All ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)(this.realKeyAll.isEmpty() ? this.keyAll.get() : this.realKeyAll)).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)42236)))));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"\u231a ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)16573184)))).method_10852((class_2561)class_2561.method_43470((String)"Playtime ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)this.playtime.get()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)16573184)))));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)"\ud83e\ude93 ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)42236)))).method_10852((class_2561)class_2561.method_43470((String)"Team ").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xFFFFFF)))).method_10852((class_2561)class_2561.method_43470((String)this.team.get()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)42236)))));
        lines.add((class_2561)class_2561.method_43470((String)" "));
        lines.add((class_2561)class_2561.method_43470((String)"").method_10852((class_2561)class_2561.method_43470((String)(this.region.get() + " (")).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xA8A8A8)))).method_10852((class_2561)class_2561.method_43470((String)this.getPingString()).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)42236)))).method_10852((class_2561)class_2561.method_43470((String)")").method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)0xA8A8A8)))));
        return lines;
    }

    private void restoreOriginalScoreboard() {
        if (this.mc.field_1687 != null) {
            class_269 scoreboard = this.mc.field_1687.method_8428();
            if (this.customObjective != null) {
                scoreboard.method_1194(this.customObjective);
                this.customObjective = null;
            }
            if (this.originalObjective != null) {
                scoreboard.method_1158(class_8646.field_45157, this.originalObjective);
                this.originalObjective = null;
            }
        }
    }

    private String getPingString() {
        class_640 entry;
        if (this.mc.method_1562() != null && this.mc.field_1724 != null && (entry = this.mc.method_1562().method_2871(this.mc.field_1724.method_5667())) != null) {
            return entry.method_2959() + "ms";
        }
        return "0ms";
    }

    private class_2561 parseGradientText(String text, String startHex, String endHex) {
        if (text.isEmpty()) {
            return class_2561.method_43470((String)"");
        }
        Color startColor = Color.decode(startHex);
        Color endColor = Color.decode(endHex);
        class_5250 result = class_2561.method_43470((String)"");
        for (int i = 0; i < text.length(); ++i) {
            float progress = text.length() > 1 ? (float)i / (float)(text.length() - 1) : 0.0f;
            int r = (int)((float)startColor.getRed() + progress * (float)(endColor.getRed() - startColor.getRed()));
            int g = (int)((float)startColor.getGreen() + progress * (float)(endColor.getGreen() - startColor.getGreen()));
            int b = (int)((float)startColor.getBlue() + progress * (float)(endColor.getBlue() - startColor.getBlue()));
            int rgb = r << 16 | g << 8 | b;
            result.method_10852((class_2561)class_2561.method_43470((String)String.valueOf(text.charAt(i))).method_10862(class_2583.field_24360.method_27703(class_5251.method_27717((int)rgb)).method_10982(Boolean.valueOf(true))));
        }
        return result;
    }
}

