#ifndef C8_TILES_H
#define C8_TILES_H

#include <stdbool.h>
#include <stdint.h>
#include <cairo.h>
#include <glib.h>

#define APP_ID "org.cont8nuum.TileMaker"

/* store.c: the tiles made here and the settings, in
 * ~/.config/cont8nuum/tilemaker.ini; their pictures in
 * ~/.local/share/cont8nuum/tilemaker/ */

enum tm_size { TM_SMALL, TM_MEDIUM, TM_WIDE, TM_LARGE };
extern const char *const tm_size_names[4];

struct tm_tile {
	char *id; // Start's tile id: APP_ID "." something
	char *name, *exec, *icon; // icon: a theme name or absolute path
	char *image, *wide_image; // absolute paths, or NULL
	uint32_t color;
	enum tm_size size;
	bool label;
	/* For games: where it came from, so art can be found for it */
	char *steam_appid; // NULL unless a Steam game
	int sgdb_id; // SteamGridDB's game id once known, else 0
	/* The command Start has for it, when pinned from here: changes to
	 * the face alone go to Start without asking while it's the same */
	char *pinned_exec;
};

extern GPtrArray *tm_tiles; // struct tm_tile
extern char *tm_sgdb_key; // NULL until given (STEAMGRIDDB_API_KEY wins)

void tm_load(void);
void tm_save(void);
struct tm_tile *tm_tile_new(void); // a fresh id, added to tm_tiles
struct tm_tile *tm_tile_find(const char *id);
void tm_tile_delete(struct tm_tile *t); // its pictures too
/* Where a tile's picture goes: a new name each time, so Start loads it
 * again; ext with its dot */
char *tm_picture_path(struct tm_tile *t, const char *kind, const char *ext);
/* Copies a picture the user chose into our folder (Start must be able to
 * read it there later); NULL if it can't */
char *tm_picture_import(struct tm_tile *t, const char *kind,
	const char *path);
/* Deletes our own picture files not used by any tile */
void tm_pictures_prune(void);
const char *tm_key(void);

/* http.c: curl as a subprocess */

typedef void (*tm_http_fn)(int status, const char *body, void *data);
/* auth: a bearer token or NULL */
void tm_http_get(const char *url, const char *auth, tm_http_fn fn,
	void *data);
/* Into dest (via dest.part); body is NULL */
void tm_download(const char *url, const char *dest, tm_http_fn fn,
	void *data);

/* games.c: games installed through Steam, Lutris, or with a desktop
 * entry in the Game category */

enum tm_source { TM_STEAM, TM_LUTRIS, TM_DESKTOP };

struct tm_game {
	char *name, *exec;
	enum tm_source source;
	char *steam_appid; // TM_STEAM
	char *icon; // a theme name or path, if any
	char *wide; // a local wide picture (Steam's header), if any
	char *tall; // a local cover, if any
};

typedef void (*tm_games_fn)(GPtrArray *games, void *data);
/* Scans (Lutris answers on its own time); fn gets struct tm_game items,
 * sorted by name, and owns nothing: the array is ours until next scan */
void tm_games_scan(tm_games_fn fn, void *data);
const char *tm_source_name(enum tm_source s);
/* A desktop entry's command line without its %U-style codes */
char *tm_strip_codes(const char *cmd);

/* sgdb.c: SteamGridDB's API v2 */

enum tm_art { TM_ART_WIDE, TM_ART_SQUARE, TM_ART_ICON, TM_ART_HERO,
	TM_ART_COUNT };

struct tm_art_item {
	char *url, *thumb, *author;
	int width, height;
};

struct tm_sgdb_game {
	int id;
	char *name;
	int year;
};

/* error: NULL or why (no key, refused...) */
typedef void (*tm_sgdb_games_fn)(GPtrArray *games, const char *error,
	void *data);
typedef void (*tm_sgdb_art_fn)(enum tm_art kind, GPtrArray *items,
	const char *error, void *data);
void tm_sgdb_search(const char *term, tm_sgdb_games_fn fn, void *data);
void tm_sgdb_steam(const char *appid, tm_sgdb_games_fn fn, void *data);
void tm_sgdb_art(int game, enum tm_art kind, tm_sgdb_art_fn fn, void *data);
void tm_sgdb_game_free(gpointer p);
void tm_art_item_free(gpointer p);

/* picture.c: pictures decoded on a thread, kept by URL or path */

typedef void (*tm_picture_fn)(void *data);
/* The picture (a URL or a path), or NULL while it loads or if it failed;
 * max: the longest side it's shrunk to. Kept until forgotten. */
cairo_surface_t *tm_picture(const char *ref, int max);
/* Runs whenever one tm_picture asked for has come */
void tm_pictures_on_ready(tm_picture_fn fn, void *data);

#endif
