<?php
namespace App\Bsp\Hot230\Files;

class Hot230 extends \App\Utils\BaseParser\TextParser\FileParser\ParserForIndividualLines
{
	protected $children = [
		[ 'key' => 'header',  'startsWith' => 'Header\\BFH01', 'children' => [
			[ 'key' => 'BFH01',  'type' => 'Header\\BFH01', 'req' => true, ],
		]],

		[ 'key'=>'perCurrencyTransactions', 'startsWith'=>'Header\\BCH02',  'rep'=> [0, 10], 'children'=>[
			[ 'key'=>'BCH02',  'type'=>'Header\\BCH02', 'req' =>false, ],
			[ 'key' => 'perOfficeTransactions',
				'endsWithAnyOf' => ['Footer\\BCT95', 'Footer\\BFT99','Footer\\BOT94', 'Footer\\BOT93', ],		// endsWithAnyOf is used as a form of optimization
				'rep' => [0, PHP_INT_MAX], 'children' => [

				[ 'key' => 'BOH03',  'type' => 'Header\\BOH03', 'req' => false, ],

				[ 'key' => 'rawTransactions',
					'startsWith' => 'Transaction\\BKT06',
					'endsWithAnyOf' => ['Footer\\BCT95', 'Footer\\BFT99','Footer\\BOT94', 'Footer\\BOT93', ],	// endsWithAnyOf is used as a form of optimization
					'rep' => [1, PHP_INT_MAX], 'children' => [

					[ 'key' => 'BKT06',  'type' => 'Transaction\\BKT06', 'req' => true, ],
					[ 'key' => 'perDocumentTransactions',  'startsWith' => 'Transaction\\BKS24',   'rep' => [1, 5], 'children' => [
						/** BKS24 values:
						 * ACMA         Agency Credit Memo
						 * ACMD         Agency Credit Memo Default
						 * ACNT         Airline Credit Non-document(e.g.; override payments)
						 * ADMA         Agency Debit Memo
						 * ADMD         Agency Debit Memo Default
						 * ADNT         Airline Debit Non-document (e.g.; airline fees)
						 * CANN         Cancelled computer generated document number
						 * CANR         Cancellation with Reversal
						 * CANX         Cancelled document
						 * EMDA         Electronic Miscellaneous Document Associated
						 * EMDS         Electronic Miscellaneous Document Standalone
						 * RFNC         Refund of Cancellation with Reversal
						 * RFND         Refund
						 * SALE         Summary of all Sales (used in BOT93 only)
						 * SPCR         Settlement Plan Credit
						 * SPDR         Settlement Plan Debit
						 * SSAC         Sales Summary Adjustment Notice, or Minor Adjustment Notice - due to agent
						 * SSAD         Same as SSAC, except - due to airline
						 * TASF         Travel Agent Service Fees
						 * TKTT         Electronic Ticketing Sale - Automated
						 */
						[ 'key' => 'BKS24',  'type' => 'Transaction\\BKS24', 'req' => true, ],
						[ 'key' => 'BKS30',  'type' => 'Transaction\\BKS30', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS31',  'type' => 'Transaction\\BKS31', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS39',  'type' => 'Transaction\\BKS39', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS49',  'type' => 'Transaction\\BKS49', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS42',  'type' => 'Transaction\\BKS42', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS45',  'type' => 'Transaction\\BKS45', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS46',  'type' => 'Transaction\\BKS46', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKS47',  'type' => 'Transaction\\BKS47', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKI61',  'type' => 'Transaction\\BKI61', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKI62',  'type' => 'Transaction\\BKI62', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKI63',  'type' => 'Transaction\\BKI63', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BAR64',  'type' => 'Transaction\\BAR64', 'req' => false, ],
						[ 'key' => 'BAR65',  'type' => 'Transaction\\BAR65', 'req' => false, ],
						[ 'key' => 'BAR66',  'type' => 'Transaction\\BAR66', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BAR67',  'type' => 'Transaction\\BAR67', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BMD75',  'type' => 'Transaction\\BMD75', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BMD76',  'type' => 'Transaction\\BMD76', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						[ 'key' => 'BKF81',  'type' => 'Transaction\\BKF81', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],

						[ 'key' => 'BCCBKP',  'req' => true, 'rep' => [1, 4], 'children' => [
							[ 'key' => 'BCC82',  'type' => 'Transaction\\BCC82', 'req' => false, ],
							[ 'key' => 'BCC83',  'type' => 'Transaction\\BCC83', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
							[ 'key' => 'BCX83',  'type' => 'Transaction\\BCX83', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
							[ 'key' => 'BKP84',  'type' => 'Transaction\\BKP84', 'req' => false, 'rep' => [1, PHP_INT_MAX], ],
						]],
					]],
				]],

				[ 'key' => 'BOT93',  'type' => 'Footer\\BOT93', 'req' => false, 'rep' => [1, 2], ],
				[ 'key' => 'BOT94',  'type' => 'Footer\\BOT94', 'req' => false, ],
			]],

			[ 'key' => 'BCT95',  'type' => 'Footer\\BCT95', 'req' => true, 'rep' => [1, 5], ],
		]],

		[ 'key' => 'footer', 'startsWith' => 'Footer\\BCT95', 'children' => [
			[ 'key' => 'BFT99',  'type' => 'Footer\\BFT99', 'req' => true, 'rep' => [1, 5], ],
		]],
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockAbstract::createObject()
	 */
	public function createObject($type, $options, $self)
	{
		$options['req'] = true;
		$obj = null;

		$class = sprintf("\App\Bsp\Hot230\Lines\%s", $type);
		if (class_exists($class)) {
			$obj = new $class($options, $self);
		}

		if (!$obj) {
			$obj = parent::createObject($type, $options, $self);
		}

		$obj->setHelper($this->lineHelper);
		return $obj;
	}

	protected function _extract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret, $originalRawData)
	{
		$originalRawData = str_replace(["\r\n", "\r", "\n",], "\n", $originalRawData);
		$originalRawData = rtrim($originalRawData, "\n");
		$ret = parent::_extract($ret, $originalRawData);

		foreach ($ret->extractedData['perCurrencyTransactions'] as $k=>$v) {
			$ret->extractedData['perCurrencyTransactions'][$k]= array_filter($ret->extractedData['perCurrencyTransactions'][$k]);
		}
		$ret->extractedData['perCurrencyTransactions'] = array_filter($ret->extractedData['perCurrencyTransactions']);

		return $ret;
	}
}