import { renderSvgIcon } from "../../graphics/icons";
import { type GrayImage, type UiFont } from "../../graphics/image";
import { type WeatherCondition, type WeatherState } from "../../native/weather";
import { formatTemperature } from "./temperature-format";
// Lucide weather icons (ISC licensed), kept verbatim so they can be diffed
// against upstream.
const WEATHER_SVGS = {
sun:
'',
moon:
'',
cloud:
'',
"cloud-sun":
'',
"cloud-moon":
'',
"cloud-fog":
'',
wind:
'',
"cloud-drizzle":
'',
"cloud-sun-rain":
'',
"cloud-moon-rain":
'',
"cloud-rain":
'',
"cloud-lightning":
'',
"cloud-snow":
'',
"cloud-hail":
'',
} as const;
type WeatherIconName = keyof typeof WEATHER_SVGS;
/** Status bar and system card hide weather whose last refresh is older than this. */
const INDICATOR_STALE_MS = 3 * 60 * 60 * 1000;
/** Gap between a reading's icon and its temperature. */
const READING_ICON_GAP = 3;
function weatherIconName(condition: WeatherCondition, isDaytime: boolean): WeatherIconName {
switch (condition) {
case "clear":
return isDaytime ? "sun" : "moon";
case "partly-cloudy":
return isDaytime ? "cloud-sun" : "cloud-moon";
case "cloudy":
return "cloud";
case "fog":
return "cloud-fog";
case "wind":
return "wind";
case "drizzle":
return "cloud-drizzle";
case "showers":
return isDaytime ? "cloud-sun-rain" : "cloud-moon-rain";
case "rain":
return "cloud-rain";
case "thunderstorm":
return "cloud-lightning";
case "snow":
return "cloud-snow";
case "sleet":
return "cloud-hail";
}
}
/** A size×size icon for the condition (sun or moon variants by time of day), rendered once and cached. */
export function renderWeatherIcon(condition: WeatherCondition, isDaytime: boolean, size: number): GrayImage | null {
const name = weatherIconName(condition, isDaytime);
return renderSvgIcon(`weather:${name}`, WEATHER_SVGS[name], size);
}
/** Current conditions as the status bar and system card show them. */
export type WeatherReading = {
condition: WeatherCondition | null;
isDaytime: boolean;
/** Rounded, unit-less ("72°"), to stay compact. */
temperature: string;
};
/** The current reading, or null when there is none or it has gone stale. */
export function weatherReading(state: WeatherState, nowMs: number): WeatherReading | null {
const current = state.current;
if (!current || state.lastUpdatedMs === null || nowMs - state.lastUpdatedMs > INDICATOR_STALE_MS) return null;
return {
condition: current.condition,
isDaytime: current.isDaytime,
temperature: current.temperatureF === null ? "--°" : formatTemperature(current.temperatureF, false),
};
}
/** Width drawWeatherReading takes for this reading. */
export function measureWeatherReading(reading: WeatherReading, font: UiFont, iconSize: number): number {
return (reading.condition ? iconSize + READING_ICON_GAP : 0) + font.measureText(reading.temperature);
}
/** The condition icon then the temperature, each centred vertically in the `height` rows from `top`. */
export function drawWeatherReading(
image: GrayImage,
reading: WeatherReading,
font: UiFont,
iconSize: number,
x: number,
top: number,
height: number,
shade: number,
): void {
if (reading.condition) {
const icon = renderWeatherIcon(reading.condition, reading.isDaytime, iconSize);
if (icon) image.drawImage(icon, x, top + (((height - icon.height) / 2) | 0));
x += iconSize + READING_ICON_GAP;
}
image.drawText(font, x, top + Math.max(0, ((height - font.lineHeight) / 2) | 0), reading.temperature, shade);
}