/*
 * Video: what the files of c8-video share.
 */
#ifndef VIDEO_H
#define VIDEO_H

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

#define APP_ID "org.cont8nuum.Video"

/* folder.c: what is in a folder, videos and folders, read on a thread.
 * Entries are shared (the grid's list and the play list), hence the
 * count. */
struct vd_entry {
	int refs;
	char *uri;
	char *name; // for display
	char *mime; // NULL for folders
	char *collate; // sort key
	bool folder;
	guint64 size;
	cairo_surface_t *thumb; // browse.c's, loaded when drawn
	struct c8ui_thumbnail *thumb_load;
	bool thumb_failed;
};
struct vd_entry *vd_entry_ref(struct vd_entry *e);
void vd_entry_unref(struct vd_entry *e);
/* entries: struct vd_entry, folders first, then by name as Files sorts;
 * NULL when the folder can't be read (error says why) */
typedef void (*vd_list_fn)(GPtrArray *entries, const char *error, void *data);
/* folders: list them too; done runs on the main loop unless cancelled */
void vd_list_folder(const char *uri, bool folders, GCancellable *cancel,
	vd_list_fn done, void *data);
/* An entry for one file or address, read now */
struct vd_entry *vd_entry_for(const char *uri);
/* A new array of only the videos, sharing the entries */
GPtrArray *vd_videos_of(GPtrArray *entries);
char *vd_parent_uri(const char *uri);
/* ~/Videos, or home if there is none */
char *vd_videos_uri(void);
bool vd_is_video(const char *mime);

/* browse.c: a folder's videos and folders in a grid */
struct browse_hooks {
	void (*open_video)(GPtrArray *videos, int index);
	void (*changed)(void); // a new folder
	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);
/* Up one folder; false at the top (Videos, or /) */
bool browse_up(void);
void browse_refresh(void);
void browse_set_narrow(bool narrow);
/* Everything shown, folders too (NULL while reading) */
GPtrArray *browse_entries(void);
/* Back from the player: show the video it was at */
void browse_select(const char *uri);
struct c8ui_widget *browse_list(void);

/* player.c: mpv plays; its picture goes on a surface of our own under
 * the window's, drawn with OpenGL, where 3D and all-round videos are
 * taken apart. */

/* How the two eyes' pictures lie in a 3D video's frame */
enum vd_layout {
	VD_MONO, // not 3D
	VD_SBS, // side by side
	VD_TB, // one over the other
	VD_ANAGLYPH, // one picture made for red and cyan glasses
	VD_LAYOUT_COUNT,
};
/* What the picture is of */
enum vd_shape {
	VD_FLAT,
	VD_SPHERE, // all round you (equirectangular 360)
	VD_HALF, // in front of you (equirectangular 180)
	VD_FISHEYE, // in front of you, as a fisheye lens saw it (180)
	VD_SHAPE_COUNT,
};
/* How a 3D one is shown on a flat screen */
enum vd_show {
	VD_ONE_EYE, // the left eye's picture: 2D
	VD_RED_CYAN, // for coloured glasses
	VD_GREEN_MAGENTA,
	VD_AMBER_BLUE,
	VD_BOTH, // the frame as it is
	VD_SHOW_COUNT,
};
struct vd_view {
	enum vd_layout layout;
	bool swap; // the right eye's picture is first (left, or on top)
	enum vd_shape shape;
	enum vd_show show;
	/* Looking round a sphere: radians, and the height of the view */
	double yaw, pitch, fov;
};
#define VD_FOV_DEFAULT 1.3
#define VD_FOV_MIN 0.5
#define VD_FOV_MAX 2.0

enum vd_change {
	VD_CH_STATE = 1 << 0, // loaded, paused, ended, buffering, error
	VD_CH_TIME = 1 << 1, // position, duration
	VD_CH_VOLUME = 1 << 2,
	VD_CH_TITLE = 1 << 3,
	VD_CH_TRACKS = 1 << 4,
	VD_CH_PICTURE = 1 << 5, // its size, or what mpv says of its 3D
	VD_CH_LOADED = 1 << 6, // a file has just started
	VD_CH_CHAPTERS = 1 << 7, // the list, or which one it is in
	VD_CH_SUBTITLES = 1 << 8, // the line shown, or who draws it
};

struct vd_chapter {
	double time; // where it starts, seconds
	char *title; // never NULL
};
struct vd_state {
	bool active; // a file is open, or being opened
	bool loaded; // and it has started
	bool paused, ended, buffering, seekable;
	double position, duration; // seconds; duration 0: not known
	double volume; // 0 to 1
	bool muted;
	double speed;
	int width, height; // the frame's pixels; 0: no picture (music)
	double aspect; // as it should look, width over height
	char *title;
	char *stereo; // mpv's name for the 3D layout it found, or NULL
	char *error; // why the last file didn't play, until the next
	GArray *chapters; // struct vd_chapter, in order; never NULL
	int chapter; // the one playing, -1 before the first
	/* Subtitles we draw over the picture ourselves: mpv's would land
	 * across both eyes' pictures, or behind you in an all-round one */
	bool own_subtitles;
	char *subtitle; // the line now, or NULL
	const struct vd_subpicture *subpicture; // or a picture (bitsubs.c's)
};
extern struct vd_state vd;

