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

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

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

		foreach ($transactionIndexes['ticketingSegments'] as $segIdx) {
			$seg = $parsedStructure['segments'][$segIdx];
			$segment = new \App\Worldspan\StructureParser\Types\ItinerarySegment();

			$segment = $this->fillItinerarySegmentR01($segment, $seg, $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations);

			$item->appendSegment($segment);
		}

		return $item;
	}

	protected function fillItinerarySegmentR01(\App\Worldspan\StructureParser\Types\ItinerarySegment $segment, array $seg, $parsedStructure, string $transactionType, array $transactionIndexes, array $internalAssociations)
	{
		$segment = $this->fillItinerarySegmentRXX($segment, $seg, $seg['R01'], $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations);
		$segment->fareBasis = 				$seg['R01']['FareBasisCodeTicketDesignator'];
		$segment->ticketDesignator = 		explode("/", $seg['R01']['FareBasisCodeTicketDesignator'])[1];

		return $segment;
	}

	protected function fillItinerarySegmentR02(\App\Worldspan\StructureParser\Types\ItinerarySegment $segment, array $seg, $parsedStructure, string $transactionType, array $transactionIndexes, array $internalAssociations)
	{
		$segment = $this->fillItinerarySegmentRXX($segment, $seg, $seg['R02'], $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations);

		return $segment;
	}

	protected function fillItinerarySegmentRXX(\App\Worldspan\StructureParser\Types\ItinerarySegment $segment, array $seg, array $RXX,$parsedStructure, string $transactionType, array $transactionIndexes, array $internalAssociations)
	{
		// departure
		$dep = new \App\Worldspan\StructureParser\Types\ItineraryLocation();
		$match = $this->match([
			'airportCode' => [ $RXX['OriginAirportCode'], ],
		]);

		$dep->suggest("airportCode", $RXX['OriginAirportCode']);

		$dep->airportCode = $match->airportCode;
		$dep->cityId = 		$match->cityId;
		$dep->countryCode = $match->countryCode;
		$dep->date = 		$RXX['DepartureDate'];
		$dep->time = 		$RXX['DepartureTime'];

		$dep->terminal = 	$RXX['DepartureAirportTerminalCode'];

		$segment->departure = $dep;


		// destination
		$dest = new \App\Worldspan\StructureParser\Types\ItineraryLocation();
		$match = $this->match([
			'airportCode' => [ $RXX['DestinationAirportCode'], ],
		]);

		$dest->suggest("airportCode", $RXX['DestinationAirportCode']);

		$dest->airportCode = $match->airportCode;
		$dest->cityId = 	 $match->cityId;
		$dest->countryCode = $match->countryCode;
		$dest->date = 		$RXX['ArrivalDate'];
		$dest->time = 		$RXX['ArrivalTime'];

		$dest->terminal = 	$RXX['ArrivalAirportTerminalCode'];

		$segment->destination = $dest;

		if ($RXX['NumberOfIntermediateStops']=='*') {
			$segment->stopOverPermitted = null;
		}
		elseif ($RXX['NumberOfIntermediateStops']=='0') {
			$segment->stopOverPermitted = false;
			$segment->stopCounts = 		0;
		}
		else {
			$segment->stopOverPermitted = true;
			$segment->stopCounts = 		(int)$RXX['NumberOfIntermediateStops'];
		}

		$segment->duration = 	$RXX['AccumulatedElapsedFlightTime'];
		$segment->distance = 	$RXX['SegmentMileage'];

		$segment->supplier->codeAirline = 	$RXX['CarrierCode'];

		if ($seg['R06']['FreeflowInformation']) {
			$segment->operatedBy->name = 		$seg['R06']['FreeflowInformation'];
		}

		$segment->serviceClassCode = 		$RXX['ClassOfService_']['code'];
		$segment->serviceClass = 			$RXX['ClassOfService_']['label'];
		$segment->segmentStatus = 			$RXX['TicketSegmentStatus'];

		$segment->mealCodes =				array_map(function($a){
			return $a['code'];
		}, (array)$RXX['MealServiceCodes_']);

		$segment->mealLabels =				array_filter(array_map(function($a){
			return $a['label'];
		}, (array)$RXX['MealServiceCodes_']));

		$segment->transportNumber = 		$RXX['FlightNumber'];
		$segment->transportModel = 			$RXX['EquipmentType'];

		$segment->fareBasis = 				$seg['R01']['FareBasisCodeTicketDesignator'];
		$segment->ticketDesignator = 		explode("/", (string)$seg['R01']['FareBasisCodeTicketDesignator'])[1];

		$segment->fareBasis = 				$seg['R01']['FareBasisCodeTicketDesignator'];
		$segment->ticketDesignator = 		explode("/", (string)$seg['R01']['FareBasisCodeTicketDesignator'])[1];

		$segment->baggageAllowance = 		$RXX['BaggageRestriction'];

		// lookup segment seat numbers in the SSR remarks
		if ($parsedStructure['RM']['SSRSEAT']) {
			foreach ($parsedStructure['RM']['SSRSEAT'] as $SSR) {
				if (   $SSR['segmentDate'] == 	$RXX['DepartureDate']
					&& $SSR['departure'] == 	$RXX['OriginAirportCode']
					&& $SSR['arrival'] == 		$RXX['DestinationAirportCode']) {

					$segment->transportSeatNumber = 	$SSR['seat'];
					break;
				}
			}
		}

		return $segment;
	}
}
