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

/**
 *
 * @author Lucian Sirbu
 */
class RG extends \App\Worldspan\LineParser\Types\WorldspanBaseLine
{
	protected $extractRegexp = "/^(?P<rawData>G.+)/sm";

	protected $children_default = [
        /**
         * This record contains the fare, taxes, total fare and equivalent fare for the ticketed air segments
         * These are the amounts to be printed in the appropriate boxes of the ticket
         * In addition, the â€œInvoice Air Amountâ€ will be sent (when entered)
         * This new field is used in conjunction with a tour ticketing identifier
         */

		['key'=> 'key',                               'type'=> 'Constant',          'len'=> 1,     'val'=> 'G',   'req'=> true,],              // Record identifier
        ['key'=> 'CurrencyCodeOfFare',                'type'=> 'StringTrim',        'len'=> 3,                    'req'=> false,],             // Currency code of fare (see Note 2)
        ['key'=> 'FareAmount',                        'type'=> 'FloatRaw',          'len'=> 8,                    'req'=> false,],             // Fare amount
        ['key'=> '#015',                              'type'=> 'Separator',                                       'req'=> false,],             // Field separator
        ['key'=> 'Tax1CodeIdentifier',                'type'=> 'StringRaw',         'len'=> 3,                    'req'=> false,],             // Tax 1 code identifier (see Note 3)
        ['key'=> 'Tax1Amount',                        'type'=> 'FloatRaw',          'len'=> 8,                    'req'=> false,],             // Tax 1 amount
        ['key'=> '#018',                              'type'=> 'Separator',                                       'req'=> false,],             // Field separator
        ['key'=> 'Tax2CodeIdentifier',                'type'=> 'StringRaw',         'len'=> 3,                    'req'=> false,],             // Tax 2 code identifier
        ['key'=> 'Tax2Amount',                        'type'=> 'FloatRaw',          'len'=> 8,                    'req'=> false,],             // Tax 2 amount
        ['key'=> '#021',                              'type'=> 'Separator',                                       'req'=> false,],             // Field separator
        ['key'=> 'Tax3CodeIdentifier',                'type'=> 'StringRaw',         'len'=> 3,                    'req'=> false,],             // Tax 3 code identifier
        ['key'=> 'Tax3Amount',                        'type'=> 'FloatRaw',          'len'=> 8,                    'req'=> false,],             // Tax 3 amount
        ['key'=> '#024',                              'type'=> 'Separator',                                       'req'=> false,],             // Field separator
        ['key'=> 'CurrencyCodeOfTotalFare',           'type'=> 'StringTrim',        'len'=> 3,                    'req'=> false,],             // Currency code of Total Fare
        ['key'=> 'TotalFareAmount',                   'type'=> 'FloatRaw',          'len'=> 8,                    'req'=> false,],             // Total Fare amount
        ['key'=> '#027',                              'type'=> 'Separator',                                       'req'=> false,],             // Field separator
        ['key'=> 'CurrencyCodeOfEquivalentFare',      'type'=> 'StringTrim',        'len'=> 3,                    'req'=> false,],             // Currency code of equivalent fare
        ['key'=> 'EquivalentFareAmount',              'type'=> 'FloatRaw',          'len'=> 8,                    'req'=> false,],             // Equivalent fare amount
        ['key'=> 'CurrencyCodeOfEquivalentFare_alt',  'type'=> 'Constant',          'len'=> 1,     'val'=> '*',   'req'=> false,],             // Currency code of equivalent fare
        ['key'=> '#030',                              'type'=> 'Separator',                                       'req'=> false,],             // Field Separator
        ['key'=> 'key',                               'type'=> 'Constant',          'len'=> 2,     'val'=> 'LT',  'req'=> false,],             // Invoice Air Amount identifier (see note 12)
        ['key'=> 'InvoiceAirAmount',                  'type'=> 'FloatRaw',          'len'=> [1, 9],          'req'=> false,],             // Invoice Air Amount


	];

	protected $children_noFare = [
        /**
         * Note 6: When a non-air transaction is sent without a @B (fare override amount), the record layout will be as follows: G*#*#*#*#*. The equivalent fare fields are not transmitted.
         * Note 8: In the case of a "No Fare" transaction (e.g. EZ#FNF) the record layout will be shipped with zeroes. In the case of a "Boarding Pass Only" transaction (e.g. EZB), the record layout will be as follows: G*#*#*#*#*.
         * 	The equivalent fare fields are not transmitted.
         * 	For the EMD (Electronic Miscellaneous Document) transaction, the G record will be sent with asterisks: G*#*#*#*#*.
         * Note 10: When processed as part of an exchange transaction the record is transmitted as G*#*#*#*#* for all locations except those using the ARC method of exchanges. (The equivalent fare fields are not transmitted.)
         * 	See Record X for a list of ARC countries.
         */
        ['key'=> 'complex:1',                        'type'=> 'Complex', 'merge'=>true,                                                                       'req'=> false, 'children'=> [
            ['key'=> 'key',                              'type'=> 'Constant',                         'len'=> 10,             'val'=> 'G*\*\*\*\*',               'req'=> true,],
            ['key'=> 'noFare',                           'type'=> 'InternalMarker',                   'val'=> true,           'req'=> true,],
		]],
	];

	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 = $ret;

		$ret->remainingBlockData = $this->rawData;
		if (!$extracted->extractedData) {
			$extracted = $this->_extract_children($ret, $this->children_noFare, $this->rawData, [$this, "_extract_children_callback_associativeArray"]);
		}

		$ret->remainingBlockData = $this->rawData;
		if (!$extracted->extractedData) {
			$extracted = $this->_extract_children($ret, $this->children_default, $this->rawData, [$this, "_extract_children_callback_associativeArray"]);
		}

		if ($extracted->extractedData) {
			$ret->remainingBlockData = false;
		}

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

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


	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		for ($i=1; $i<=3; $i++) {
			if ($ret->extractedData["Tax{$i}CodeIdentifier"]) {
				$ret->extractedData["Tax{$i}CodeIdentifier"] = trim($ret->extractedData["Tax{$i}CodeIdentifier"]);
				if ($ret->extractedData["Tax{$i}CodeIdentifier"][0]=='X' && strlen($ret->extractedData["Tax{$i}CodeIdentifier"])==3) {
					$ret->extractedData["Tax{$i}CodeIdentifier"] = substr($ret->extractedData["Tax{$i}CodeIdentifier"], 1);
				}
			}
		}


		return $ret;
	}
}
