/*
 * What a video's picture is: 3D (the two eyes' pictures side by side or
 * one over the other) and all round or not. Files rarely say: Matroska
 * and MP4 can, and mpv passes on what they say of 3D, but most only
 * have it in their name ("Film.3D.HSBS.mkv", "dive_360.mp4"). So it is
 * guessed from both, and what you set by hand for a video is kept for
 * the next time (~/.local/state/cont8nuum/video/views.ini, by address).
 */
#include <string.h>
#include "video.h"

#define KEPT_MAX 300

static char *path(void) {
	return g_build_filename(g_get_user_state_dir(), "cont8nuum", "video",
		"views.ini", NULL);
}

/* The name has word, with something that isn't a letter or a digit (or
 * nothing) on both sides: "sbs" in "film.SBS.mkv", not in "subsbs" */
static bool has_word(const char *name, const char *word) {
	size_t n = strlen(word);
	for (const char *at = name; (at = strstr(at, word)); at++) {
		bool before = at == name || !g_ascii_isalnum(at[-1]);
		bool after = !g_ascii_isalnum(at[n]);
		if (before && after) {
			return true;
		}
	}
	return false;
}

static bool has_any(const char *name, const char *const *words) {
	for (int i = 0; words[i]; i++) {
		if (has_word(name, words[i])) {
			return true;
		}
	}
	return false;
}

static void from_name(const char *uri, struct vd_view *view) {
	char *base = g_path_get_basename(uri);
	char *plain = g_uri_unescape_string(base, NULL);
	char *name = g_ascii_strdown(plain ? plain : base, -1);
	static const char *const sbs[] = { "sbs", "hsbs", "fsbs", "h-sbs",
		"half-sbs", "full-sbs", "3dsbs", "lr", "3dh", "side-by-side", NULL };
	static const char *const sbs_swapped[] = { "rl", NULL };
	static const char *const tb[] = { "tab", "htab", "ftab", "ou", "hou",
		"fou", "h-ou", "tb", "3dv", "over-under", "top-bottom", NULL };
	static const char *const sphere[] = { "360", "360vr", "vr360",
		"360°", "equirectangular", NULL };
	static const char *const half[] = { "180", "vr180", "180vr", "180x180",
		"180°", NULL };
	static const char *const fisheye[] = { "fisheye", "fish-eye", "fe180",
		"180fe", "dual-fisheye", "dfe", NULL };
	static const char *const coloured[] = { "anaglyph", "anaglyphic",
		"anaglyph3d", "red-cyan", "red cyan", "redcyan", NULL };
	if (has_any(name, coloured)) {
		view->layout = VD_ANAGLYPH;
	} else if (has_any(name, sbs)) {
		view->layout = VD_SBS;
	} else if (has_any(name, sbs_swapped)) {
		view->layout = VD_SBS;
		view->swap = true;
	} else if (has_any(name, tb)) {
		view->layout = VD_TB;
	}
	if (has_any(name, fisheye)) {
		view->shape = VD_FISHEYE;
		/* Two lenses side by side, as 180 cameras have */
		if (view->layout == VD_MONO && (has_word(name, "vr180") ||
				has_word(name, "dual-fisheye") || has_word(name, "dfe"))) {
			view->layout = VD_SBS;
		}
	} else if (has_any(name, half)) {
		view->shape = VD_HALF;
		/* Nearly every 180 video is 3D, side by side */
		if (view->layout == VD_MONO && has_word(name, "vr180")) {
			view->layout = VD_SBS;
		}
	} else if (has_any(name, sphere)) {
		view->shape = VD_SPHERE;
	}
	g_free(name);
	g_free(plain);
	g_free(base);
}

void views_guess(const char *uri, const char *stereo, struct vd_view *view) {
	*view = (struct vd_view){ .show = prefs.show, .fov = VD_FOV_DEFAULT };
	from_name(uri, view);
	/* What the file says of itself counts for more than its name */
	if (stereo && g_str_has_prefix(stereo, "sbs")) {
		view->layout = VD_SBS;
		view->swap = strstr(stereo, "2r") != NULL;
	} else if (stereo && g_str_has_prefix(stereo, "ab")) {
		view->layout = VD_TB;
		view->swap = strstr(stereo, "2r") != NULL;
	}
	/* And what you said, for more still */
	char *file = path();
	GKeyFile *kf = g_key_file_new();
	if (g_key_file_load_from_file(kf, file, G_KEY_FILE_NONE, NULL) &&
			g_key_file_has_group(kf, uri)) {
		int layout = g_key_file_get_integer(kf, uri, "layout", NULL);
		int shape = g_key_file_get_integer(kf, uri, "shape", NULL);
		int show = g_key_file_get_integer(kf, uri, "show", NULL);
		if (layout >= 0 && layout < VD_LAYOUT_COUNT && shape >= 0 &&
				shape < VD_SHAPE_COUNT && show >= 0 && show < VD_SHOW_COUNT) {
			view->layout = layout;
			view->shape = shape;
			view->show = show;
			view->swap = g_key_file_get_boolean(kf, uri, "swap", NULL);
		}
	}
	g_key_file_free(kf);
	g_free(file);
}

void views_remember(const char *uri, const struct vd_view *view) {
	char *file = path();
	GKeyFile *kf = g_key_file_new();
	g_key_file_load_from_file(kf, file, G_KEY_FILE_NONE, NULL);
	/* Again at the end: the oldest are first, and go first */
	g_key_file_remove_group(kf, uri, NULL);
	g_key_file_set_integer(kf, uri, "layout", view->layout);
	g_key_file_set_integer(kf, uri, "shape", view->shape);
	g_key_file_set_integer(kf, uri, "show", view->show);
	g_key_file_set_boolean(kf, uri, "swap", view->swap);
	gsize count = 0;
	char **groups = g_key_file_get_groups(kf, &count);
	for (gsize i = 0; count > KEPT_MAX && i < count - KEPT_MAX; i++) {
		g_key_file_remove_group(kf, groups[i], NULL);
	}
	g_strfreev(groups);
	char *dir = g_path_get_dirname(file);
	g_mkdir_with_parents(dir, 0700);
	GError *err = NULL;
	if (!g_key_file_save_to_file(kf, file, &err)) {
		g_warning("views.ini: %s", err->message);
		g_error_free(err);
	}
	g_free(dir);
	g_key_file_free(kf);
	g_free(file);
}
