#ifndef WEATHER_H
#define WEATHER_H

#include <glib.h>
#include <glib/gstdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>

#define APP_ID "org.cont8nuum.Weather"
#define TILE_WIDGET_ID "c8-weather" /* the tile made from c8-weather.widget */
#define API "https://weathersnail.com"
/* A place's own tile on Start (RequestPin): this, then the place's id */
#define PLACE_TILE_PREFIX APP_ID "."

/* Everything comes metric; these say how it is shown */
enum ws_units {
	WS_FAHRENHEIT, // °F, mph, inches of mercury (the NWS's own)
	WS_CELSIUS, // °C, km/h, hPa
};

struct ws_day {
	char *date; // YYYY-MM-DD, the place's own
	char *name; // the NWS's: "This Afternoon", "Veterans Day", "Thursday"
	double high, low; // °C, NAN if not given
	int pop; // chance of precipitation, %
	char *icon; // "day:rain_showers"
	char *summary; // "Scattered Rain Showers"
	char *day_text, *night_text; // the NWS's words, may be NULL
};

struct ws_hour {
	GDateTime *time; // in the place's own time zone (its offset)
	double temp; // °C
	int pop;
	double wind_kmh;
	char *wind_dir; // "ESE"
	char *summary, *icon;
};

struct ws_forecast {
	GArray *days; // struct ws_day
	GArray *hours; // struct ws_hour
	char *office; // the NWS office, "LOT"
	char *time_zone; // "America/Los_Angeles"
	int64_t updated; // unix seconds
};

/* Weather now, from the nearest station (a METAR) */
struct ws_now {
	double temp, dewpoint; // °C, NAN if not known
	double humidity; // %, NAN
	double wind_dir; // degrees from, NAN when calm or variable
	double wind_kt, gust_kt; // NAN
	char *weather; // METAR present weather ("-RA"), may be NULL
	double pressure; // hPa, NAN
	char *trend; // "rising", "falling", "steady", may be NULL
	char *station; // "Seattle-Tacoma Intl, WA"
	int64_t time; // when it was observed
	/* What radar sees falling around the place, may be NULL */
	char *precip, *intensity; // "Rain", "light"
};

/* An NWS alert covering a place */
struct ws_alert {
	char *id, *event, *headline, *severity, *sender, *area;
	uint32_t color; // the NWS's colour for the event
	int64_t onset, ends; // 0 if not given
	GPtrArray *rings; // GArray of float: lon, lat, lon, lat… each ring
	bool cancelled; // a later one says it's over
};

struct ws_place {
	char *id; // ours ("c8-…"), or the account's marker id
	char *name; // "Seattle, WA"
	double lat, lon;
	bool remote; // a marker on the weathersnail account
	struct ws_forecast *forecast; // NULL until read
	struct ws_now *now; // NULL until read
	GPtrArray *alerts; // struct ws_alert, the alert list's
	bool loading; // the forecast is being read
	char *error; // why there is no forecast, NULL if fine
	int64_t fetched; // when the forecast came, 0: not yet
};

struct ws_settings {
	enum ws_units units;
	char *home; // the default place's id, may be NULL
};

extern struct ws_settings ws_settings;
/* The favourites: the account's markers while synced, else ours */
extern GPtrArray *ws_places; // struct ws_place
/* A place being looked at that isn't a favourite (found by searching):
 * refreshed like the rest; the app's to set and free */
extern struct ws_place *ws_preview;

/* store.c: weather.ini, the cache */
void ws_load(void);
struct ws_place *ws_place_new(const char *id, const char *name, double lat,
	double lon);
void ws_place_free(gpointer place);
struct ws_place *ws_place_find(const char *id);
/* The name to show: a made-up town in stealth mode (the real one stays
 * in the file and is what's looked up) */
const char *ws_place_label(const struct ws_place *place);
/* The place shown first: the default, else the first; NULL if none */
struct ws_place *ws_home(void);
/* name: "Seattle, WA"; NULL if it's there already (close enough to one) */
struct ws_place *ws_add_place(const char *name, double lat, double lon);
void ws_remove_place(struct ws_place *place); // frees it
void ws_set_home(struct ws_place *place);
void ws_save(void);
GPtrArray *ws_local_places(void); // ours, whichever ws_places is
void ws_use_places(GPtrArray *places); // NULL: ours again
char *ws_cache_path(const char *name); // ~/.cache/cont8nuum/weather/NAME
/* An id safe in a file name and a command: letters, digits, . _ -, not
 * starting with a dot, 64 at most. Ids come from servers; what isn't
 * one is left out */
