<?php
namespace App\Galileo\LineParser\Types;

/**
 * Data line
 * @author Lucian Sirbu
 */
class Data_A28 extends \App\Galileo\LineParser\Types\GalileoBase
{
	/**
	 * OTHER FARE LEVEL SECTION
	 */

	/**
	 * Children elements
	 */
	protected $children_orig = [
			'key' => 			'orig',
			'description' => 	'Original A28 line format',

			[ 'key'=>'A28SEC',  'type'=>'Constant', 'len'=>3,      'val'=>'A28',  'label'=>'SECTION LABEL' ],
			[ 'key'=>'A28FSI',  'type'=>'Integer',  'len'=>2,      'label'=>'FARE SECTION INDICATOR' ],
			[ 'key'=>'A28LVL',  'type'=>'Letters',  'len'=>1,      'label'=>'FARE LEVEL INDICATOR' ],
			[ 'key'=>'A28CRB',  'type'=>'Letters',  'len'=>3,      'label'=>'CURRENCY CODE FOR BASE FARE' ],
			[ 'key'=>'A28TBF',  'type'=>'Price',    'len'=>12,     'label'=>'BASE FARE AMOUNT' ],
			[ 'key'=>'A28CRT',  'type'=>'Letters',  'len'=>3,      'label'=>'CURRENCY CODE FOR TOTAL AMOUNT' ],
			[ 'key'=>'A28TTA',  'type'=>'Price',    'len'=>12,     'label'=>'TOTAL AMOUNT' ],
			[ 'key'=>'A28CRE',  'type'=>'Letters',  'len'=>3,      'label'=>'CURRENCY CODE FOR EQUIVALENT AMOUNT' ],
			[ 'key'=>'A28EQV',  'type'=>'Price',    'len'=>12,     'label'=>'EQUIVALENT AMOUNT' ],

			[ 'key'=>'taxes',       'type'=>'OrderedComposer',  'req'=>false, 'children' => [
				[ 'key'=>'A28CUR',  'type'=>'Letters',  'len'=>3,                'label'=>'CURRENCY FOR TAXES' ],
				[ 'key'=>'A28TI1',  'type'=>'Constant', 'len'=>3, 'val'=>'T1:',  'label'=>'TAX 1 IDENTIFIER "T1:"' ],
				[ 'key'=>'A28TT1',  'type'=>'StringRaw',   'len'=>8,                'label'=>'TAX 1' ],
				[ 'key'=>'A28TC1',  'type'=>'StringRaw',   'len'=>2,                'label'=>'TAX 1 TAX CODE' ],
				[ 'key'=>'A28TI2',  'type'=>'Constant', 'len'=>3, 'val'=>'T2:',  'label'=>'TAX 2 IDENTIFIER "T2:"' ],
				[ 'key'=>'A28TT2',  'type'=>'StringRaw',   'len'=>8,                'label'=>'TAX 2' ],
				[ 'key'=>'A28TC2',  'type'=>'StringRaw',   'len'=>2,                'label'=>'TAX 2 TAX CODE' ],
				[ 'key'=>'A28TI3',  'type'=>'Constant', 'len'=>3, 'val'=>'T3:',  'label'=>'TAX 3 IDENTIFIER "T3:"' ],
				[ 'key'=>'A28TT3',  'type'=>'StringRaw',   'len'=>8,                'label'=>'TAX 3' ],
				[ 'key'=>'A28TC3',  'type'=>'StringRaw',   'len'=>2,                'label'=>'TAX 3 TAX CODE' ],
				[ 'key'=>'A28TI4',  'type'=>'Constant', 'len'=>3, 'val'=>'T4:',  'label'=>'TAX 4 IDENTIFIER "T4:"' ],
				[ 'key'=>'A28TT4',  'type'=>'StringRaw',   'len'=>8,                'label'=>'TAX 4' ],
				[ 'key'=>'A28TC4',  'type'=>'StringRaw',   'len'=>2,                'label'=>'TAX 4 TAX CODE' ],
				[ 'key'=>'A28TI5',  'type'=>'Constant', 'len'=>3, 'val'=>'T5:',  'label'=>'TAX 5 IDENTIFIER "T5:"' ],
				[ 'key'=>'A28TT5',  'type'=>'StringRaw',   'len'=>8,                'label'=>'TAX 5' ],
				[ 'key'=>'A28TC5',  'type'=>'StringRaw',   'len'=>2,                'label'=>'TAX 5 TAX CODE' ],
			]],
	];

	protected $children_without_A28CRT = [
			'key' => 			'without_A28CRT',
			'description' => 	'A28 line without A28CRT',

			[ 'key'=>'A28SEC',  'type'=>'Constant', 'len'=>3,      'val'=>'A28',  'label'=>'SECTION LABEL' ],
			[ 'key'=>'A28FSI',  'type'=>'Integer',  'len'=>2,      'label'=>'FARE SECTION INDICATOR' ],
			[ 'key'=>'A28LVL',  'type'=>'Letters',  'len'=>1,      'label'=>'FARE LEVEL INDICATOR' ],
			[ 'key'=>'A28CRB',  'type'=>'Letters',  'len'=>3,      'label'=>'CURRENCY CODE FOR BASE FARE' ],
			[ 'key'=>'A28TBF',  'type'=>'Price',    'len'=>12,     'label'=>'BASE FARE AMOUNT' ],
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockRegexpAbstract::isValid()
	 */
	public function isValid()
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, func_get_args());
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logInput([
			'key' => $this->options['key'],
			'rawData' => $this->rawData,
		]);

		$ret = false;

		$m = $this->safeMatch("/^A28.+/", $this->rawData);
		$ret = (bool)$m->count;

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logOutput([
			'return' => $ret,
		]);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
		return $ret;
	}

	protected function _extract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret, $originalRawData)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, func_get_args());

		$m = $this->safeMatch($this->extractRegexp, $originalRawData);
		$this->rawData = $m->matches['rawData'];

		$ret->remainingGlobalData = substr($originalRawData, strlen($this->rawData));

		$extracted = false;

		if (!$extracted) {
			$extracted = $this->_extractFromChildren($this->children_orig, $ret, $this->rawData);
		}

		if (!$extracted) {
			$extracted = $this->_extractFromChildren($this->children_without_A28CRT, $ret, $this->rawData);
		}

		if (!$extracted) {
			\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
			throw new \App\Utils\BaseParser\TextParser\Exception\MissingRequiredElementException("Don't know how o parse this A28 line. No defined format matches it", null, null, $this);
		}

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
		return $ret;
	}

	protected function _extractFromChildren($children, \App\Utils\BaseParser\TextParser\ExtractedBlock $ret, $rawData)
	{
		try {
			$ch = $children;
			$key = 	$ch['key'];
			$desc = $ch['description'];

			unset($ch['key']);
			unset($ch['description']);

			\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->dlog("Trying {$key} ({$desc})");
			$this->children = $ch;
			$ret = parent::_extract($ret, $rawData);

			$ret->extractedData['type_'] = $key;
			$ret->extractedData['desc_'] = $desc;

			return $key;
		} catch (\App\Utils\BaseParser\TextParser\Exception\MissingRequiredElementException $ex) {
			// try next version
			\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
			\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
			return false;
		}
	}
}
