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

jingrow.ui.form.on("Contact", {
	onload(frm) {
		frm.email_field = "email_id";
	},
	refresh: function (frm) {
		if (frm.pg.__islocal) {
			const last_pg = jingrow.contacts.get_last_pg(frm);
			if (
				jingrow.dynamic_link &&
				jingrow.dynamic_link.pg &&
				jingrow.dynamic_link.pg.name == last_pg.pagename
			) {
				frm.set_value("links", "");
				frm.add_child("links", {
					link_pagetype: jingrow.dynamic_link.pagetype,
					link_name: jingrow.dynamic_link.pg[jingrow.dynamic_link.fieldname],
				});
			}
		}

		if (
			!frm.pg.user &&
			!frm.is_new() &&
			frm.perm[0].write &&
			jingrow.boot.user.can_create.includes("User")
		) {
			frm.add_custom_button(__("Invite as User"), function () {
				return jingrow.call({
					method: "jingrow.contacts.pagetype.contact.contact.invite_user",
					args: {
						contact: frm.pg.name,
					},
					callback: function (r) {
						frm.set_value("user", r.message);
					},
				});
			});
		}
		frm.set_query("link_pagetype", "links", function () {
			return {
				query: "jingrow.contacts.address_and_contact.filter_dynamic_link_pagetypes",
				filters: {
					fieldtype: "HTML",
					fieldname: "contact_html",
				},
			};
		});
		frm.refresh_field("links");

		let numbers = frm.pg.phone_nos;
		if (numbers && numbers.length && jingrow.phone_call.handler) {
			frm.add_custom_button(__("Call"), () => {
				numbers = frm.pg.phone_nos
					.sort((prev, next) => next.is_primary_mobile_no - prev.is_primary_mobile_no)
					.map((d) => d.phone);
				jingrow.phone_call.handler(numbers);
			});
		}

		if (frm.pg.links && frm.pg.links.length > 0) {
			const filtered_links = frm.pg.links.filter(
				(link) => link.link_pagetype && link.link_name
			);

			if (filtered_links.length > 0) {
				jingrow.call({
					method: "jingrow.contacts.pagetype.contact.contact.address_query",
					args: { links: filtered_links },
					callback: function (r) {
						if (r && r.message) {
							frm.set_query("address", function () {
								return {
									filters: {
										name: ["in", r.message],
									},
								};
							});
						}
					},
				});
			}

			for (const link of filtered_links) {
				frm.add_custom_button(
					__("{0}: {1}", [__(link.link_pagetype), __(link.link_name)]),
					function () {
						jingrow.set_route("Form", link.link_pagetype, link.link_name);
					},
					__("Links")
				);
			}
		}

		if (!frm.is_dirty()) {
			frm.page.add_menu_item(__("Download vCard"), function () {
				window.open(
					`/api/action/jingrow.contacts.pagetype.contact.contact.download_vcard?contact=${encodeURIComponent(
						frm.pg.name
					)}`,
					"_blank"
				);
			});
		}
	},
	validate: function (frm) {
		// clear linked customer / supplier / sales partner on saving...
		if (frm.pg.links) {
			frm.pg.links.forEach(function (d) {
				jingrow.model.remove_from_locals(d.link_pagetype, d.link_name);
			});
		}
	},
	after_save: function (frm) {
		jingrow.run_serially([
			() => jingrow.timeout(1),
			() => {
				const last_pg = jingrow.contacts.get_last_pg(frm);
				if (
					jingrow.dynamic_link &&
					jingrow.dynamic_link.pg &&
					jingrow.dynamic_link.pg.name == last_pg.pagename
				) {
					for (let i in frm.pg.links) {
						let link = frm.pg.links[i];
						if (
							last_pg.pagetype == link.link_pagetype &&
							last_pg.pagename == link.link_name
						) {
							jingrow.set_route("Form", last_pg.pagetype, last_pg.pagename);
						}
					}
				}
			},
		]);
	},
	sync_with_google_contacts: function (frm) {
		if (frm.pg.sync_with_google_contacts) {
			jingrow.db.get_value(
				"Google Contacts",
				{ email_id: jingrow.session.user },
				"name",
				(r) => {
					if (r && r.name) {
						frm.set_value("google_contacts", r.name);
					}
				}
			);
		}
	},
});

jingrow.ui.form.on("Dynamic Link", {
	link_name: function (frm, cdt, cdn) {
		var child = locals[cdt][cdn];
		if (child.link_name) {
			jingrow.model.with_pagetype(child.link_pagetype, function () {
				var title_field = jingrow.get_meta(child.link_pagetype).title_field || "name";
				jingrow.model.get_value(
					child.link_pagetype,
					child.link_name,
					title_field,
					function (r) {
						jingrow.model.set_value(cdt, cdn, "link_title", r[title_field]);
					}
				);
			});
		}
	},
});
