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

jingrow.ui.form.on('Proxy 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],
			[
				__('Update Agent Ansible'),
				'update_agent_ansible',
				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],
			[
				__('Setup SSH Proxy'),
				'setup_ssh_proxy',
				true,
				frm.pg.ssh_certificate_authority && !frm.pg.is_ssh_proxy_setup,
			],
			[
				__('Setup ProxySQL'),
				'setup_proxysql',
				true,
				!frm.pg.is_proxysql_setup,
			],
			[
				__('Setup ProxySQL Monitor'),
				'setup_proxysql_monitor',
				true,
				frm.pg.is_proxysql_setup,
			],
			[
				__('Setup Wildcard Hosts'),
				'setup_wildcard_hosts',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Show Agent Password'),
				'show_agent_password',
				false,
				frm.pg.is_server_setup,
			],
			[
				__('Fetch Keys'),
				'fetch_keys',
				false,
				frm.pg.is_server_setup &&
					(!frm.pg.jingrow_public_key || !frm.pg.root_public_key),
			],
			[__('Update TLS Certificate'), 'update_tls_certificate', true],
			[__('Create Image'), 'create_image', true, frm.pg.status == 'Active'],
			[
				__('Setup Replication'),
				'setup_replication',
				true,
				frm.pg.is_server_setup &&
					!frm.pg.is_primary &&
					!frm.pg.is_replication_setup,
			],
			[
				__('Trigger Failover'),
				'trigger_failover',
				true,
				frm.pg.is_server_setup &&
					!frm.pg.is_primary &&
					frm.pg.is_replication_setup,
			],
			[__('Archive'), 'archive', true, frm.pg.status !== 'Archived'],
			[__('Setup Fail2ban'), 'setup_fail2ban', true, frm.pg.is_server_setup],
			[__('Setup Wireguard'), 'setup_wireguard', true],
			[__('Reload Wireguard'), 'reload_wireguard', true],
			[
				__('Reboot with serial console'),
				'reboot_with_serial_console',
				true,
				frm.pg.virtual_machine,
			],
		].forEach(([label, method, confirm, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						if (confirm) {
							jingrow.confirm(
								`Are you sure you want to ${label.toLowerCase()}?`,
								() =>
									frm.call(method).then((r) => {
										if (r.message) {
											jingrow.msgprint(r.message);
										} else {
											frm.refresh();
										}
									}),
							);
						} else {
							frm.call(method).then((r) => {
								if (r.message) {
									jingrow.msgprint(r.message);
								} else {
									frm.refresh();
								}
							});
						}
					},
					__('Actions'),
				);
			}
		});
	},

	hostname: function (frm) {
		jcloud.set_hostname_abbreviation(frm);
	},
});
