<?php

namespace App\OfferProfileRules;

class OfferDetails_ServiceFee implements OfferDetailsInterface
{

	/**
	 * The service fee apply mode: 'value', 'percent', 'segment', 'duration'.
	 * @var String
	 */
	public $mode;

	/**
	 * The percent source: 'servicePrice', 'servicePrice+tax'.
	 * @var String
	 */
	public $percentSource;

	/**
	 * Service fee percent - [0-100]
	 * @var Float
	 */
	public $percent;

	/**
	 * Service fee absolute value
	 * @var Float
	 */
	public $value;

	/**
	 * Service fee currency (associated to service fee value).
	 * @var String
	 */
	public $currency;


	/**
	 * {@inheritDoc}
	 * @see \App\OfferProfileRules\OfferDetailsInterface::check()
	 */
	public function check(\App\MessagesCollector $msgs)
	{
		$return = true;

		// check service fee mode
		if ($this->mode!='value' and $this->mode!='percent' and $this->mode!='segment' and $this->mode!='duration') {
			$msgs->addError('Service fee mode is not valid - it must be set as `value`, `percent`, `segment`, `duration`.');
			$return = false;
		}

		// check service fee mode
		if ($this->percentSource!='servicePrice' and $this->percentSource!='servicePrice+tax') {
			$msgs->addError('Service fee percent source is not valid - it must be set as `servicePrice`, `servicePrice+tax`.');
			$return = false;
		}

		$value = (string)$this->value;
		$percent = (string)$this->percent;

		// check value and percent
		if (!strlen($value) and !strlen($percent)) {
			$msgs->addWarning('You must set the service fee percent or value.');
			$return = false;
		}
		if (strlen($value) and strlen($percent)) {
			$msgs->addWarning('The absolute value and the percent cannot be both set at the same time.');
			$return = false;
		}

		// check percent
		if (strlen($percent)) {
			if (!is_numeric($percent) or $percent<0) {
				$msgs->addWarning('Service fee percent must be set as e a positive value.');
				$return = false;
			}
			if ($this->mode!='percent') {
				$msgs->addWarning('You cannot set the percent value unless the applying mode is `percent`.');
				$return = false;
			}
		}

		// check value
		if (strlen($value)) {
			if (!is_numeric($value) or $value<0) {
				$msgs->addError('The service fee value must be a positive value.');
				$return = false;
			}
			if (!\App\Config\Currencies::getInstance()->check($this->currency)) {
				$msgs->addError('Service fee currency is not valid.');
				$return = false;
			}
		}

		// check that the percent value is set on mode==percent
		if ($this->mode=='percent' and !strlen($percent)) {
			$msgs->addWarning('You did not set the percent value of the service fee.');
			$return = false;
		}

		return $return;

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\OfferDetailsInterface::getDescription()
	 */
	public function getDescription($fullDescription=true)
	{

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

		$return = '';

		if ($this->percent!==null) {
			if ($fullDescription) {
				$return .= $this->percent.'% - '.$_LANG['OfferProfileRules']['serviceFee']['percentSource'][$this->percentSource];
			}
			else {
				$return .= $this->percent.'%';
			}
		} elseif ($this->value!==null) {
			$return .= \App\Utils\PriceFormat::format($this->value, $this->currency);
		}
		if ($this->mode=='segment') {
			$return .= ' / '.$_LANG['OfferProfileRules']['serviceFee']['mode_segment'];
		}
		elseif ($this->mode=='duration') {
			$return .= ' / '.$_LANG['OfferProfileRules']['serviceFee']['mode_duration'];
		}

		return $return;
	}

}
