package com.dynamicwallpaper.core;

import java.util.ArrayList;
import java.util.List;

/**
 * Produces a plausible, evenly-spaced sun-position schedule for a set of
 * images, so a user who doesn't want to hand-enter altitude/azimuth values
 * for every image still gets a smooth sunrise-to-sunset-to-night arc.
 *
 * <p>This is a stylistic approximation, not an astronomical calculation: it
 * assumes the images are ordered around a single day/night cycle and spaces
 * them evenly across it. Users can override any individual value in the
 * table before generating the wallpaper.
 */
public final class SunPositionUtil {

    private static final double PEAK_ALTITUDE = 70.0;

    private SunPositionUtil() {
    }

    public record SunPosition(double altitude, double azimuth) {
    }

    /**
     * @param count number of images to distribute across one day/night cycle (must be &gt;= 2)
     */
    public static List<SunPosition> distribute(int count) {
        if (count < 2) {
            throw new IllegalArgumentException("Need at least 2 images to distribute sun positions");
        }
        List<SunPosition> positions = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            double t = (double) i / (count - 1);
            double altitude = PEAK_ALTITUDE * Math.sin(2 * Math.PI * t - Math.PI / 2);
            double azimuth = 360.0 * t;
            positions.add(new SunPosition(round(altitude), round(azimuth)));
        }
        return positions;
    }

    private static double round(double value) {
        return Math.round(value * 100.0) / 100.0;
    }
}
