'use strict';
'require baseclass';
'require fs';
'require form';

return baseclass.extend({
	title: _('Thermal Plugin Configuration'),
	description: _('The thermal plugin will monitor temperature of the system. Data is typically read from /sys/class/thermal/*/temp ( \'*\' denotes the thermal device to be read, e.g. thermal_zone1 )'),

	addFormOptions(s) {
		let o;

		o = s.option(form.Flag, 'enable', _('Enable this plugin'));

		o = s.option(form.DynamicList, 'Device', _('Monitor device(s) / thermal zone(s)'), _('Empty value = monitor all'));
		o.load = function(section_id) {
			return Promise.all([
				L.resolveDefault(fs.list('/sys/class/thermal'), []),
				L.resolveDefault(fs.list('/proc/acpi/thermal_zone'), [])
			]).then(L.bind(function([therm, therm_zone]) {
				const entries = therm.concat(therm_zone);

				for (let e of entries)
					if (e.type == 'directory' && !e.name.match(/^cooling_device/))
						o.value(e.name);

				return this.super('load', [ section_id ]);
			}, this));
		};

		o.optional = true;
		o.depends('enable', '1');

		o = s.option(form.Flag, 'IgnoreSelected', _('Monitor all except specified'));
		o.default = '0';
		o.optional = true;
		o.depends('enable', '1');
	},

	configSummary(section) {
		const zones = L.toArray(section.Device);
		const invert = section.IgnoreSelected == '1';

		if (zones.length)
			return (invert
				? _('Monitoring all thermal zones except %s')
				: _('Monitoring thermal zones %s')
			).format(zones.join(', '));
		else
			return _('Monitoring all thermal zones');
	}
});