bool ws_id_ok(const char *id);
struct ws_place *ws_place_near(GPtrArray *list, double lat, double lon);

/* http.c: curl, the request on its stdin (no token in argv) */
typedef void (*ws_http_fn)(int status, const char *body, void *data);
/* body: JSON to send (POST, PUT); token may be NULL */
void ws_http(const char *method, const char *url, const char *body,
	const char *token, ws_http_fn fn, void *data);
/* Into a file (fn gets body NULL) */
void ws_download(const char *url, const char *dest, ws_http_fn fn,
	void *data);
char *ws_json_escape(const char *text);
bool ws_allow_http(void); // C8_ACCOUNTS_ALLOW_HTTP, for tests

/* data.c: forecasts, conditions and alerts */
typedef void (*ws_change_fn)(struct ws_place *place, void *data);
/* Runs whenever something about a place came (place NULL: all of them,
 * or the list changed) */
void ws_on_change(ws_change_fn fn, void *data);
/* What the cache has, at once */
void ws_load_cached(struct ws_place *place);
/* From the net: the forecast and what it's like now; alerts too when
 * they're older than a few minutes */
void ws_refresh(struct ws_place *place);
void ws_refresh_all(void);
/* Forgets a place's forecast and alert links before it is freed */
void ws_forget(struct ws_place *place);
/* An alert's long text: fn gets description and instruction (either may
 * be NULL); NULL both when it couldn't be read */
typedef void (*ws_text_fn)(const char *description, const char *instruction,
	void *data);
void ws_alert_text(const char *id, ws_text_fn fn, void *data);
/* Places the places API knows that match a name or ZIP code: fn gets a
 * list of struct ws_found, NULL when the API couldn't be reached */
struct ws_found {
	char *label; // "Seattle, WA", "Seattle-Tacoma International Airport"
	char *name, *detail; // detail: "IL · city", may be ""
	double lat, lon;
};
typedef void (*ws_found_fn)(GPtrArray *found, void *data);
void ws_find_places(const char *query, ws_found_fn fn, void *data);

/* What things look like (format.c) */
/* The glyph Start and the app draw for an NWS icon ("night:sct"): clear,
 * night, partly, partly-night, cloudy, fog, rain, sleet, snow, thunder */
const char *ws_glyph(const char *icon);
bool ws_icon_is_night(const char *icon);
/* What it's like now: present weather in words, else the forecast's */
const char *ws_now_summary(struct ws_place *place);
const char *ws_now_icon(struct ws_place *place);
/* "54°" in the units chosen ("--" for NAN); units adds F or C */
char *ws_temp(double celsius, bool units);
char *ws_wind(double kt, double dir); // "NW 9 mph", "Calm"
char *ws_wind_kmh(double kmh, const char *dir);
char *ws_pressure(double hpa);
double ws_feels_like(struct ws_now *now); // °C
/* "Today", "Wed 11" */
char *ws_day_label(struct ws_day *day, int index);
/* "2 PM" or "14:00", by the desktop's clock setting */
char *ws_hour_label(GDateTime *time);
char *ws_age(int64_t when); // "Updated 5 minutes ago"
uint32_t ws_sky_top(const char *glyph), ws_sky_bottom(const char *glyph);
bool ws_clock_24h(void);

/* account.c: the places saved on a weathersnail account (PC settings'
 * Online accounts), instead of ours, while there is one */
extern bool ws_synced;
/* error NULL: ws_places changed; else what went wrong, worth a dialog
 * when loud */
typedef void (*ws_remote_fn)(const char *error, bool loud, void *data);
/* After ws_load. may_import: put our places on an account used for the
 * first time (the app does, the tile provider doesn't) */
void ws_account_init(ws_remote_fn fn, void *data, bool may_import);
void ws_account_sync(void);
const char *ws_account_error(void);
const char *ws_account_name(void); // who is signed in, NULL if unknown
void ws_account_add(struct ws_place *place); // a new marker for it
void ws_account_remove(struct ws_place *place); // frees it

/* tile.c: a live tile over org.cont8nuum.Tiles1 */
void ws_tile_update(const char *tile_id, struct ws_place *place);
/* The main tiles (the app's and the widget's) for the default place, and
 * every place's own tile that is pinned */
void ws_tiles_update(void);
char *ws_place_tile_id(struct ws_place *place);

#endif
