#ifndef NEWS_H
#define NEWS_H

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

#define APP_ID "org.cont8nuum.News"
#define TILE_WIDGET_ID "news" /* the id of the tile made from news.widget */

struct feed;

struct story {
	struct feed *feed;
	char *id; // the feed's own id for it (guid, else link): what is remembered
	char *title, *summary, *link, *author, *image_url;
	/* The body: as Pango markup once asked for (news_story_markup), its
	 * HTML until then; either may be NULL */
	char *markup, *html;
	int64_t date; // unix seconds
	bool read;
	int64_t post; // rssnail's id for it, 0: from a feed read here
};

struct feed {
	char *url;
	char *name; // what is shown
	bool named; // the user gave the name: the feed's own doesn't replace it
	char *site; // the site's page
	char *logo; // the feed's own picture (RSS <image>, Atom <icon>), may be NULL
	char *favicon; // what stands in for it: see news_feed_logo
	GPtrArray *stories; // struct story, newest first
	char *etag, *modified;
	bool loading, failed;
	char *error;
	int64_t fetched; // when it was last read from the net, 0: not yet
	bool rediscovered; // already looked for the real feed address once
	int64_t remote; // rssnail's subscription id, 0: followed here
};

struct settings {
	bool images; // show pictures
	int text_size; // 0 small, 1 medium, 2 large
	int interval; // minutes between refreshes
};

extern struct settings news_settings;
extern GPtrArray *news_feeds; // struct feed

/* store.c: the feeds, the settings and what has been read */
void news_load(void);
struct feed *news_feed_new(const char *url, const char *name);
void news_feed_free(gpointer feed);
/* The feeds followed here, whichever news_feeds is */
GPtrArray *news_local_feeds(void);
/* news_feeds becomes these (rssnail's), or the local ones again (NULL) */
void news_use_feeds(GPtrArray *feeds);
void news_save_feeds(void);
void news_save_settings(void);
/* NULL if it is there already, or when synced (rssnail adds it, and
 * news_remote_fn tells when it's there) */
struct feed *news_add_feed(const char *url, const char *name);
void news_remove_feed(struct feed *feed);
char *news_cache_path(const char *name); // under ~/.cache/cont8nuum/news
bool news_is_read(const char *id, const char *feed_url);
void news_set_read(struct story *story, bool read);
void news_mark_all_read(void);
int news_unread(void);
/* Saves what changed after a moment; an app that saves nothing (the tile
 * provider) never calls it */
void news_flush(void);

/* fetch.c: curl, in the background */
/* status: the HTTP status (200 also for a file:// read), 304 not modified,
 * 0 if it could not be fetched */
typedef void (*fetch_fn)(int status, const char *etag, const char *modified,
	void *data);
void fetch_start(const char *url, const char *dest, const char *etag,
	const char *modified, fetch_fn fn, void *data);

/* feed.c */
typedef void (*feed_done_fn)(struct feed *feed, bool changed, void *data);
void feed_load_cache(struct feed *feed);
/* feed may go while it runs: fn is not called then */
void feed_refresh(struct feed *feed, feed_done_fn fn, void *data);
void feed_forget(struct feed *feed); // before it is freed
/* Newest first, up to max; the stories are the feeds' */
GPtrArray *news_latest(int max);
char *news_age(int64_t date); // "2 h", "yesterday"
/* The address of the source's little picture: its own, else the site's
 * favicon.ico; NULL if there's no site to ask */
const char *news_feed_logo(struct feed *feed);
/* The story's body as Pango markup, or NULL without one. Made when first
 * asked for: parsing every story's at each launch held the window up. */
const char *news_story_markup(struct story *story);
void news_story_free(struct story *story);
/* The feed address from a page's <link rel=alternate>, resolved against
 * base; NULL if none */
char *feed_discover(const char *html, size_t len, const char *base);

/* html.c */
/* Pango markup for an article's HTML; *first_image gets the first real
 * picture's address (g_free it) if given */
char *html_to_markup(const char *html, char **first_image);
/* Plain text, up to max characters */
char *html_to_text(const char *html, size_t max);

/* image.c: pictures, fetched into the cache and decoded on demand */
typedef void (*image_fn)(const char *url, void *data);
void news_image_on_ready(image_fn fn, void *data);
/* The picture (no more than 480 px), or NULL while it comes or if it
 * can't; asking starts the download. Not the caller's: do not destroy. */
cairo_surface_t *news_image(const char *url);
/* Set whenever news_image had to say NULL for one that is on its way
 * (downloading or being decoded): what was drawn then is not how it will
 * look. Whoever keeps drawings clears it before and looks after. */
extern bool news_image_missed;
/* The same up to 1920 px, for a picture across the window; only the last
 * two are kept. The small one until it's decoded, NULL while it comes. */
cairo_surface_t *news_image_large(const char *url);
/* The file, if the picture has been downloaded and is readable */
const char *news_image_path(const char *url);

/* rssnail.c: the feeds of an rssnail account from PC settings' Online
 * accounts, instead of the ones followed here, while there is one */
extern bool news_synced;
/* error NULL: news_feeds changed (let go of the old ones' feeds now, and
 * refresh); else something went wrong, worth a dialog when loud */
typedef void (*news_remote_fn)(const char *error, bool loud, void *data);
/* After news_load, before feed_load_cache. may_import: add the feeds
 * followed here to an account used the first time (the app, not the
 * tile provider as well) */
void rssnail_init(news_remote_fn fn, void *data, bool may_import);
void rssnail_sync(void); // the subscriptions again
const char *rssnail_error(void); // the last problem, NULL if none
void rssnail_add(const char *url);
void rssnail_remove(struct feed *feed); // frees it
void rssnail_set_read(struct story *story); // as story->read says
void rssnail_mark_all_read(void);
/* For feed.c: a feed's posts into dest, and the stories in them */
void rssnail_posts(struct feed *feed, const char *dest, fetch_fn fn,
	void *data);
GPtrArray *rssnail_parse_posts(struct feed *feed, const char *text);

/* tile.c: the live tile over org.cont8nuum.Tiles1 */
void news_tile_update(const char *tile_id);

#endif
