// Copyright (c) 2021, Frappe and contributors
// For license information, please see license.txt

frappe.ui.form.on('Monitor Server', {
	refresh: function (frm) {
		[
			[__('Ping Agent'), 'ping_agent', false, frm.pg.is_server_setup],
			[__('Ping Ansible'), 'ping_ansible', true],
			[__('Ping Ansible Unprepared'), 'ping_ansible_unprepared', true],
			[__('Update Agent'), 'update_agent', true, frm.pg.is_server_setup],
			[__('Prepare Server'), 'prepare_server', true, !frm.pg.is_server_setup],
			[__('Setup Server'), 'setup_server', true, !frm.pg.is_server_setup],
			[__('Archive'), 'archive', true, frm.pg.provider === 'AWS EC2'],
			[
				__('Reconfigure Monitor Server'),
				'reconfigure_monitor_server',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Fetch Keys'),
				'fetch_keys',
				false,
				frm.pg.is_server_setup &&
					(!frm.pg.frappe_public_key || !frm.pg.root_public_key),
			],
			[
				__('Show Grafana Password'),
				'show_grafana_password',
				false,
				frm.pg.is_server_setup,
			],
			[__('Update TLS Certificate'), 'update_tls_certificate', true],
		].forEach(([label, method, confirm, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						if (confirm) {
							frappe.confirm(
								`Are you sure you want to ${label.toLowerCase()}?`,
								() =>
									frm.call(method).then((r) => {
										if (r.message) {
											frappe.msgprint(r.message);
										} else {
											frm.refresh();
										}
									}),
							);
						} else {
							frm.call(method).then((r) => {
								if (r.message) {
									frappe.msgprint(r.message);
								} else {
									frm.refresh();
								}
							});
						}
					},
					__('Actions'),
				);
			}
		});
	},
});
