<?php
namespace App\Amadeus\StructureParser\Parser;

/**
 * Base class, used by individual transaction classes
 * @author Lucian
 */
class Passengers
{
	/**
	 * The calling parent class
	 * @var \App\Amadeus\Storage\Operation
	 */
	protected $transaction = null;

	protected $passengers = [];

	/**
	 * Internal cache, to keep track of instantiated objects
	 * @var array
	 */
	protected $CACHE = [];

	public function __construct($transaction)
	{
		$this->transaction = 	$transaction;
		//
	}

	/**
	 * Cleanup any memory references
	 */
	public function destroy()
	{
		foreach($this->CACHE as $k=>$obj){
			unset($this->CACHE[$k]);
		}

		foreach($this->passengers as $idx=>$pax){
			unset($this->passengers[$idx]);
		}
		$this->transaction = 	null;
	}

	/**
	 * Set the internal cache data. Used to overwrite parsed data
	 */
	public function setInternalCache($CACHE)
	{
		$this->CACHE = $CACHE;
		return $this;
	}

	/**
	 * Check if this is a valid transaction
	 * @return boolean|default:true
	 */
	public function isValid()
	{
		return true;
	}

	/**
	 * Get a list of all passengers
	 * @return array:\App\Amadeus\StructureParser\Types\Passenger
	 */
	public function getAll()
	{
		throw new \Exception(__METHOD__." Not implemented!");
	}

	/**
	 * Get the owner(main) passenger on this trnsaction
	 * @return \App\Amadeus\StructureParser\Types\Passenger
	 */
	public function getOwner()
	{
		throw new \Exception(__METHOD__." Not implemented!");
	}

	/**
	 * Get the number of passengers in this transaction
	 * @return number
	 */
	public function getCount()
	{
		return count($this->getAll());
	}

	/**
	 * Extract the FFCards list from the file
	 * @param Array $parsedStructure
	 * @param \App\Amadeus\StructureParser\Types\Passenger $item
	 * @param Array $I
	 */
	protected function internal_getFreqentFlyerCards($parsedStructure, $index, $item, $I)
	{
		$FFCards = [];
		$rawFFCards = [];
		if($I['FQV']){
			foreach($I['FQV'] as $FQV){
				$rawFFCards[$FQV['PRIOR']][] = ['supplier' => $FQV['AIRCDE'], 'card'=>$FQV['FQTVNBR']];
			}
		}

		if($I['FQR']){
			foreach($I['FQR'] as $FQR){
				$rawFFCards['z'][] = 			['supplier' => $FQR['AIRCDE'], 'card'=>$FQR['FQTRNBR']];
			}
		}

		if($I['FQS']){
			foreach($I['FQS'] as $FQS){
				$rawFFCards[$FQS['PRIOR']][] = ['supplier' => $FQS['AIRCDE'], 'card'=>$FQS['FQSNBR']];
			}
		}

		if($I['FQU']){
			foreach($I['FQU'] as $FQU){
				$rawFFCards[$FQU['PRIOR']][] = ['supplier' => $FQU['AIRCDE'], 'card'=>$FQU['FQUNBR']];
			}
		}

		if($rawFFCards){
			// flatten the list of cards, sorting by priority, where available, and removing duplicate cards
			ksort($rawFFCards);
			foreach($rawFFCards as $cards){
				foreach($cards as $card){
					$key = sprintf("%s.%s", $card['supplier'], $card['card']);
					$FFCards[$key] = $card;
				}
			}

			$FFCards = array_values($FFCards);
		}

		$item->FFCards = $FFCards;
		return $item;
	}


	/**
	 * Go through the provided list of passengers and pick the first owner found
	 * @param array $passengers
	 * @return \App\Amadeus\StructureParser\Types\Passenger
	 */
	protected function internal_pickOwner($passengers)
	{
		foreach($passengers as $pax) {
			/* @var $pax \App\Amadeus\StructureParser\Types\Passenger */
			if($pax->isOwner) {
				return $pax;
			}
		}

		return $passengers[0];
	}
}
