<?php
namespace Entity;

class Validator_FluxInstance extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{

	protected function check_insert()
	{
		// get current fields details
		$fields = $this->entity->getFields();

		// current collected messages
		$currentMessages = $this->entity->getMessages();

		// get system details
		$model = new \Modules\Admin\FluxInstances\Model();
		$system = $model->getSystem($fields['assignedSystem']);

		// make sure the name is unique
		if ($fields['name']) {
			$list = new \App\EntityList\FluxInstances();
			$list->setFilter('instance.name', '=', $fields['name']);
			$list->setLimit(1);

			if ($list->exportRecords()) {
				$currentMessages->addWarningCode('insert_nameNotUnique');
			}
		}

		// make sure the appToken is unique
		if ($fields['appToken']) {
			$list = new \App\EntityList\FluxInstances();
			$list->setFilter('instance.appToken', '=', $fields['appToken']);
			$list->setLimit(1);

			if ($list->exportRecords()) {
				$currentMessages->addWarningCode('insert_appTokenNotUnique');
			}
		}

		if ($system->isImporter and !$fields['confirmedPath']) {
			$currentMessages->addWarningCode('insert_confirmedPathMandatory');
		}

		if (($system->isExporter || $system->isImporter) and !$fields['basePath']) {
			$currentMessages->addWarningCode('insert_basePathMandatory');
		}

		if ($system->isImporter and !$fields['behaviourOnInvalidFile']) {
			$currentMessages->addWarningCode('insert_behaviourOnInvalidFileMandatory');
		}

		if (($system->isExporter || $system->isImporter) and !$fields['createMonthBasedFolders']) {
			$currentMessages->addWarningCode('insert_createMonthBasedFoldersMandatory');
		}
	}

	protected function check_update()
	{
		// get current fields details
		$fields = $this->entity->getFields();

		// get current collected messages
		$currentMessages = $this->entity->getMessages();

		// current entity id
		$currentEntityId = $this->entity->getId();

		// get system details
		$model = new \Modules\Admin\FluxInstances\Model();
		$system = $model->getSystem($fields['assignedSystem']);

		// make sure the name is unique
		if ($fields['name']) {
			$list = new \App\EntityList\FluxInstances();
			$list->setFilter('instance.name', '=', $fields['name']);
			$list->setFilter('instance.id', '!=', $currentEntityId);
			$list->setLimit(1);

			if ($list->exportRecords()) {
				$currentMessages->addWarningCode('insert_nameNotUnique');
			}
		}

		// make sure the appToken cannot be updated. It can only be set on insert, once
		if (array_key_exists('appToken', $fields)) {
			$currentMessages->addWarningCode('insert_appTokenDeniedUpdate');
		}

		if ($system->isImporter and !$fields['confirmedPath']) {
			$currentMessages->addWarningCode('insert_confirmedPathMandatory');
		}

		if ($system->isImporter and !$fields['behaviourOnInvalidFile']) {
			$currentMessages->addWarningCode('insert_behaviourOnInvalidFileMandatory');
		}

		if (($system->isExporter || $system->isImporter) and !$fields['createMonthBasedFolders']) {
			$currentMessages->addWarningCode('insert_createMonthBasedFoldersMandatory');
		}
	}

	protected function isAvailable_update()
	{
		// current entity id
		$currentIdEntity = $this->entity->getId();

		// current collected messages
		$currMessages = $this->entity->getMessages();

		$fluxInstance = \Entity\FluxInstance::selectForUpdate($currentIdEntity);

		if (!$fluxInstance->recordExists()) {
			$currMessages->addErrorCode('update_invalidId');
		}
	}

	protected function isAvailable_delete()
	{
		// current entity id
		$currentIdEntity = $this->entity->getId();

		// current collected messages
		$currMessages = $this->entity->getMessages();

		$fluxInstance = \Entity\FluxInstance::selectForUpdate($currentIdEntity);

		if (!$fluxInstance->recordExists()) {
			$currMessages->addErrorCode('delete_invalidId');
		}
	}
}