<?php
namespace App\Amadeus\FileParser;

class Line_X extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.21. X- Operational Flight Air Segment
	 * see page 97(466)
	 * Used to store the data related to an Air Segment Line in the case where operational flight information
	 * 	differs from that of the commercial flight number. These lines will immediately follow the "H-" or "U-"
	 * 	lines to which they are associated and will bear the same "SEGNBR" and "TTONBR".
	 * @author Lucian
	 */
 	protected $structure = [
		['name'=> 'MSGTAG',  'val' => 'X-',    'type' => 'AN',   'req' => 'M',  'len' => 2,],                          // 1 : Message Tag (X-)
		['name'=> 'TTONBR',                    'type' => 'N',    'req' => 'M',  'len' => 3,],                          // 2 : Tattoo Number in the PNR
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 3 : Delimiter (Constant value of ';')
		['name'=> 'SEGNBR',                    'type' => 'N',    'req' => 'M',  'len' => 3,],                          // 4 : Segment Number in the PNR
		['name'=> 'STOP',                      'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 5 : Stopover Indicator (X is no stopover permitted, O is stopover permitted)
		['name'=> 'ORIGAIR',                   'type' => 'AN',   'req' => 'M',  'len_min' => 3,   'len_max' => 5,],    // 6 : Origin Airport Code
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 7 : Delimiter (Constant value of ';')
		['name'=> 'ORIGCTY',                   'type' => 'AN',   'req' => 'M',],                                       // 8 : Origin City Name plus Airport detail if applicable
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 9 : Delimiter (Constant value of ';')
		['name'=> 'DESTAIR',                   'type' => 'AN',   'req' => 'M',  'len_min' => 3,   'len_max' => 5,],    // 10 : Destination Airport Code
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 11 : Delimiter (Constant value of ';')
		['name'=> 'DESTCTY',                   'type' => 'AN',   'req' => 'M',],                                       // 12 : Destination City Name plus Airport detail if applicable
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 13 : Delimiter (Constant value of ';')
		['name'=> 'complex_01',                'type' => 'AN',   'req' => 'M',],
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 22 : Delimiter (Constant value of ';')
		['name'=> 'STATUS',                    'type' => 'AN',   'req' => 'M',  'len' => 4,],                          // 23 : Status Code and Number in Party
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 24 : Delimiter (Constant value of ';')
		['name'=> 'PNRSTAT',                   'type' => 'AN',   'req' => 'M',  'len' => 4,],                          // 25 : PNR Status Code and Number in Party
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 26 : Delimiter (Constant value of ';')
		['name'=> 'MEAL',                      'type' => 'AN',                  'len' => 2,],                          // 27 : Meal Code
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 28 : Delimiter (Constant value of ';')
		['name'=> 'NBRSTOP',                   'type' => 'N',    'req' => 'M',  'len' => 1,],                          // 29 : Number of Stops
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 30 : Delimiter (Constant value of ';')
		['name'=> 'EQUIP',                     'type' => 'AN',                  'len' => 3,],                          // 31 : Equipment Type
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 32 : Delimiter (Constant value of ';')
		['name'=> 'ENTER',                     'type' => 'AN',                  'len' => 1,],                          // 33 : Entertainment Code
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 34 : Delimiter (Constant value of ';')
		['name'=> 'SHRCOMM',                   'type' => 'AN',],                                                       // 35 : Shared Designator Commuter Airline Name
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 36 : Delimiter (Constant value of ';')
		['name'=> 'BAGALL',                    'type' => 'AN',                  'len' => 3,],                          // 37 : Baggage Allowance
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 38 : Delimiter (Constant value of ';')
		['name'=> 'CKINTRM',                   'type' => 'S:trim',              'len' => 2,],                          // 39 : Check-In Terminal
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 40 : Delimiter (Constant value of ';')
		['name'=> 'CKINTIM',                   'type' => 'AN',                  'len' => 5,],                          // 41 : Latest Check-In Time 24 Hour Clock HHMM 12 Hour Clock HHMMx Where X is: A=AM, P=PM, N=Noon, M=Midnight
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 42 : Delimiter (Constant value of ';')
		['name'=> 'TKTT',                      'type' => 'AN',                  'len' => 2,],                          // 43 : Electronic Ticket Segment Indicator (Value of 'ET')
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 44 : Delimiter (Constant value of ';')
		['name'=> 'FLTDUR',                    'type' => 'AN',                  'len' => 5,],                          // 45 : Flight Duration Time
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 46 : Delimiter (Constant value of ';')
		['name'=> 'NONSMOK',                   'type' => 'AN',                  'len' => 1,],                          // 47 : Flight Non-smoking Indicator
		['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',  'len' => 1,],                          // 48 : Delimiter (Constant value of ';')
		['name'=> 'ARRTRM',                    'type' => 'S:trim',              'len' => 2,],                          // 49 : Arrival Terminal
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::postParse()
	 */
	protected function postParse()
	{
		parent::postParse();

		if ($this->tree['complex_01']) {
			$structure = null;
			if (strlen($this->tree['complex_01'])==35) {
				$structure = [
					[ 'name'=> 'AIRCDE',                    'type' => 'AN',   'req' => 'M',                                   'len' => 6,],                          // 14 : Airline Code.
					// FLTNBR also contains the SUFFIX field
					[ 'name'=> 'FLTNBR',                    'type' => 'AN',   'req' => 'M',                                   'len' => 5,],                          // 15 : Flight Number.
					[ 'name'=> 'CLSSVC',                    'type' => 'AN',   'req' => 'M',                                   'len' => 2,],                          // 16 : Class of Service.
					[ 'name'=> 'CLSBKG',                    'type' => 'AN',   'req' => 'M',                                   'len' => 2,],                          // 17 : Class of Booking.
					[ 'name'=> 'DEPDTE',                    'type' => 'D:DDMMM:AIRDTE','req' => 'M',                          'len' => 5,],                          // 18 : Departure Date (DDMMM)
					[ 'name'=> 'DEPTIM',                    'type' => 'HR',   'req' => 'M',                                   'len' => 5,],                          // 19 : Departure Time 24 Hour Clock HHMM 12 Hour Clock HHMMx Where x is: A=AM, P=PM, N=Noon, M=Midnight
					[ 'name'=> 'ARRTIM',                    'type' => 'HR',   'req' => 'M',                                   'len' => 5,],                          // 20 : Arrival Time 24 Hour Clock HHMM 12 Hour Clock HHMMx Where x is: A=AM, P=PM, N=Noon, M=Midnight
					[ 'name'=> 'ARRDTE',                    'type' => 'D:DDMMM:AIRDTE','req' => 'M',                          'len' => 5,],
				];
			}
			elseif (strlen($this->tree['complex_01'])==43) {
				$structure = [
					[ 'name'=> 'AIRCDE',                    'type' => 'AN',   'req' => 'M',                                   'len' => 6,],                          // 14 : Airline Code.
					// FLTNBR also contains the SUFFIX field
					[ 'name'=> 'FLTNBR',                    'type' => 'AN',   'req' => 'M',                                   'len' => 5,],                          // 15 : Flight Number.
					[ 'name'=> 'CLSSVC',                    'type' => 'AN',   'req' => 'M',                                   'len' => 2,],                          // 16 : Class of Service.
					[ 'name'=> 'CLSBKG',                    'type' => 'AN',   'req' => 'M',                                   'len' => 2,],                          // 17 : Class of Booking.
					[ 'name'=> 'DEPDTE',                    'type' => 'D:DDMMMYYYY|DDMMM:AIRDTE','req' => 'M',                'len' => 9,],                          // 18 : Departure Date (DDMMM)
					[ 'name'=> 'DEPTIM',                    'type' => 'HR',   'req' => 'M',                                   'len' => 5,],                          // 19 : Departure Time 24 Hour Clock HHMM 12 Hour Clock HHMMx Where x is: A=AM, P=PM, N=Noon, M=Midnight
					[ 'name'=> 'ARRTIM',                    'type' => 'HR',   'req' => 'M',                                   'len' => 5,],                          // 20 : Arrival Time 24 Hour Clock HHMM 12 Hour Clock HHMMx Where x is: A=AM, P=PM, N=Noon, M=Midnight
					[ 'name'=> 'ARRDTE',                    'type' => 'D:DDMMMYYYY|DDMMM:AIRDTE','req' => 'M',                'len' => 9,],
				];
			}
			else {
				throw new \Exception(sprintf("The X line has an unknown format of the internal complex_01 field. Field length: %d", strlen($this->tree['complex_01'])));
			}

			$index = 0;
			$complex_01 = $this->parseFromStructure_v2($structure, $this->tree['complex_01'], $index, []);

			// "inject" the new field in the place of the "complex_01" pseudo-field
			$this->tree = array_merge(
				array_slice($this->tree, 0, array_search('complex_01', array_keys($this->tree)), true),
				$complex_01,
				array_slice($this->tree, array_search('complex_01', array_keys($this->tree))+1, null, true)
			);
		}

	}
}
