<?php

namespace Entity;

/**
 * The class is instantiated and the method validate() is executed in the Entity->check() method.
 *
 */
class Validator_EntityMngWsAccount extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{

    /**
	 * Validates the fields for inserting
	 */
	protected function check_insert()
	{

		// get current fields details
		$fields=$this->entity->getFields();

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

		// CHECK THAT UNIQUE NAME--------------------------------
		if ($fields['name']) {

			$listObj = new \App\EntityList\EntityMngWsAccounts();
			$listObj->setFilter('name', '=', $fields['name']);
			$listObj->setLimit(1);
			$results = $listObj->exportRecords();

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

		// CHECK THAT UNIQUE AUTH TOKEN-------------------------------
		if ($fields['authToken']) {

			$listObj = new \App\EntityList\EntityMngWsAccounts();
			$listObj->setFilter('authToken', '=', $fields['authToken']);
			$listObj->setLimit(1);
			$results = $listObj->exportRecords();

			if ($results) {
				$currentMessages->addWarningCode('insert_authTokenNotUnique');
			}
		}
	}

	/**
	 * Check fields for update
	 */
	protected function isAvailable_update()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

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

		// CHECK THE ID ---------------------------------------------------
		$entityObj = EntityMngWsAccount::selectForUpdate($currentIdEntity);

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		}
	}

	/**
	 * Validates fields for update
	 */
	protected function check_update()
	{

		// get current fields details
		$fields=$this->entity->getFields();

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

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

		// CHECK UNIQUE NAME ----------------------
		if ($fields['name']) {

			$listObj = new \App\EntityList\EntityMngWsAccounts();
			$listObj->setFilter('name', '=', $fields['name']);
			$listObj->setFilter('id', '!=', $currentIdEntity);
			$listObj->setLimit(1);
			$results = $listObj->exportRecords();

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

		}

		// CHECK UNIQUE AUTH TOKEN ----------------
		if ($fields['authToken']) {

			$listObj = new \App\EntityList\EntityMngWsAccounts();
			$listObj->setFilter('authToken', '=', $fields['authToken']);
			$listObj->setFilter('id', '!=', $currentIdEntity);
			$listObj->setLimit(1);
			$results = $listObj->exportRecords();

			if ($results[0]['id']) {
				$currentMessages->addWarningCode('insert_authTokenNotUnique');

			}

		}

		// CHECK ACCESS LEVEL ---------------------
		if (array_key_exists("accessLevel", $fields) && !is_array($fields['accessLevel'])) {
			throw new \Exception('Access Level should be set as array.');
		}

		if ($fields['accessLevel']) {

			$accountAccessConfig = new \Modules\Admin\EntityMngWsAccounts\AccountAccessConfig();
			$accountAccessConfigArr = $accountAccessConfig->getAccountAvailableAccess();

			foreach ($fields['accessLevel'] as $entity => $operations) {

				// Check for Entity
				if (!array_key_exists($entity, $accountAccessConfigArr)) {
					throw new \Exception('Entity '.$entity.' is not valid for entity management WS access level.');
				}


				// Check for Operations array
				if (!is_array($operations)) {
					throw new \Exception('Operations should be set as array.');
				}

				foreach ($operations as $operation) {
					// Checks for operation
					if (!$accountAccessConfigArr[$entity][$operation]) {
						$currentMessages->addWarningCode('update_invalidEntityOperation', $operation);
					}
				}
			}
		}
	}

	/**
	 * Check fields for delete
	 */
	protected function isAvailable_delete()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

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

		// CHECK THE ID ---------------------------------------------------------------------------
		$entityObj = EntityMngWsAccount::selectForUpdate($currentIdEntity);

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		}
	}

	/**
	 * Check for block
	 */
	protected function isAvailable_block()
	{

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

		// CHECK THE ID
		$entityObj = EntityMngWsAccount::selectForUpdate($this->entity->getId());

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		}

		// CHECK IF ALREADY BLOCKED
		if ($entityObj->blocked) {
			$currMessages->addWarningCode('block_alreadyBlocked');
		}
	}

	/**
	 * Check for unblock
	 */
	protected function isAvailable_unblock()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THE ID
		$entityObj = EntityMngWsAccount::selectForUpdate($this->entity->getId());

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		}

		// CHECK IF ALREADY UNBLOCKED
		if (!$entityObj->blocked) {
			$currMessages->addWarningCode('unblock_notBlocked');
		}
	}


	/**
	 * Check for Session Token
	 */
	protected function isAvailable_generateSessionToken()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THE ID
		$entityObj = EntityMngWsAccount::selectForUpdate($this->entity->getId());

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		} elseif ($entityObj->blocked) {
			$currMessages->addWarningCode('accountBlocked');
		}
	}

	/**
	 * Check for HIT COUNT
	 */
	protected function isAvailable_increaseHitCount()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THE ID
		$entityObj = EntityMngWsAccount::selectForUpdate($this->entity->getId());

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif (!$entityObj->sessionToken) {
			$currMessages->addWarningCode('noSessionTokenFound');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		} elseif ($entityObj->blocked) {
			$currMessages->addWarningCode('accountBlocked');
		}
	}

	/**
	 * Check for MINUTE COUNT
	 */
	protected function isAvailable_increaseMinuteCount()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THE ID
		$entityObj = EntityMngWsAccount::selectForUpdate($this->entity->getId());

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif (!$entityObj->sessionToken) {
			$currMessages->addWarningCode('noSessionTokenFound');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		} elseif ($entityObj->blocked) {
			$currMessages->addWarningCode('accountBlocked');
		}
	}


	/**
	 * Check for Reset
	 */
	protected function isAvailable_resetSessionToken()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THE ID
		$entityObj = EntityMngWsAccount::selectForUpdate($this->entity->getId());

		if (!$entityObj->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		} elseif ($entityObj->deleted) {
			$currMessages->addWarningCode('deletedId');
		} elseif ($entityObj->blocked) {
			$currMessages->addWarningCode('accountBlocked');
		}
	}

}

