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

jingrow.ui.form.on('Database Server', {
	refresh: function (frm) {
		frm.add_web_link(
			`/dashboard/servers/${frm.pg.name}`,
			__('Visit Dashboard'),
		);

		[
			[__('Ping Agent'), 'ping_agent', false, !frm.pg.is_server_setup],
			[__('Ping Ansible'), 'ping_ansible', true, frm.pg.is_server_prepared],
			[
				__('Ping Ansible Unprepared'),
				'ping_ansible_unprepared',
				true,
				!frm.pg.is_server_prepared,
			],
			[__('Update Agent'), 'update_agent', true, frm.pg.is_server_setup],
			[
				__('Update Agent Ansible'),
				'update_agent_ansible',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Fetch Keys'),
				'fetch_keys',
				true,
				frm.pg.is_server_setup &&
					(!frm.pg.jingrow_public_key || !frm.pg.root_public_key),
			],
			[
				__('Prepare Server'),
				'prepare_server',
				true,
				!frm.pg.is_server_prepared,
			],
			[__('Setup Server'), 'setup_server', true, !frm.pg.is_server_setup],
			[
				__('Setup Rename'),
				'rename_server',
				true,
				frm.pg.is_server_setup &&
					frm.pg.is_server_prepared &&
					!frm.pg.is_server_renamed,
			],
			[
				__('Convert From Jingrow Server'),
				'convert_from_jingrow_server',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('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,
			],
			[
				__('Reset Root Password'),
				'reset_root_password',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Enable Performance Schema'),
				'enable_performance_schema',
				true,
				frm.pg.is_server_setup && !frm.pg.is_performance_schema_enabled,
			],
			[
				__('Disable Performance Schema'),
				'disable_performance_schema',
				true,
				frm.pg.is_server_setup && frm.pg.is_performance_schema_enabled,
			],
			[__('Restart MariaDB'), 'restart_mariadb', true, frm.pg.is_server_setup],
			[__('Stop MariaDB'), 'stop_mariadb', true, frm.pg.is_server_setup],
			[
				__('Run Upgrade MariaDB Job'),
				'run_upgrade_mariadb_job',
				true,
				frm.pg.is_server_setup,
			],
			[__('Upgrade MariaDB'), 'upgrade_mariadb', true, frm.pg.is_server_setup],
			[__('Update MariaDB'), 'update_mariadb', true, frm.pg.is_server_setup],
			[
				__('Upgrade MariaDB Patched'),
				'upgrade_mariadb_patched',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Reconfigure MariaDB Exporter'),
				'reconfigure_mariadb_exporter',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Setup Deadlock Logger'),
				'setup_deadlock_logger',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Setup Percona Stalk'),
				'setup_pt_stalk',
				true,
				frm.pg.is_server_setup,
			],
			[
				__('Fetch MariaDB Stalks'),
				'fetch_stalks',
				true,
				frm.pg.is_server_setup && frm.pg.is_stalk_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],
			[
				__('Adjust Memory Config'),
				'adjust_memory_config',
				true,
				frm.pg.status === 'Active',
			],
			[__('Create Image'), 'create_image', true, frm.pg.status == 'Active'],
			[__('Archive'), 'archive', true, frm.pg.status !== 'Archived'],
			[
				__('Reboot with serial console'),
				'reboot_with_serial_console',
				true,
				frm.pg.virtual_machine,
			],
			[
				__('Setup Essentials'),
				'setup_essentials',
				true,
				frm.pg.is_self_hosted,
			],
			[
				__('Mount Volumes'),
				'mount_volumes',
				true,
				frm.pg.virtual_machine && frm.pg.mounts,
			],
		].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'),
				);
			}
		});
		if (frm.pg.is_server_setup) {
			frm.add_custom_button(
				__('Increase Swap'),
				() => {
					const dialog = new jingrow.ui.Dialog({
						title: __('Increase Swap'),
						fields: [
							{
								fieldtype: 'Int',
								label: __('Swap Size'),
								description: __('Size in GB'),
								fieldname: 'swap_size',
								default: 4,
							},
						],
					});

					dialog.set_primary_action(__('Increase Swap'), (args) => {
						frm.call('increase_swap', args).then(() => {
							dialog.hide();
							frm.refresh();
						});
					});
					dialog.show();
				},
				__('Actions'),
			);
			frm.add_custom_button(
				__('Perform Physical Backup'),
				() => {
					const dialog = new jingrow.ui.Dialog({
						title: __('Perform Physical Backup'),
						fields: [
							{
								fieldtype: 'Data',
								label: __('Backup Path'),
								description: __('Absolute path to store the backup'),
								default: '/tmp/replica',
								fieldname: 'path',
								reqd: 1,
							},
						],
					});

					dialog.set_primary_action(__('Backup'), (args) => {
						frm.call('perform_physical_backup', args).then(() => {
							dialog.hide();
							frm.refresh();
						});
					});
					dialog.show();
				},
				__('Actions'),
			);
			frm.add_custom_button(
				__('Update Memory Allocator'),
				() => {
					const dialog = new jingrow.ui.Dialog({
						title: __('Update Memory Allocator'),
						fields: [
							{
								fieldtype: 'Select',
								label: __('Memory Allocator'),
								options: ['System', 'jemalloc', 'TCMalloc']
									.filter((option) => option !== frm.pg.memory_allocator)
									.join('\n'),
								fieldname: 'memory_allocator',
								reqd: 1,
							},
						],
					});

					dialog.set_primary_action(__('Update'), (args) => {
						frm.call({
							method: 'update_memory_allocator',
							pg: frm.pg,
							args: args,
							freeze: true,
							callback: () => {
								dialog.hide();
								frm.refresh();
							},
						});
					});
					dialog.show();
				},
				__('Dangerous Actions'),
			);
		}
	},

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