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

class Passengers extends \App\Amadeus\StructureParser\Structures\Ticketing\Passengers
{

	public function getOwner()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];

		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		foreach ($parsedStructure['assoc:U:segment-pax'][$index->segment] as $paxIdx) {
			$I = $parsedStructure['I'][$paxIdx];
			$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_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);

			if (count($this->passengers)) {
				$item->isOwner = false;
			}

			$this->passengers[] = $item;
		}

		if (count($this->passengers)>1) {
			$this->transaction->addLog(__METHOD__.": This reservation has more than one passenger, picking the first one as 'owner'");
		}

		// "randomly" pick the first defined passenger as the owner
		$this->CACHE[__METHOD__] = $this->passengers[0];

		return $this->CACHE[__METHOD__];
	}

	public function getAll()
	{
		return $this->passengers;
	}
}
