jingrow.provide("jingrow.ui.misc");
jingrow.ui.misc.about = function () {
	if (!jingrow.ui.misc.about_dialog) {
		var d = new jingrow.ui.Dialog({ title: __("Installed Apps") });

		$(d.body).html(
			repl(
				`<div>
					<div id='about-app-versions'>${__("Loading versions...")}</div>
					<hr>
					<p class='text-muted'>${__("&copy; JINGROW")} </p>
					</div>`,
				jingrow.app
			)
		);

		jingrow.ui.misc.about_dialog = d;

		jingrow.ui.misc.about_dialog.on_page_show = function () {
			if (!jingrow.versions) {
				jingrow.call({
					method: "jingrow.utils.change_log.get_versions",
					callback: function (r) {
						show_versions(r.message);
					},
				});
			} else {
				show_versions(jingrow.versions);
			}
		};

		var show_versions = function (versions) {
			var $wrap = $("#about-app-versions").empty();
			$.each(Object.keys(versions).sort(), function (i, key) {
				var v = versions[key];
				let text;
				if (v.branch) {
					text = $.format("<p><b>{0}:</b> v{1} ({2})<br></p>", [
						v.title,
						v.branch_version || v.version,
						v.branch,
					]);
				} else {
					text = $.format("<p><b>{0}:</b> v{1}<br></p>", [v.title, v.version]);
				}
				$(text).appendTo($wrap);
			});

			jingrow.versions = versions;
		};
	}

	jingrow.ui.misc.about_dialog.show();
};
