package net.mcreator.phayriosisreborn.client.gui;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.mcreator.phayriosisreborn.config.PhayriosisPendingWorldSettings;

/** Lightweight animated preview used only by the Create World meteor settings page. */
public final class PhayriosisMeteorPreview {
    private static final ResourceLocation BACKGROUND = new ResourceLocation("phayriosisreborn:textures/gui/worldsettings/meteor_preview_states.png");
    private static final ResourceLocation EARTH = new ResourceLocation("phayriosisreborn:textures/gui/worldsettings/earth_rotation_8f_2x.png");
    private static final ResourceLocation MOON = new ResourceLocation("phayriosisreborn:textures/gui/worldsettings/moon.png");
    private static final ResourceLocation METEOR = new ResourceLocation("phayriosisreborn:textures/gui/worldsettings/meteor_orbit_8dir.png");
    private static final ResourceLocation ICON = new ResourceLocation("phayriosisreborn:textures/gui/worldsettings/meteor_icon_16.png");

    private static final int PREVIEW_W = 154;
    private static final int PREVIEW_H = 130;

    private PhayriosisMeteorPreview() {}

    public static void render(GuiGraphics graphics, int screenWidth, int screenHeight) {
        int cx = screenWidth / 2;
        int tabY = Math.max(12, screenHeight / 2 - 112);
        int x = cx - 202;
        int y = tabY + 28;

        int state = backgroundState();
        graphics.m_280163_(BACKGROUND, x, y, (float)(state * PREVIEW_W), 0.0F, PREVIEW_W, PREVIEW_H, PREVIEW_W * 5, PREVIEW_H);

        long now = System.currentTimeMillis();
        int earthFrame = (int)((now / 240L) & 7L);
        int earthX = x + 47;
        int earthY = y + 34;
        graphics.m_280163_(EARTH, earthX, earthY, (float)(earthFrame * 64), 0.0F, 64, 64, 512, 64);

        double moonAngle = (now % 9000L) / 9000.0D * Math.PI * 2.0D;
        int moonX = earthX + 32 + (int)Math.round(Math.cos(moonAngle) * 49.0D) - 8;
        int moonY = earthY + 32 + (int)Math.round(Math.sin(moonAngle) * 25.0D) - 8;
        graphics.m_280163_(MOON, moonX, moonY, 0.0F, 0.0F, 16, 16, 16, 16);

        if (PhayriosisPendingWorldSettings.meteorEnabled) {
            double radius = meteorRadius(PhayriosisPendingWorldSettings.meteorMinimumDay);
            double angle = -((now % 12000L) / 12000.0D * Math.PI * 2.0D) - 0.65D;
            int centerX = earthX + 32;
            int centerY = earthY + 32;
            int meteorX = centerX + (int)Math.round(Math.cos(angle) * radius) - 12;
            int meteorY = centerY + (int)Math.round(Math.sin(angle) * radius * 0.58D) - 12;

            // The sprite points toward Earth. Eight authored direction frames keep it crisp.
            double toward = Math.atan2(centerY - (meteorY + 12), centerX - (meteorX + 12));
            int frame = directionFrame(toward);
            int u = (frame & 3) * 24;
            int v = (frame >> 2) * 24;
            graphics.m_280163_(METEOR, meteorX, meteorY, (float)u, (float)v, 24, 24, 96, 48);
        }

        graphics.m_280163_(ICON, x + 7, y + 7, 0.0F, 0.0F, 16, 16, 16, 16);
    }

    private static int backgroundState() {
        if (!PhayriosisPendingWorldSettings.meteorEnabled) return 0;
        double day = PhayriosisPendingWorldSettings.meteorMinimumDay;
        if (day <= 1.0D) return 1;
        if (day <= 4.0D) return 2;
        if (day <= 9.0D) return 3;
        return 4;
    }

    private static double meteorRadius(double day) {
        // Existing choices are 0, 3, 7 and 14 days. Earlier means visually closer.
        if (day <= 0.0D) return 42.0D;
        if (day <= 3.0D) return 48.0D;
        if (day <= 7.0D) return 57.0D;
        return 68.0D;
    }

    private static int directionFrame(double radians) {
        double turns = radians / (Math.PI * 2.0D);
        int eighth = (int)Math.round(turns * 8.0D);
        eighth %= 8;
        if (eighth < 0) eighth += 8;
        // Source frame zero points east; baked frames advance clockwise.
        return eighth;
    }
}
