<?php

namespace App\OfferProfileRules;

class Criterion_ServicePrice extends CriterionAbstract
{

	/**
	 * Sets the operation and the values for the current criterion - the values are stored as a unique list of elements.
	 * @param String $operation
	 * @param Array $values
	 */
	public function __construct($operation, $price, $currency)
	{
		parent::__construct($operation, ['value'=>$price, 'currency'=>$currency]);
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\OfferProfileRules\CriterionAbstract::checkFields()
	 */
	public function checkFields(\App\OfferProfileRules\Fields $fields)
	{
		// the servicePriceValue is set and is greater than 0 and the currency is different from the one from the rule
		if ($fields->servicePriceValue > 0 and $fields->servicePriceCurrency != $this->values['currency']) {
			// verificare currency date
			if (!$fields->currencyDate) {
				throw new \Exception("You must set the currency date.");
			}
			// convert the price to the currency from rule
			$servicePrice = \App\CurrencyExchange\Rates::getInstance($fields->currencyDate)->convert($fields->servicePriceValue, $fields->servicePriceCurrency, $this->values['currency']);

			return $this->checkField($servicePrice);
		}

		elseif ($fields->servicePriceValue and $fields->servicePriceValue > 0 and $fields->servicePriceCurrency == $this->values['currency']) {
			return $this->checkField($fields->servicePriceValue);
		}

		elseif ($fields->servicePriceValue or $fields->servicePriceValue == 0) {
			return $this->checkField(0);
		}
	}


	protected function checkField($value)
	{

		switch ($this->operation) {
			case 'equal': {
				if ($value == $this->values['value']) {
					return true;
				}
				return false;
			}
			case 'smaller': {
				if ($value < $this->values['value']) {
					return true;
				}
				return false;
			}
			case 'smallerOrEqual': {
				if ($value <= $this->values['value']) {
					return true;
				}
				return false;
			}
			case 'greater': {
				if ($value > $this->values['value']) {
					return true;
				}
				return false;
			}
			case 'greaterOrEqual': {
				if ($value >= $this->values['value']) {
					return true;
				}
				return false;
			}

			default: {
				throw new \Exception("Invalid operation `{$this->operation}`.");
			}
		}
	}


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\OfferProfileRules\CriterionAbstract::checkAssignedValues()
	 */
	public function checkAssignedValues(\App\MessagesCollector $msgs)
	{
		// the price and the currency must be both set
		if (count($this->values)==0 or !$this->values['currency'] or !$this->values['value'] ) {
			$msgs->addWarning("The service price and the currency must be both set.");
		}

		// the currency must be in currency list from TINA
		if ($this->values['currency'] and !\App\Config\Currencies::getInstance()->check($this->values['currency'])) {
			$msgs->addWarning("The currency is invalid.");
		}

		// the price can be float
		if ($this->values['value'] and (!is_numeric($this->values['value']) or $this->values['value']<0) ) {
			$msgs->addWarning("The service price must be numeric and greater than 0.");
		}

	}


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\OfferProfileRules\CriterionAbstract::createDescription_getEntityName()
	 */
	protected function createDescription_getEntityName($code)
	{
		return $code;
	}

	/**
	 * Creates a human readable text to easily identify a criterion.
	 * @return String
	 */
	public function createDescription()
	{

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

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

		return sprintf($_LANG['OfferProfileRules']['criterionDescription2'], $_LANG['OfferProfileRules']['criterionFieldName'][$entityField], '<i>'.$_LANG['OfferProfileRules']['criterionOperation'][$this->operation].'</i>', '<b>'.implode(' ', $this->values).'</b>');


	}

}