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

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

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

		if (frm.pg.refresh_token) {
			let sync_button = frm.add_custom_button(__("Sync Contacts"), function () {
				jingrow.show_alert({
					indicator: "green",
					message: __("Syncing"),
				});
				jingrow
					.call({
						method: "jingrow.integrations.pagetype.google_contacts.google_contacts.sync",
						args: {
							g_contact: frm.pg.name,
						},
						btn: sync_button,
					})
					.then((r) => {
						jingrow.hide_progress();
						jingrow.msgprint(r.message);
					});
			});
		}
	},
	authorize_google_contacts_access: function (frm) {
		jingrow.call({
			method: "jingrow.integrations.pagetype.google_contacts.google_contacts.authorize_access",
			args: {
				g_contact: frm.pg.name,
				reauthorize: frm.pg.authorization_code ? 1 : 0,
			},
			callback: function (r) {
				if (!r.exc) {
					frm.save();
					window.open(r.message.url);
				}
			},
		});
	},
});
