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

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

		$item = new \App\Galileo\StructureParser\Types\Itinerary();
		if ($transactionIndexes['source']=='A167') {
			$item = $this->_getData_A167($item, $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations);
		}
		elseif($transactionIndexes['source']=='A16A') {
			$item = $this->_getData_A16A($item, $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations);
		}
		else {
			throw new \Exception("Unknown transaction source: '{$transactionIndexes['source']}'");
		}

		return $item;
	}

	protected function _getData_A167(\App\Utils\BaseParser\Structure\StructureParser\Types\Itinerary $item, $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations)
	{
		$A167 = $parsedStructure['A167'][0];
		$segment = new \App\Galileo\StructureParser\Types\ItinerarySegment();

		// departure
		$dep = new \App\Galileo\StructureParser\Types\ItineraryLocation();
		$dep->date = 		$A167['A167']['A16DTE'];
		$segment->departure = $dep;

		// destination
		$dest = new \App\Galileo\StructureParser\Types\ItineraryLocation();
		$match = $this->match([
			'cityCode' => [$A167['A167']['A16CTY'],],
		]);
		$dest->suggest('cityCode', $A167['A167']['A16CTY']);

		$dest->airportCode = 	$match->airportCode;
		$dest->cityId = 		$match->cityId;
		$dest->countryCode = 	$match->countryCode;
		$dest->date = 			$A167['A167']['A16OUT'];
		$segment->destination = $dest;

		$item->appendSegment($segment);

		return $item;
	}


	protected function _getData_A16A(\App\Utils\BaseParser\Structure\StructureParser\Types\Itinerary $item, $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations)
	{
		$A16A = $parsedStructure['A16A'][0];
		$segment = new \App\Galileo\StructureParser\Types\ItinerarySegment();

		// departure
		$dep = new \App\Galileo\StructureParser\Types\ItineraryLocation();
		$dep->date = 		$A16A['A16A']['A16DTE'];
		$segment->departure = $dep;

		// destination
		$dest = new \App\Galileo\StructureParser\Types\ItineraryLocation();
		$match = $this->match([
			'cityCode' => [$A16A['A16A']['A16CTY'],],
		]);
		$dest->suggest('cityCode', $A16A['A16A']['A16CTY']);

		$dest->airportCode = 	$match->airportCode;
		$dest->cityId = 		$match->cityId;
		$dest->countryCode = 	$match->countryCode;
		$dest->date = 			\Engine\DateTime::createFromIsoDate($dep->date)->modify("+{$A16A['A16A']['A16DAY']}days")->formatDateIso();
		$segment->destination = $dest;

		$item->appendSegment($segment);

		return $item;
	}
}
