<?php
namespace App\Amadeus\StructureParser\Structures\MCO;

class Passengers extends \App\Amadeus\StructureParser\Parser\Passengers
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Passengers::getAll()
	 */
	public function getAll()
	{
		return [$this->getOwner()];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Passengers::getOwner()
	 */
	public function getOwner()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];
//		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
//		$index = $this->transaction->getIndex();
//		$U = $parsedStructure['U'][$index->segment];

		if($indexType=='MCO'){
			$pax = $this->getOwner_MCO();
			if($pax){
				$this->CACHE[__METHOD__] = $pax;
				return $pax;
			}
		}

		return null;
	}

	/**
	 * Get data from specific line
	 * @return \App\Amadeus\StructureParser\Types\Passenger
	 */
	protected function getOwner_MCO()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$MCO = $parsedStructure['MCO'][$index->MCO];
		$I = \App\Amadeus\StructureParser\Helpers::internal_MCO_MCO_to_I($parsedStructure, $MCO, $this->transaction);

		if($I){
			$item = new \App\Amadeus\StructureParser\Types\Passenger();

			$item->title = 		implode(" ", array_filter([$I['PSGRNME_honorifics_'], $I['PSGRNME_title_']]));
			$item->type = 		($I['PSGRID_']['type']?$I['PSGRID_']['type']:'adult');
			$item->firstName = 	$I['PSGRNME_firstName'];
			$item->lastName =	$I['PSGRNME_lastName'];

			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractType($item, $I, $parsedStructure, $index, $this->transaction);
			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractAmadeusId($item, $I, $this->transaction, $parsedStructure);
			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractAddress($item, $I, $this->transaction);
			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractPassport($item, $I, $this->transaction);
			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractDateOfBirth($item, $I, $parsedStructure, $index, $this->transaction);

		}

		return $item;
	}
}