struct vd_track {
	char type; // 'a' audio, 's' subtitles, 'v' video
	int id;
	char *label;
	bool selected;
};

/* False when mpv can't start: error says why */
bool player_init(struct c8ui_app *app, char **error);
void player_shutdown(void);
void player_on_change(void (*fn)(unsigned changes));
/* fn runs just before a file that played is let go (another opened,
 * stopped, the window closed), while its frame can still be had */
void player_on_leave(void (*fn)(void));
/* The frame now, width pixels wide, as a PNG at path */
bool player_snapshot(const char *path, int width);
/* The window the picture goes under; it must be see-through there */
void player_attach(struct c8ui_window *win);
void player_detach(void);
/* The window's size or scale changed */
void player_resize(void);
void player_load(const char *uri);
/* Remembers where it was (if the option is on) and lets the file go */
void player_stop(void);
void player_set_paused(bool paused);
void player_seek(double seconds);
void player_seek_by(double seconds);
void player_set_volume(double volume);
void player_set_muted(bool muted);
void player_set_speed(double speed);
void player_step(bool back); // one frame
void player_set_chapter(int index);
/* The tracks of one type; free with player_tracks_free */
GArray *player_tracks(char type);
void player_tracks_free(GArray *tracks);
/* id 0: none (subtitles off) */
void player_set_track(char type, int id);
void player_add_subtitles(const char *uri);
/* Where one eye's picture is in the window, as shown now */
struct c8ui_rect player_picture(void);
const struct vd_view *player_view(void);
void player_set_view(const struct vd_view *view);
/* bitsubs.c: subtitles that are pictures (Blu-ray, DVD), decoded by us
 * from the file for when mpv's would be cut between the eyes. Pieces in
 * the canvas's pixels, which is one eye's picture as it was made. */
struct vd_subrect {
	int x, y, w, h;
	cairo_surface_t *pic;
};
struct vd_subpicture {
	double start, end; // seconds; end INFINITY: until the next
	int canvas_w, canvas_h;
	int count;
	struct vd_subrect *rects;
};
/* index: the subtitle stream (FFmpeg's), -1: the first; fallback_w, _h:
 * the canvas when the stream has none. changed: something new was read,
 * or (failed) it can't be read: then it has closed itself. */
bool bitsubs_open(const char *path, int index, int fallback_w,
	int fallback_h, void (*changed)(bool failed));
void bitsubs_close(void);
bool bitsubs_running(void);
/* Where mpv plays now: read ahead of it */
void bitsubs_tell(double pos);
/* What shows at pos, or NULL; valid until the next bitsubs call */
const struct vd_subpicture *bitsubs_at(double pos);

/* views.c: what a file's picture is (3D, all round), guessed from what
 * mpv and its name say, and kept once you said otherwise */
void views_guess(const char *uri, const char *stereo, struct vd_view *view);
void views_remember(const char *uri, const struct vd_view *view);

/* stage.c: the player's page: nothing but the video, with the controls
 * over it while the pointer moves */
struct stage_hooks {
	void (*back)(void);
	void (*context)(void); // right click: the app bar
	void (*fullscreen)(bool on);
	void (*previous)(void);
	void (*next)(void);
	bool (*busy)(void); // something of the app's is up: keep the controls
};
struct c8ui_widget *stage_new(const struct stage_hooks *hooks);
/* Once it is in its window */
void stage_attach(struct c8ui_window *win);
/* The player changed: bring the controls after it */
void stage_update(unsigned changes);
void stage_set_fullscreen(bool fullscreen);
void stage_set_skip(bool skip); // there are other videos to go to
/* Show the controls now (a key was pressed) */
void stage_wake(void);
/* Something of ours is up over the video: keep the controls */
void stage_hold(bool hold);
void stage_focus(void);

/* mpris.c: the player on the session bus, for the media keys, the
 * Now playing tile and the lock screen */
struct mpris_hooks {
	void (*play)(void);
	void (*pause)(void);
	void (*toggle)(void);
	void (*stop)(void);
	void (*next)(void);
	void (*previous)(void);
	void (*raise)(void);
};
void mpris_start(const struct mpris_hooks *hooks);
void mpris_stop(void);
/* Tells the bus what changed; uri is what plays, or NULL */
void mpris_update(const char *uri, bool can_skip);
void mpris_seeked(void);

/* prefs.c: ~/.config/cont8nuum/video.ini */
struct vd_prefs {
	bool resume; // carry on where a video was left
	enum vd_show show; // 3D videos, until said otherwise for one
	bool hwdec; // decode on the graphics card
	double volume;
	bool repeat;
	bool tile; // what was watched last on the app's tile
};
extern struct vd_prefs prefs;
void prefs_load(void);
void prefs_save(void);

/* tile.c: the app's own tile on Start, with what was watched last */
/* A video was left at position (seconds) of duration (0: not known) */
void tile_left(const char *name, double position,
	double duration);
/* Off in the options: nothing on the tile */
void tile_clear(void);
/* Before quitting: what was sent must reach the bus */
void tile_flush(void);

#endif
