<?php
namespace App\Sabre\LineParser\Types\Line;

/**
 *
 * @author Lucian Sirbu
 */
class M5USDowntown extends M5
{
	protected $children_1_1 = [
		'key' => 			'1',
		'subkey' => 		'1',
		'description' => 	'Automated ARC/BSP Ticketing/US/Downtown version (1.1)',


		[ 'key'=>'IU5MID',    'type'=>'Constant',     'len'=>2,       'val'=>'M5', ],
		[ 'key'=>'IU5PTY',    'type'=>'StringTrim',   'len'=>2, ],
		[ 'key'=>'IU5MIN',    'type'=>'StringTrim',   'len'=>2, ],

		// IU5VR1
		[ 'key'=>'separator',               'type'=>'Constant',     'len'=>1,       'val'=>' ', ],
		[ 'key'=>'ticketType',              'type'=>'Set',          'len'=>1,       'val'=>[' ', 'A', 'E', 'M', ],            ],
		[ 'key'=>'validatingCarrierCode',   'type'=>'StringTrim',   'len'=>2, ],
		[ 'key'=>'transactionType',         'type'=>'Set',          'len'=>1,       'val'=>['#', '-', '@', '$', ],            ],
		[ 'key'=>'ticketNumber',            'type'=>'StringTrim',   'len'=>10, ],
		[ 'key'=>'separator',               'type'=>'Constant',     'len'=>1,       'val'=>'/', ],
		[ 'key'=>'commission',              'type'=>'FloatWithOptionalPercent',     'len'=>[0,9], ],
		[ 'key'=>'separator',               'type'=>'Constant',     'len'=>1,       'val'=>'/', ],
		[ 'key'=>'baseFareCurrency',        'type'=>'Currency',     'len'=>3, 'req'=>false, ],
		[ 'key'=>'baseFareAmount',          'type'=>'FloatRaw', ],
		[ 'key'=>'separator',               'type'=>'Constant',     'len'=>1,       'val'=>'/', ],
		[ 'key'=>'tax1Amount',              'type'=>'FloatRaw', ],
		[ 'key'=>'separator',               'type'=>'Constant',     'len'=>1,       'val'=>'/', ],
		[ 'key'=>'tax2',                                            'req'=>false,    'merge'=>true,                     'children'=>[
			[ 'key'=>'tax2AmountCode',          'type'=>'Set',                       'len'=>1, 'val'=>['A', 'B', 'C', 'D', ]],
			[ 'key'=>'tax2Amount',              'type'=>'FloatRaw',                      ],
			[ 'key'=>'separator',               'type'=>'Constant',                  'len'=>1,       'val'=>'/', ],
		]],
		[ 'key'=>'tax3Amount',              'type'=>'FloatRaw',        'req'=>false,],
		[ 'key'=>'unknown01s',              'type'=>'Constant',     'req'=>false,'len'=>1,       'val'=>'/', ],
		[ 'key'=>'unknown01v',              'type'=>'Constant',     'req'=>false,'len'=>2,       'val'=>'C0', ],
		[ 'key'=>'separator',               'type'=>'Constant',     'req'=>false,'len'=>1,       'val'=>'/', ],
		[ 'key'=>'priceType',               'type'=>'Set',           'len'=>3,       'val'=>['ONE', 'PER', 'ALL', ],           ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'formOfPayment',           'type'=>'Alphanumeric', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>' ', ],
		[ 'key'=>'passengerName',           'type'=>'PassengerRawName', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'conjunctionTickets',      'type'=>'Integer', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'routingIndicator',        'type'=>'Set',           'len'=>1,       'val'=>['D', 'F', 'T', 'I', ],                 ],
		[ 'key'=>'NL',        'type'=>'NewLine', ],
	];

