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

class Itinerary extends \App\Amadeus\StructureParser\Parser\Itinerary
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Itinerary::updateInternalSegmentData()
	 */
	protected function updateInternalSegmentData()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();
		$U = $parsedStructure['U'][$index->segment];

		if($indexType=='U' && $U['_type']=='17'){
			$seg = $this->updateInternalSegmentData_U_17();
			if($seg){
				return $seg;
			}
		}

		return false;
	}


	/**
	 * Extract data from a specific line
	 * @throws \App\Amadeus\StructureParser\StructureException
	 */
	protected function updateInternalSegmentData_U_17()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$item = new \App\Amadeus\StructureParser\Types\ItinerarySegment();

		$U = $parsedStructure['U'][$index->segment];

		if ($U['FREE_']['pickupLocation_type']=='iata') {
			$item->departure->suggest('airport', 			$U['FREE_']['pickupLocation']);
			$match = $item->match([
				'airportCode' => 	[$U['FREE_']['pickupLocation']],
				'cityCode' => 		[$U['FREE_']['pickupLocation']],
			], false);

			$item->departure->airportCode = 	$match->airport;
			$item->departure->cityId = 			$match->city;
			$item->departure->countryCode = 	$match->country;
		}
		else {
			$item->departure->suggest('address', 			$U['FREE_']['pickupLocation']);

			$remarks = $this->transaction->getServiceDetails()->getRemarks();
			$RM = new \App\Amadeus\StructureParser\Types\RemarkComponent();
			$RM->type = 'custom';
			$RM->text = sprintf("PICKUP_LOCATION=%s", $U['FREE_']['pickupLocation']);
			$remarks->remarks[]= $RM;
		}


		if ($U['FREE_']['dropoffLocation_type']=='iata') {
			$item->destination->suggest('airport', 			$U['FREE_']['dropoffLocation']);
			$match = $item->match([
				'airportCode' => 	[$U['FREE_']['dropoffLocation']],
				'cityCode' => 		[$U['FREE_']['dropoffLocation']],
			], false);

			$item->destination->airportCode = 	$match->airport;
			$item->destination->cityId = 		$match->city;
			$item->destination->countryCode = 	$match->country;
		}
		else {
			$item->departure->suggest('address', 			$U['FREE_']['dropoffLocation']);

			$remarks = $this->transaction->getServiceDetails()->getRemarks();
			$RM = new \App\Amadeus\StructureParser\Types\RemarkComponent();
			$RM->type = 'custom';
			$RM->text = sprintf("DROPOFF_LOCATION=%s", $U['FREE_']['dropoffLocation']);
			$remarks->remarks[]= $RM;

			$remarks = $this->transaction->getServiceDetails()->getRemarks();
		}

		$item->departure->date = 		$U['FREE_']['pickupDate'];
		$item->departure->time = 		$U['FREE_']['pickupTime'];

		$item->supplierCode = 			$U['FREE_']['supplierCode'];

		$item->bookingClassCode = 		$U['FREE_']['bookingClass'];

		$item->segmentType = 			$U['FREE_']['_type'];

		$this->itinerary->appendSegment($item);

		return true;
	}
}
