// Copyright (c) 2019, JINGROW
// For license information, please see license.txt

jingrow.ui.form.on("Google Calendar", {
	refresh: function (frm) {
		if (frm.is_new()) {
			frm.dashboard.set_headline(
				__("To use Google Calendar, enable {0}.", [
					`<a href='/app/google-settings'>${__("Google Settings")}</a>`,
				])
			);
		}

		jingrow.realtime.on("import_google_calendar", (data) => {
			if (data.progress) {
				frm.dashboard.show_progress(
					"Syncing Google Calendar",
					(data.progress / data.total) * 100,
					__("Syncing {0} of {1}", [data.progress, data.total])
				);
				if (data.progress === data.total) {
					frm.dashboard.hide_progress("Syncing Google Calendar");
				}
			}
		});

		if (frm.pg.refresh_token) {
			frm.add_custom_button(__("Sync Calendar"), function () {
				jingrow.show_alert({
					indicator: "green",
					message: __("Syncing"),
				});
				jingrow
					.call({
						method: "jingrow.integrations.pagetype.google_calendar.google_calendar.sync",
						args: {
							g_calendar: frm.pg.name,
						},
					})
					.then((r) => {
						jingrow.hide_progress();
						jingrow.msgprint(r.message);
					});
			});
		}
	},
	authorize_google_calendar_access: function (frm) {
		let reauthorize = 0;
		if (frm.pg.authorization_code) {
			reauthorize = 1;
		}

		jingrow.call({
			method: "jingrow.integrations.pagetype.google_calendar.google_calendar.authorize_access",
			args: {
				g_calendar: frm.pg.name,
				reauthorize: reauthorize,
			},
			callback: function (r) {
				if (!r.exc) {
					frm.save();
					window.open(r.message.url);
				}
			},
		});
	},
});
