/*
 * Photos: what the files of c8-photos share.
 */
#ifndef PHOTOS_H
#define PHOTOS_H

#include <stdbool.h>
#include <stdint.h>
#include <cairo.h>
#include <gio/gio.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-wayland.h>
#include "encode.h"

#define APP_ID "org.cont8nuum.Photos"

/* folder.c: what is in a folder, pictures and folders, read on a thread.
 * Entries are shared (the browser's list and the viewer's set), hence
 * the count. */
struct ph_entry {
	int refs;
	char *uri;
	char *name; // for display
	char *mime; // NULL for folders
	char *collate; // sort key
	bool folder;
	guint64 size;
	gint64 taken; // the date scan's: when taken, else modified (unix s)
	cairo_surface_t *thumb; // browse.c's, loaded when drawn
	struct c8ui_thumbnail *thumb_load;
	bool thumb_failed;
};
struct ph_entry *ph_entry_ref(struct ph_entry *e);
void ph_entry_unref(struct ph_entry *e);
/* entries: struct ph_entry, folders first, then by name as Files sorts;
 * NULL when the folder can't be read (error says why) */
typedef void (*ph_list_fn)(GPtrArray *entries, const char *error, void *data);
/* folders: list them too; done runs on the main loop unless cancelled */
void ph_list_folder(const char *uri, bool folders, GCancellable *cancel,
	ph_list_fn done, void *data);
/* An entry for one file, read now (a file opened by itself) */
struct ph_entry *ph_entry_for(const char *uri);
/* A new array of only the pictures, sharing the entries */
GPtrArray *ph_pictures_of(GPtrArray *entries);
/* Every picture under uri (hidden folders left out), newest first by
 * when it was taken (EXIF, else when it was modified), each date read
 * once and then kept in the cache; done on the main loop unless
 * cancelled */
void ph_list_dates(const char *uri, GCancellable *cancel, ph_list_fn done,
	void *data);
char *ph_parent_uri(const char *uri);
/* ~/Pictures, or home if there is none */
char *ph_pictures_uri(void);

/* picture.c: a picture decoded by glycin (each decoder in a sandbox),
 * animations frame by frame while playing */
enum ph_state {
	PH_LOADING,
	PH_READY,
	PH_FAILED,
};
struct ph_picture {
	char *uri;
	enum ph_state state;
	cairo_surface_t *surface; // the frame shown, when READY
	int width, height;
	char *error; // when FAILED: what to say
	bool animated;
	/* private */
	int refs, owners;
	GCancellable *cancel;
	void *image; // GlyImage
	bool playing, fetching, released;
	int64_t delay; // µs, the frame shown
	guint timer;
	void (*fn)(struct ph_picture *pic, void *data);
	void *data;
};
/* fn runs when it is decoded, failed, or the next frame is up */
struct ph_picture *ph_picture_load(const char *uri,
	void (*fn)(struct ph_picture *pic, void *data), void *data);
/* One more owner (the one loading it is the first) */
struct ph_picture *ph_picture_ref(struct ph_picture *pic);
/* An owner is done with it; after the last, fn is not called again */
void ph_picture_release(struct ph_picture *pic);
void ph_picture_set_playing(struct ph_picture *pic, bool playing);

/* viewer.c: one picture at a time, the others of its set a flip away */
struct c8ui_widget *viewer_new(void);
/* items: struct ph_entry pictures, referenced */
void viewer_show(GPtrArray *items, int index);
/* The set changed (a picture deleted): keeps what is shown if it is
 * still there */
void viewer_set_items(GPtrArray *items, int index);
GPtrArray *viewer_items(void);
int viewer_index(void);
struct ph_entry *viewer_current(void);
struct ph_picture *viewer_picture(void);
void viewer_go(int index);
/* Decode the one shown again (rotated) */
void viewer_reload(void);
void viewer_slideshow(bool on);
bool viewer_in_slideshow(void);
/* Cropping the one shown (not an animation): a box over it to drag by
 * its edges, corners or middle, ratio its width over its height or 0
 * for any. Flipping, zooming and the slide show wait until it ends. */
bool viewer_crop_begin(double ratio);
void viewer_crop_set_ratio(double ratio);
void viewer_crop_end(void);
bool viewer_cropping(void);
bool viewer_crop_key(const struct c8ui_event *ev);
/* The box, in the picture's pixels */
bool viewer_crop_get(int *x, int *y, int *w, int *h);
/* main.c hears about a new picture shown (or loaded), and a right
 * click or a tap (for the app bar) */
struct viewer_hooks {
	void (*changed)(void);
	void (*context)(void);
	void (*slideshow_ended)(void);
	void (*back)(void); // the mouse's back button
};
void viewer_set_hooks(const struct viewer_hooks *hooks);

/* browse.c: a folder's pictures and folders in a grid */
struct browse_hooks {
	void (*open_picture)(GPtrArray *pictures, int index);
	void (*changed)(void); // a new folder (title, back button)
	void (*context)(void); // right click, hold: the app bar
};
struct c8ui_widget *browse_new(const struct browse_hooks *hooks);
void browse_set_folder(const char *uri, const char *select);
const char *browse_folder(void);
/* The pictures shown, for a slide show */
GPtrArray *browse_pictures(void);
/* Up one folder; false at the top (Pictures, or /) */
bool browse_up(void);
void browse_refresh(void);
void browse_stealth_changed(void);
void browse_set_narrow(bool narrow);
/* Everything shown, folders too (NULL while reading) */
GPtrArray *browse_entries(void);
/* Back from the viewer: show the picture it was at */
void browse_select(const char *uri);
struct c8ui_widget *browse_list(void);
/* Every picture in Pictures by month (photos.ini's by_date), or a folder */
void browse_set_by_date(bool on);
bool browse_by_date(void);
/* The picture or folder picked in the grid shown, or NULL */
struct ph_entry *browse_selected(void);

/* meta.c: the file's metadata through gexiv2 (without it: none, and no
 * rotating) */
bool ph_can_rotate(const char *mime);
typedef void (*ph_done_fn)(const char *error, void *data);
/* Turns it quarter_turns clockwise (negative: anticlockwise) by its
 * EXIF orientation, on a thread */
void ph_rotate(const char *uri, int quarter_turns, ph_done_fn done,
	void *data);
/* Pairs of label and value, for the Details panel */
void ph_details(const char *uri, GPtrArray *pairs);
/* When it was taken by its EXIF, or 0; blocks */
gint64 ph_taken(const char *path);

/* prefs.c: ~/.config/cont8nuum/photos.ini */
enum ph_wheel {
	PH_WHEEL_FLIP, // the wheel goes through the pictures, Ctrl zooms
	PH_WHEEL_ZOOM,
};
struct ph_prefs {
	enum ph_wheel wheel;
	int slide_seconds;
	bool shuffle;
	bool by_date; // the grid: by month, or by folder
};
extern struct ph_prefs prefs;
void prefs_load(void);
void prefs_save(void);

#endif
