/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1657
 *  net.minecraft.class_243
 *  net.minecraft.class_5498
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.settings.ColorSetting;
import meteordevelopment.meteorclient.settings.DoubleSetting;
import meteordevelopment.meteorclient.settings.IntSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1657;
import net.minecraft.class_243;
import net.minecraft.class_5498;

public class RoninHat
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Double> scale;
    private final Setting<Double> yOffset;
    private final Setting<Integer> segments;
    private final Setting<SettingColor> hatColor;

    public RoninHat() {
        super(Categories.Donut, "ronin-hat", "A Samurai hat that stays locked to the head model.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.scale = this.sgGeneral.add(((DoubleSetting.Builder)new DoubleSetting.Builder().name("scale")).defaultValue(1.0).min(0.5).sliderMax(2.0).build());
        this.yOffset = this.sgGeneral.add(((DoubleSetting.Builder)new DoubleSetting.Builder().name("y-offset")).defaultValue(0.15).min(-1.0).sliderMax(1.0).build());
        this.segments = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("smoothness")).defaultValue(32)).min(8).sliderMax(64).build());
        this.hatColor = this.sgGeneral.add(((ColorSetting.Builder)new ColorSetting.Builder().name("hat-color")).defaultValue(new SettingColor(180, 160, 120, 200)).build());
    }

    @EventHandler
    private void onRender(Render3DEvent event) {
        if (this.mc.field_1687 == null || this.mc.field_1724 == null) {
            return;
        }
        for (class_1657 player : this.mc.field_1687.method_18456()) {
            if (player == this.mc.field_1724 && this.mc.field_1690.method_31044() == class_5498.field_26664) continue;
            this.renderHat(event, player);
        }
    }

    private void renderHat(Render3DEvent event, class_1657 player) {
        float delta = event.tickDelta;
        class_243 lerped = player.method_30950(delta);
        float yaw = player.method_36454();
        float pitch = player.method_36455();
        double eyeHeight = player.method_18381(player.method_18376());
        class_243 pivot = new class_243(lerped.field_1352, lerped.field_1351 + eyeHeight, lerped.field_1350);
        if (player.method_5715()) {
            pivot = pivot.method_1031(0.0, -0.125, 0.0);
        }
        this.drawHat(event, pivot, yaw, pitch);
    }

    private void drawHat(Render3DEvent event, class_243 pivot, float yaw, float pitch) {
        int segs = this.segments.get();
        double s = this.scale.get();
        double radius = 0.45 * s;
        double height = 0.22 * s;
        double offset = this.yOffset.get();
        SettingColor color = this.hatColor.get();
        SettingColor lineColor = new SettingColor(color.r, color.g, color.b, 255);
        class_243 tip = this.rotate(new class_243(0.0, offset + height, 0.0), yaw, pitch).method_1019(pivot);
        for (int i = 0; i < segs; ++i) {
            double angle1 = (double)i / (double)segs * Math.PI * 2.0;
            double angle2 = (double)(i + 1) / (double)segs * Math.PI * 2.0;
            class_243 p1Rel = new class_243(Math.cos(angle1) * radius, offset, Math.sin(angle1) * radius);
            class_243 p2Rel = new class_243(Math.cos(angle2) * radius, offset, Math.sin(angle2) * radius);
            class_243 p1 = this.rotate(p1Rel, yaw, pitch).method_1019(pivot);
            class_243 p2 = this.rotate(p2Rel, yaw, pitch).method_1019(pivot);
            event.renderer.quad(p1.field_1352, p1.field_1351, p1.field_1350, p2.field_1352, p2.field_1351, p2.field_1350, tip.field_1352, tip.field_1351, tip.field_1350, tip.field_1352, tip.field_1351, tip.field_1350, color);
            event.renderer.line(p1.field_1352, p1.field_1351, p1.field_1350, p2.field_1352, p2.field_1351, p2.field_1350, lineColor);
            event.renderer.line(p1.field_1352, p1.field_1351, p1.field_1350, tip.field_1352, tip.field_1351, tip.field_1350, lineColor);
        }
    }

    private class_243 rotate(class_243 vec, float yaw, float pitch) {
        float p = (float)Math.toRadians(pitch);
        float y = (float)Math.toRadians(-yaw);
        double cosP = Math.cos(p);
        double sinP = Math.sin(p);
        double y1 = vec.field_1351 * cosP - vec.field_1350 * sinP;
        double z1 = vec.field_1351 * sinP + vec.field_1350 * cosP;
        double cosY = Math.cos(y);
        double sinY = Math.sin(y);
        double x2 = vec.field_1352 * cosY + z1 * sinY;
        double z2 = -vec.field_1352 * sinY + z1 * cosY;
        return new class_243(x2, y1, z2);
    }
}