	protected $children_6_1 = [
		'key' => 			'6',
		'subkey' => 		'1',
		'description' => 	'Manual Non-ARC/BSP Document/US/Downtown version (6.1)',

		[ 'key'=>'IU5MID',    'type'=>'Constant',     'len'=>2,       'val'=>'M5', ],
		[ 'key'=>'IU5PTY',    'type'=>'StringTrim',   'len'=>2, ],
		[ 'key'=>'IU5MIN',    'type'=>'StringTrim',   'len'=>2, ],

		// IU5VR1
		[ 'key'=>'IU3LNK',                  'type'=>'StringTrim',    'len'=>1, ],
		[ 'key'=>'transactionType',         'type'=>'Set',           'len'=>1,       'val'=>['L', 'B', ' ', ],                      ],
		[ 'key'=>'vendorCode',              'type'=>'StringDelimitedByChar',         'val'=>'/', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'transactionType2',        'type'=>'Letters',       'len'=>3,       'req'=>false, ],
		[ 'key'=>'documentNumber',          'type'=>'Alphanumeric',                  'req'=>false, ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'commission',              'type'=>'FloatWithOptionalPercent',     'len'=>[0,9], ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'baseFareCurrency',        'type'=>'Currency',      'len'=>3,       'req'=>false, ],
		[ 'key'=>'baseFareAmount',          'type'=>'FloatRaw', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'tax1Amount',              'type'=>'BlockDelimited',      'req'=>false,          'merge'=>true,  'children'=> [
			[  'key'=>'tax1Amount',  'type'=>'FloatRaw', ],
			[  'key'=>'separator',   'type'=>'Constant',   'val'=>'/', ],
		]],
		[ 'key'=>'tax2',                                                             'req'=>false,   'merge'=>true,                     'children'=>[
		 [  'key'=>'tax2Amount',  'type'=>'FloatRaw', ],
		 [  'key'=>'separator',   'type'=>'Constant',   'len'=>1,   'val'=>'/', ],
		 ]],
		[ 'key'=>'tax3Amount',              'type'=>'FloatRaw',                      'req'=>false,],
		[ 'key'=>'separator',               'type'=>'Constant',                      'req'=>false,'len'=>1,       'val'=>'/', ],
		[ 'key'=>'priceType',               'type'=>'Set',           'len'=>3,       'val'=>['ONE', 'PER', 'ALL', ],           ],

		[ 'key'=>'customEOL1',                                                                 'req'=>false,   'merge'=>true,                     'children'=>[
			[ 'key'=>'separator',               'type'=>'Set',                                 'val'=>['/', '-'] , ],
			[ 'key'=>'formOfPayment',           'type'=>'AlphanumericWithStars', ],
			[ 'key'=>'customEOL2',                                                             'req'=>false,   'merge'=>true,                     'children'=>[
				[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,         'val'=>' ', 'req'=>false,  ],
				[ 'key'=>'PNR_Name_number_1',       'type'=>'Integer',                                     'req'=>false,  ],
				[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,         'val'=>'.', 'req'=>false,  ],
				[ 'key'=>'PNR_Name_number_2',       'type'=>'Integer',                                     'req'=>false,  ],
				[ 'key'=>'passengerName',           'type'=>'PassengerRawName', ],
			]],

			[ 'key'=>'customEOL3',                                                             'req'=>false,   'merge'=>true,                     'children'=>[
				[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,         'val'=>'/', ],
				[ 'key'=>'numberOfDocuments',       'type'=>'Alphanumeric', ],
			]],

			[ 'key'=>'customEOL4',                                                             'req'=>false,   'merge'=>true,                     'children'=>[
				[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
				[ 'key'=>'routingIndicator',        'type'=>'Set',           'len'=>1,       'val'=>['D', 'F', 'T', ],                 ],
			]],
		]],

		[ 'key'=>'otherText',      		    'type'=>'StringRaw',        'req'=>false, ],

		[ 'key'=>'NL',        'type'=>'NewLine', ],
	];

	protected $children_6_2 = [
		'key' => 			'6',
		'subkey' => 		'2',
		'description' => 	'Manual Non-ARC/BSP Document/US/Downtown version, refund (6.2)',

		[ 'key'=>'IU5MID',    'type'=>'Constant',     'len'=>2,       'val'=>'M5', ],
		[ 'key'=>'IU5PTY',    'type'=>'StringTrim',   'len'=>2, ],
		[ 'key'=>'IU5MIN',    'type'=>'StringTrim',   'len'=>2, ],

		// IU5VR1
		[ 'key'=>'IU3LNK',                  'type'=>'StringTrim',    'len'=>1, ],
		[ 'key'=>'transactionType',         'type'=>'Set',           'len'=>1,       'val'=>['R', ],                      ],
		[ 'key'=>'vendorCode',              'type'=>'StringTrim',    'len'=>6, ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'transactionType2',        'type'=>'StringRaw',     'len'=>3,        ],
		[ 'key'=>'documentNumber',          'type'=>'Alphanumeric',                  'req'=>false, ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'commission',              'type'=>'FloatWithOptionalPercent',     'len'=>[0,9], ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'baseFareCurrency',        'type'=>'Currency',      'len'=>3,       'req'=>false, ],
		[ 'key'=>'baseFareAmount',          'type'=>'FloatRaw', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'tax1Amount',              'type'=>'FloatRaw', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'tax2',                                                             'req'=>false,   'merge'=>true,                     'children'=>[
		 [  'key'=>'tax2Amount',  'type'=>'FloatRaw', ],
		 [  'key'=>'separator',   'type'=>'Constant',   'len'=>1,   'val'=>'/', ],
		 ]],
		[ 'key'=>'tax3Amount',              'type'=>'FloatRaw',                      'req'=>false,],
		[ 'key'=>'separator',               'type'=>'Constant',                      'req'=>false,'len'=>1,       'val'=>'/', ],
		[ 'key'=>'priceType',               'type'=>'Set',           'len'=>3,       'val'=>['ONE', 'PER', 'ALL', ],           ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'formOfPayment',           'type'=>'Alphanumeric', ],
		[ 'key'=>'customEOL2',                                                             'req'=>false,   'merge'=>true,                     'children'=>[
			[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,         'val'=>' ', ],
			[ 'key'=>'PNR_Name_number_1',       'type'=>'Integer',                 ],
			[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,         'val'=>'.', ],
			[ 'key'=>'PNR_Name_number_2',       'type'=>'Integer',                 ],
			[ 'key'=>'passengerName',           'type'=>'PassengerRawName', ],
		]],

		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'numberOfDocuments',       'type'=>'Alphanumeric', ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', ],
		[ 'key'=>'refundedCoupons',         'type'=>'InSet',                         'val'=>['1', '2', '3', '4', 'A', ',', ],       'req' =>false, ],
		[ 'key'=>'separator',               'type'=>'Constant',      'len'=>1,       'val'=>'/', 'req' =>false, 'requires' => ['refundedCoupons'], ],
		[ 'key'=>'refundType',              'type'=>'Set',           'len'=>1,       'val'=>['F', 'P', ],                      ],
		[ 'key'=>'otherText',      		    'type'=>'StringRaw',        'req'=>false, ],

		[ 'key'=>'NL',        'type'=>'NewLine', ],
	];

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

		try {
			$ret = parent::_extract($ret, $originalRawData);
		}
		catch (\App\Utils\BaseParser\TextParser\Exception\MissingRequiredElementException $ex) {
			$extracted = false;

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

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

			if (!$extracted) {
				$extracted = $this->_extractFromChildren($this->children_6_2, $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 M5 line. No defined format matches it", null, null, $this);
			}
		}

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


	protected function setupLineIsAttributes($ret)
	{
		$ret = parent::setupLineIsAttributes($ret);

		if (in_array($ret->extractedData['typeWithSubkey_'], ['6.1', '6.2', ])) {
			$ret->extractedData["isIssue_"] = 	!(bool)$ret->extractedData["ticketType_"]['refund'];
			$ret->extractedData["isRefund_"] = 	(bool)$ret->extractedData["ticketType_"]['refund'];
		}

		return $ret;
	}
}
