<?php

namespace App\OfferProfileRules;

class Criterion_ServiceRemarks extends CriterionAbstract
{

	/***
	 * 	$remarksValuesList = array(
	 *		array('operation' => 'equal', 	'var' => 'ccp', 	'values' => array('150', 'ssab') ),
	 *		array('operation' => 'notSet', 	'var' => 'CC' ),
	 *	)
	 */
	private $remarksValuesList;


	/**
	 * @param array $remarksValuesList
	 */
	public function __construct(array $remarksValuesList)
	{
		if ($remarksValuesList) {
			$this->remarksValuesList = $remarksValuesList;
		}

		$this->checkOperation();

	}

	/**
	 * returns the remarks values list
	 */
	public function getRemarksValues()
	{
		return $this->remarksValuesList;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\OfferProfileRules\CriterionAbstract::checkOperation()
	 */
	public function checkOperation()
	{
		foreach ($this->remarksValuesList as $list) {
			if (!$list['operation']) {
				throw new \Exception('No operation set for '.get_called_class());
			}

			$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
			$currentEntity = $this->getEntity();
			if (!$CFG['offerProfileRules']['criteriaOperations'][$currentEntity]) {
				throw new \Exception("The entity `{$currentEntity}` is not configured for ".get_called_class());
			}
			if (!in_array($list['operation'], $CFG['offerProfileRules']['criteriaOperations'][$currentEntity])) {
				throw new \Exception("The operation `{$this->operation}` is not valid for ".get_called_class());
			}

		}
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::checkFields()
	 */
	public function checkFields(\App\OfferProfileRules\Fields $fields)
	{

		foreach ($this->remarksValuesList as $crit) {

			if (!$this->getOperationResult($fields->serviceRemarks[$crit['var']], $crit['operation'], $crit['values'])) {
				return false;
			}
		}

		return true;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::checkAssignedValues()
	 */
	public function checkAssignedValues(\App\MessagesCollector $msgs)
	{
		foreach ($this->remarksValuesList as $remark) {

			// check the var
			if ($remark['operation'] and !$remark['var']) {
				$msgs->addWarning("You must define the variable name for the service remark.");
			}

			if (in_array($remark['operation'], ['equal', 'notEqual']) and !$remark['values']) {
				$msgs->addWarning("For the operation you set in services remarks you must also set the values.");
			}

			// check the values
			if ($remark['values'] and !is_array($remark['values'])) {
				$msgs->addWarning("The remark values must be set list.");
			}

		}
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\OfferProfileRules\CriterionAbstract::createDescription()
	 */
	public function createDescription()
	{
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		// the field name
		$entityField = $this->getEntity();

		$return = [];
		foreach ($this->remarksValuesList as $remark) {
			if ($remark['operation']=='anyValue' or $remark['operation']=='notSet') {
				$return[] = "<b>".$remark['var']."</b> ".$_LANG['OfferProfileRules']['criterionOperation'][$remark['operation']];
			}
			else {
				$return[] = "<b>".$remark['var']."</b> ".$_LANG['OfferProfileRules']['criterionOperation'][$remark['operation']]." ".implode(', ', $remark['values']);
			}
		}

		return sprintf(sprintf($_LANG['OfferProfileRules']['criterionDescription1'],  $_LANG['OfferProfileRules']['criterionFieldName'][$entityField], implode(', ', $return)));


	}

	/**
	 * Returns the name of the current entity id
	 * @param String $val
	 */
	protected function createDescription_getEntityName($code)
	{}

}
