var Assert = require('assert');
var ValidatorTrigger = require('libresignage/ui/validator/ValidatorTrigger');

/**
* A class for grouping together Validators.
*
* @memberof module:libresignage/ui/validator
*/
class ValidatorController {
	/**
	* Construct a new ValidatorController object.
	*
	* @param {object} validators An associative array of Validators.
	*
	* @throws {AssertionError} If validators is not an object.
	*/
	constructor(validators) {
		Assert.ok(
			validators && validators === Object(validators),
			'Invalid validators object.'
		);

		
		this.validators = Object.values(validators);
		this.triggers = [];

		// Validate once to take the initial validation state into account.
		this.validate();
	}

	/**
	* Create a ValidatorTrigger on the validators of a ValidatorController.
	*
	* @param {ValidatorTrigger_trigger_hook} hook The trigger hook function.
	*/
	create_trigger(hook) {
		this.triggers.push(new ValidatorTrigger(this.validators, hook));
	}

	/**
	* Manually validate all Validators.
	*/
	validate() {
		for (let v of this.validators) { v.validate(); }
	}

	/**
	* Enable or disable all Validators.
	*
	* @param {boolean} state True = enabled, false = disabled.
	*/
	enable(state) {
		for (let v of this.validators) { v.enable(state); }
	}

	/**
	* Get the validation state of a ValidatorController, ie. whether all of
	* the individual input validators are valid or not.
	*
	* @return {boolean} True = valid, false = invalid.
	*/
	get_state() {
		for (let v of this.validators) {
			if (!v.get_state()) { return false; }
		}
		return true;
	}
}
module.exports = ValidatorController;
