<?php

namespace App\OfferProfileRules;

class OfferDetails_Commission implements OfferDetailsInterface
{

	/**
	 * Commissionable/net price - if set to true the price is commissionable.
	 * @var Boolean
	 */
	public $included;

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

	/**
	 * Commission absolute value
	 * @var Float
	 */
	public $value;

	/**
	 * Commission currency (associated to commission value).
	 * @var String
	 */
	public $currency;


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

		// check 'included'
		if ($this->included===null) {
			$msgs->addError('Commission included is not set.');
			$return = false;
		}

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

		// general checks
		if (!strlen($value) and !strlen($percent)) {
			$msgs->addWarning('You must set the commission 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) and (!is_numeric($percent) or $percent<0)) {
			$msgs->addError('The commission percent must be a positive value.');
			$return = false;
		}

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

		return $return;

	}


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

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

		$return = '';

		// commissionable/net price
		if ($fullDescription and $this->included!==null){
			$return .= $_LANG['OfferProfileRules']['commission'][(Int)$this->included];
		}

		if ($this->percent!==null) {
			$return .= ' - '.$this->percent.'%';
		} elseif ($this->value!==null)  {
			$return .= ' - '.\App\Utils\PriceFormat::format($this->value, $this->currency);
		}

		return $return;

	}

}

