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

class Itinerary extends \App\Amadeus\StructureParser\Structures\MCO\Itinerary
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Itinerary::isValid()
	 */
	public function isValid()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();

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

		if($indexType=='EMD'){
			if(isset($parsedStructure['EMD'])) {
				return true;
			}
		}

		if($indexType=='U' && $U['_type']=='19'){
			if(isset($parsedStructure['U'][$index->segment??null])) {
				return true;
			}
		}

		return false;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Itinerary::updateInternalSegmentData()
	 */
	protected function updateInternalSegmentData()
	{
		$indexType = $this->transaction->getIndexType();

		if($indexType=='EMD'){
			$ret = $this->updateInternalSegmentData_EMD();
			if($ret){
				return $ret;
			}
		}

		return false;
	}


	/**
	 * Get data from specific line
	 * @return multitype:\App\Amadeus\StructureParser\Types\ItinerarySegment
	 */
	protected function updateInternalSegmentData_EMD()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		foreach ($index->EMD as $EMDIndex) {
			$EMDt = $parsedStructure['EMD'][$EMDIndex];
			$I = \App\Amadeus\StructureParser\Helpers::internal_MCO_MCO_to_I($parsedStructure, $EMDt, $this->transaction);

			foreach ($parsedStructure['EMD'] as $idx=>$EMD) {
				$add = false;
				if ($EMD && $idx==$EMDIndex) {
					$add = true;
				}

				if ($add && $EMD['PSGASS']) {
					$add = false;
					foreach ($EMD['PSGASS'] as $PSGASS) {
						if ($PSGASS==(int)$I['PSGRNBR']) {
							$add = true;
							break;
						}
					}
				}

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

					$item->departure->suggest('airport', 	$EMD['ORIGIN']);
					$item->departure->suggest('airport', 	$EMD['DESTIN']);

					// detect the departure airport/city/country
					$match = $item->match([
						'airportCode' => 	[$EMD['ORIGIN']],
					]);
					$item->departure->airportCode = 	$match->airport??null;
					$item->departure->cityId = 			$match->city??null;
					$item->departure->countryCode = 	$match->country??null;

					// detect the destination airport/city/country
					$match = $item->match([
						'airportCode' => 	[$EMD['DESTIN']],
					]);
					$item->destination->airportCode = 	$match->airport??null;
					$item->destination->cityId = 		$match->city??null;
					$item->destination->countryCode = 	$match->country??null;

					// detect departure/arrival dates & hours
					$item->departure->date = 		$EMD['PRCNDATE'];
					$item->destination->date = 		$EMD['PRCNDATE'];

					if ($EMD['NVADATE']) {
						$item->departure->date = 	$EMD['NVADATE'];
						$item->destination->date = 	$EMD['NVADATE'];
					}

					// transport details
					$item->supplierCode = 			rtrim($EMD['AIRCDE']);
					$item->segmentType = 			$EMD['_type'];

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

		return true;
	}

	/**
	 * Get data from specific line
	 */
	protected function updateInternalSegmentData_U()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

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

		$itinerary = [];
		foreach($parsedStructure['U'] as $segIdx=>$EMD){
			if($EMD['_type']=='19' && in_array($segIdx, $parsedStructure['assoc:U:pax-segment'][$index->passenger])){
				$item = new \App\Amadeus\StructureParser\Types\ItinerarySegment();

				$item->departure->suggest('airport', 	$EMD['ORIGIN']);
				$item->departure->suggest('airport', 	$EMD['DESTIN']);

				// detect the departure airport/city/country
				$match = $item->match([
					'airportCode' => 	[$EMD['ORIGIN']],
				]);
				$item->departure->airportCode = 	$match->airport;
				$item->departure->cityId = 			$match->city;
				$item->departure->countryCode = 	$match->country;

				// detect the destination airport/city/country
				$match = $item->match([
					'airportCode' => 	[$EMD['DESTIN']],
				]);
				$item->destination->airportCode = 	$match->airport;
				$item->destination->cityId = 		$match->city;
				$item->destination->countryCode = 	$match->country;


				// detect departure/arrival dates & hours
				$item->departure->date = 		$EMD['PRCNDATE'];
				$item->destination->date = 		$EMD['PRCNDATE'];

				// transport details
				$item->supplierCode = 			rtrim($U['AIRCDE']);
				$item->segmentType = 			$U['_type'];
				$this->itinerary->appendSegment($item);
			}
		}
		return true;
	}
}
