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

class Passengers extends \App\Amadeus\StructureParser\Parser\Passengers
{

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

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

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

		return null;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Passengers::getAll()
	 */
	public function getAll()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];
		$indexType = $this->transaction->getIndexType();

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

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

		return null;
	}

	/**
	 * Get the owner as reported in the IVD or U-INS line
	 * @return \App\Amadeus\StructureParser\Types\Passenger
	 */
	protected function getDirectOwnerIVD()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
//		$index = $this->transaction->getIndex();

		$IVD = $parsedStructure['IVD'];
		$item = new \App\Amadeus\StructureParser\Types\Passenger();

		$item->title = 		$IVD['PSGRNME_title_'];
		$item->type = 		'adult'; // TODO: might not work correctly; should be somethign like: ($I['PSGRID_']['type']?$I['PSGRID_']['type']:'adult');
		$item->firstName = 	$IVD['PSGRNME_firstName'];
		$item->lastName =	$IVD['PSGRNME_lastName'];

		return $item;
	}

	/**
	 * Get the owner as reported in the IVD or U-INS line
	 * @return \App\Amadeus\StructureParser\Types\Passenger
	 */
	protected function getDirectOwnerU()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$U = $parsedStructure['U'][$index->segment];
		$item = new \App\Amadeus\StructureParser\Types\Passenger();

		$item->title = 		$U['PSGRNME_title_'];
		$item->type = 		($U['PSGRID_']['type']?$U['PSGRID_']['type']:'adult');
		$item->firstName = 	$U['PSGRNME_firstName'];
		$item->lastName =	$U['PSGRNME_lastName'];

		return $item;
	}


	protected function getOwner_IVD()
	{
		return $this->internal_pickOwner($this->getAll_IVD());
	}

	protected function getOwner_U()
	{
		return $this->internal_pickOwner($this->getAll_U());
	}

	protected function getAll_IVD()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$owner = $this->getDirectOwnerIVD();

		$passengers = [];
		foreach($parsedStructure['assoc:IVD:segment-pax'][$index->segment] as $paxIdx){
			$item = new \App\Amadeus\StructureParser\Types\Passenger();

			$I = $parsedStructure['I'][$paxIdx];
			$item->isOwner = 	($I['PSGRNME_firstName']==$owner->firstName && $I['PSGRNME_lastName']==$owner->lastName);

			$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);
			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractDateOfBirth($item, $I, $parsedStructure, $index, $this->transaction);

			$passengers[] = $item;
		}
		return $passengers;
	}

	protected function getAll_U()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$owner = $this->getDirectOwnerU();

		$passengers = [];

		foreach($parsedStructure['assoc:U:segment-pax'][$index->segment] as $paxIdx){
			$item = new \App\Amadeus\StructureParser\Types\Passenger();

			$I = $parsedStructure['I'][$paxIdx];
			$item->isOwner = 	($I['PSGRNME_firstName']==$owner->firstName && $I['PSGRNME_lastName']==$owner->lastName);

			$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);
			$item = \App\Amadeus\StructureParser\Helpers::internal_passenger_extractDateOfBirth($item, $I, $parsedStructure, $index, $this->transaction);

			$passengers[] = $item;
		}
		return $passengers;
	}
}
