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

jingrow.ui.form.on('Cluster', {
	refresh: function (frm) {
		[
			[__('Create Servers'), 'create_servers', frm.pg.status === 'Active'],
			[__('Add Images'), 'add_images', frm.pg.status === 'Active'],
		].forEach(([label, method, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						frm.call(method).then((r) => frm.refresh());
					},
					__('Actions'),
				);
			}
		});
		if (frm.pg.vpc_id) {
			if (frm.pg.cloud_provider === 'AWS EC2') {
				frm.add_web_link(
					`https://${frm.pg.region}.console.aws.amazon.com/vpc/home?region=${frm.pg.region}#VpcDetails:VpcId=${frm.pg.vpc_id}`,
					__('Visit AWS Dashboard'),
				);
			} else if (frm.pg.cloud_provider === 'OCI') {
				frm.add_web_link(
					`https://cloud.oracle.com/networking/vcns/${frm.pg.vpc_id}?region=${frm.pg.region}`,
					__('Visit OCI Dashboard'),
				);
			}
		}
	},
});
