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

jingrow.ui.form.on('Virtual Machine', {
	refresh: function (frm) {
		[
			[__('Sync'), 'sync'],
			[__('Provision'), 'provision', true, frm.pg.status == 'Draft'],
			[__('Reboot'), 'reboot', true, frm.pg.status == 'Running'],
			[__('Stop'), 'stop', true, frm.pg.status == 'Running'],
			[__('Force Stop'), 'force_stop', true, frm.pg.status == 'Running'],
			[__('Start'), 'start', true, frm.pg.status == 'Stopped'],
			[__('Terminate'), 'terminate', true, !frm.pg.termination_protection],
			[
				__('Force Terminate'),
				'force_terminate',
				true,
				Boolean(jingrow.boot.developer_mode),
			],
			[
				__('Disable Termination Protection'),
				'disable_termination_protection',
				true,
				frm.pg.termination_protection,
			],
			[
				__('Enable Termination Protection'),
				'enable_termination_protection',
				true,
				!frm.pg.termination_protection,
			],
			[__('Increase Disk Size'), 'increase_disk_size', true],
			[__('Create Image'), 'create_image', true, frm.pg.status == 'Stopped'],
			[
				__('Create Snapshots'),
				'create_snapshots',
				true,
				frm.pg.status == 'Running',
			],
			[__('Create Server'), 'create_server', true, frm.pg.series === 'f'],
			[
				__('Create Database Server'),
				'create_database_server',
				false,
				frm.pg.series === 'm',
			],
			[
				__('Create Proxy Server'),
				'create_proxy_server',
				false,
				frm.pg.series === 'n',
			],
			[
				__('Create Registry Server'),
				'create_registry_server',
				false,
				frm.pg.series === 'r',
			],
			[
				__('Create Monitor Server'),
				'create_monitor_server',
				false,
				frm.pg.series === 'm',
			],
			[
				__('Create Log Server'),
				'create_log_server',
				false,
				frm.pg.series === 'e',
			],
			[
				__('Reboot with serial console'),
				'reboot_with_serial_console',
				true,
				frm.pg.status === 'Running' && frm.pg.cloud_provider === 'AWS EC2',
			],
		].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'),
				);
			}
		});
		[
			[
				__('Resize'),
				'resize',
				frm.pg.status == 'Stopped' ||
				(frm.pg.cloud_provider == 'OCI' && frm.pg.status != 'Draft'),
			],
		].forEach(([label, method, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						jingrow.prompt(
							{
								fieldtype: 'Data',
								label: 'Machine Type',
								fieldname: 'machine_type',
								reqd: 1,
							},
							({ machine_type }) => {
								frm
									.call(method, {
										machine_type,
									})
									.then((r) => frm.refresh());
							},
							__('Resize Virtual Machine'),
						);
					},
					__('Actions'),
				);
			}
		});
		[
			[
				__('Update OCI Volume Performance'),
				'update_oci_volume_performance',
				frm.pg.cloud_provider == 'OCI',
			],
		].forEach(([label, method, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						jingrow.prompt(
							[
								{
									fieldtype: 'Int',
									label: 'VPUs / GB',
									fieldname: 'vpus',
									reqd: 1,
									default:
										(frm.pg.volumes[0].iops / frm.pg.volumes[0].size - 45) /
										1.5,
								},
							],
							({ vpus }) => {
								frm
									.call(method, {
										vpus,
									})
									.then((r) => frm.refresh());
							},
							__('Update OCI Volume Performance'),
						);
					},
					__('Actions'),
				);
			}
		});
		[
			[
				__('Convert to ARM'),
				'convert_to_arm',
				frm.pg.cloud_provider == 'AWS EC2' && frm.pg.platform == 'x86_64',
			],
		].forEach(([label, method, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						jingrow.prompt(
							[
								{
									fieldtype: 'Link',
									label: 'Virtual Machine Image',
									fieldname: 'virtual_machine_image',
									options: 'Virtual Machine Image',
									reqd: 1,
									get_query: function () {
										return {
											filters: {
												platform: 'arm64',
												cluster: frm.pg.cluster,
												status: 'Available',
												series: frm.pg.series,
											},
										};
									},
								},
								{
									fieldtype: 'Data',
									label: 'Machine Type',
									fieldname: 'machine_type',
									reqd: 1,
								},
							],
							({ virtual_machine_image, machine_type }) => {
								frm
									.call(method, {
										virtual_machine_image,
										machine_type,
									})
									.then((r) => frm.refresh());
							},
							__(label),
						);
					},
					__('Actions'),
				);
			}
		});
		if (frm.pg.status == 'Running') {
			frm.add_custom_button(
				'Attach New Volume',
				() => {
					jingrow.prompt(
						[
							{
								fieldtype: 'Int',
								label: 'Size',
								fieldname: 'size',
								reqd: 1,
								default: 10,
							},
						],
						({ size }) => {
							frm
								.call('attach_new_volume', {
									size,
								})
								.then((r) => frm.refresh());
						},
						__('Attach New Volume'),
					);
				},
				__('Actions'),
			);
		}
		if (frm.pg.instance_id) {
			if (frm.pg.cloud_provider === 'AWS EC2') {
				frm.add_web_link(
					`https://${frm.pg.region}.console.aws.amazon.com/ec2/v2/home?region=${frm.pg.region}#InstanceDetails:instanceId=${frm.pg.instance_id}`,
					__('Visit AWS Dashboard'),
				);
			} else if (frm.pg.cloud_provider === 'OCI') {
				frm.add_web_link(
					`https://cloud.oracle.com/compute/instances/${frm.pg.instance_id}?region=${frm.pg.region}`,
					__('Visit OCI Dashboard'),
				);
			}
		}
	},
});

jingrow.ui.form.on('Virtual Machine Volume', {
	detach(frm, cdt, cdn) {
		let row = frm.selected_pg;
		jingrow.confirm(
			`Are you sure you want to detach volume ${row.volume_id}?`,
			() =>
				frm
					.call('detach', { volume_id: row.volume_id })
					.then((r) => frm.refresh()),
		);
	},
	delete_volume(frm, cdt, cdn) {
		let row = frm.selected_pg;
		jingrow.confirm(
			`Are you sure you want to delete volume ${row.volume_id}?`,
			() =>
				frm
					.call('delete_volume', { volume_id: row.volume_id })
					.then((r) => frm.refresh()),
		);
	},
	increase_disk_size(frm, cdt, cdn) {
		let row = frm.selected_pg;
		jingrow.prompt(
			{
				fieldtype: 'Int',
				label: 'Increment (GB)',
				fieldname: 'increment',
				reqd: 1,
			},
			({ increment }) => {
				frm
					.call('increase_disk_size', {
						volume_id: row.volume_id,
						increment,
					})
					.then((r) => frm.refresh());
			},
			__('Increase Disk Size'),
		);
	},
	update_ebs_performance(frm, cdt, cdn) {
		let row = frm.selected_pg;
		jingrow.prompt(
			[
				{
					fieldtype: 'Int',
					label: 'IOPS',
					fieldname: 'iops',
					reqd: 1,
					default: row.iops,
				},
				{
					fieldtype: 'Int',
					label: 'Throughput (MB/s)',
					fieldname: 'throughput',
					reqd: 1,
					default: row.throughput,
				},
			],
			({ iops, throughput }) => {
				frm
					.call('update_ebs_performance', {
						volume_id: row.volume_id,
						iops,
						throughput,
					})
					.then((r) => frm.refresh());
			},
			__('Update EBS Performance'),
		);
	},
});
