<?php
namespace App\Bsp\Hot230\Lines\Types;

/**
 * Only letters, UT8 compatible
 * @author Lucian Sirbu
 *
 */
class PriceAmount extends \App\Utils\BaseParser\TextParser\BlockRegexpAbstract
{

	public function __construct($options, $parent = null)
	{
		if ($options['allowSpaces']) {
			$this->extractRegexp = "/^(?P<rawData>([0-9]+(\{|A|B|C|D|E|F|G|H|I|\}|J|K|L|M|N|O|P|Q|R))|([\s]__LENGHTS__))/";
		}
		else {
			$this->extractRegexp = "/^(?P<rawData>([0-9]+(\{|A|B|C|D|E|F|G|H|I|\}|J|K|L|M|N|O|P|Q|R)))/";
		}

		parent::__construct($options, $parent);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockRegexpAbstract::_extract_value()
	 */
	protected function _extract_value(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret, $originalRawData)
	{
		$ret = parent::_extract_value($ret, $originalRawData);

		if ($this->options['allowSpaces']) {
			if (!trim($ret->extractedData, ' ')) {
				$ret->extractedData = null;
				return $ret;
			}
		}

		$lch = substr($ret->extractedData, -1);
		$ret->extractedData = substr($ret->extractedData, 0, -1);

		$sign = null;

		switch($lch) {
			case '{':
				$sign = '+';
				$ret->extractedData.='0';
			break;
			case 'A':
				$sign = '+';
				$ret->extractedData.='1';
			break;
			case 'B':
				$sign = '+';
				$ret->extractedData.='2';
			break;
			case 'C':
				$sign = '+';
				$ret->extractedData.='3';
			break;
			case 'D':
				$sign = '+';
				$ret->extractedData.='4';
			break;
			case 'E':
				$sign = '+';
				$ret->extractedData.='5';
			break;
			case 'F':
				$sign = '+';
				$ret->extractedData.='6';
			break;
			case 'G':
				$sign = '+';
				$ret->extractedData.='7';
			break;
			case 'H':
				$sign = '+';
				$ret->extractedData.='8';
			break;
			case 'I':
				$sign = '+';
				$ret->extractedData.='9';
			break;

			case '}':
				$sign = '-';
				$ret->extractedData.='0';
			break;
			case 'J':
				$sign = '-';
				$ret->extractedData.='1';
			break;
			case 'K':
				$sign = '-';
				$ret->extractedData.='2';
			break;
			case 'L':
				$sign = '-';
				$ret->extractedData.='3';
			break;
			case 'M':
				$sign = '-';
				$ret->extractedData.='4';
			break;
			case 'N':
				$sign = '-';
				$ret->extractedData.='5';
			break;
			case 'O':
				$sign = '-';
				$ret->extractedData.='6';
			break;
			case 'P':
				$sign = '-';
				$ret->extractedData.='7';
			break;
			case 'Q':
				$sign = '-';
				$ret->extractedData.='8';
			break;
			case 'R':
				$sign = '-';
				$ret->extractedData.='9';
			break;
		}

		$ret->extractedData = (int)ltrim($ret->extractedData, '0');
		if ($sign=='-') {
			$ret->extractedData = -1*$ret->extractedData;
		}

		return $ret;
	}
}
