<?php
namespace App\Galileo\StructureParser\Structures\Ticketing;

class Itinerary extends \App\Galileo\StructureParser\Structures\Base\Itinerary
{
	protected function _getData()
	{
		list($parsedStructure, , , $internalAssociations) = $this->getTransactionDetails();

		$item = new \App\Galileo\StructureParser\Types\Itinerary();

		if ($parsedStructure['A04']) {
			foreach ($parsedStructure['A04'] as $k04=>$a04) {
				$segment = new \App\Galileo\StructureParser\Types\ItinerarySegment();

				$a08 = $parsedStructure['A08'][$internalAssociations['A04->A08'][$k04][0]];

				// departure
				$dep = new \App\Galileo\StructureParser\Types\ItineraryLocation();
				$match = $this->match([
					'airportCode' => [$a04['A04']['A04OCI']['A04OCC'],],
					'airportName' => [$a04['A04']['A04OCI']['A04OCN'],],
				]);

				$dep->suggest("airportCode", $a04['A04']['A04OCI']['A04OCC']);
				$dep->suggest("airportName", $a04['A04']['A04OCI']['A04OCN']);

				$dep->airportCode = $a04['A04']['A04OCI']['A04OCC'];
				$dep->cityId = 		$match->cityId;
				$dep->countryCode = $match->countryCode;
				$dep->date = 			$a04['A04']['A04DTE'];
				$dep->time = 			$a04['A04']['A04TME'];

				$segment->departure = $dep;


				// destination
				$dest = new \App\Galileo\StructureParser\Types\ItineraryLocation();
				$match = $this->match([
					'airportCode' => [$a04['A04']['A04DCI']['A04DCC'],],
					'airportName' => [$a04['A04']['A04DCI']['A04DCN'],],
				]);

				$dest->suggest("airportCode", $a04['A04']['A04DCI']['A04DCC']);
				$dest->suggest("airportName", $a04['A04']['A04DCI']['A04DCN']);

				$dest->airportCode = 	$match->airportCode;
				$dest->cityId = 		$match->cityId;
				$dest->countryCode = 	$match->countryCode;
				$dest->date = 			$a04['A04']['A04DTE_A04IND'];
				$dest->time = 			$a04['A04']['A04ARV'];

				$segment->destination = $dest;

				$segment->stopOverPermitted = ($a04['A04']['A04STP']=='O');

				$segment->duration = 	$a04['A04']['A04JTM'];
				$segment->distance = 	$a04['A04']['A04MIL'];

				$segment->supplier->code3 = 		$a04['A04']['A04NUM'];
				$segment->supplier->codeAirline = 	$a04['A04']['A04CDE'];
				$segment->supplier->name = 			$a04['A04']['A04NME'];

				$segment->transportNumber = 		$a04['A04']['A04FLT'];
				$segment->transportModel = 			$a04['A04']['A04AIR'];

				$segment->serviceClassCode = 		$a04['A04']['A04CLS_']['code'];
				$segment->serviceClass = 			$a04['A04']['A04CLS_']['label'];
				$segment->segmentStatus = 			$a04['A04']['A04STS'];

				$segment->mealCodes =				$a04['A04']['A04SVC_'];

				if ($a04 && $a04['A04']['A04BAG_']) {
					$segment->baggageAllowance = 		sprintf("%s %s", $a04['A04']['A04BAG_']['quantity'], $a04['A04']['A04BAG_']['UM']);
				}
				elseif ($a08 && $a08['A08']['A08BAG_']) {
	            	$segment->baggageAllowance = sprintf('%s %s', $a08['A08']['A08BAG_']['quantity'], $a08['A08']['A08BAG_']['UM']);
	            }

				if (count((array)$internalAssociations['A04->A08'][$k04])>1) {
					$this->transaction->getMessagesCollector()->addWarning(sprintf("[%s] there are multiple A08 lines associated with this A04 line. Picking the first one for the fareBasis", __METHOD__));
				}

				if ($a08) {
					$segment->fareBasis = ($a08['A08']['A08CFB'] ?: $a08['A08']['A08FBC']);
					$segment->ticketDesignator = $a08['A08']['A08CFB'] ? explode('/', $a08['A08']['A08CFB'])[1] : null;
				}

				$item->appendSegment($segment);
			}
		}

		return $item;
	}
}
