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

class ServiceDetails extends \App\Amadeus\StructureParser\Structures\MCO\ServiceDetails
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\ServiceDetails::getDetails()
	 */
	public function getDetails()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];

		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();
		$U = $parsedStructure['U'][$index->segment??null];

		if($indexType=='EMD'){
			$details = $this->getDetails_EMD();
			if($details){
				$this->CACHE[__METHOD__] = $details;
				return $details;
			}
		}

		if($indexType=='U' && $U['_type']=='19'){	// U-EMD
			$pax = $this->getDetails_U_19();
			if($pax){
				$this->CACHE[__METHOD__] = $pax;
				return $pax;
			}
		}

		return null;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\ServiceDetails::getRelations()
	 */
	public function getRelations()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];

		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();
		$U = $parsedStructure['U'][$index->segment??null];

		if($indexType=='EMD'){
			$details = $this->getRelations_EMD();
			if($details){
				$this->CACHE[__METHOD__] = $details;
				return $details;
			}
		}

		if($indexType=='U' && $U['_type']=='19'){	// U-EMD
			$details = $this->getRelations_U_19();
			if($details){
				$this->CACHE[__METHOD__] = $details;
				return $details;
			}
		}

		return null;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\ServiceDetails::getRemarks()
	 */
	public function getRemarks()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];

		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();

		$item = new \App\Amadeus\StructureParser\Types\Remarks();
		if ($indexType=='EMD') {
			$EMD = $parsedStructure['EMD'][$index->EMD[0]];
			$I = \App\Amadeus\StructureParser\Helpers::internal_MCO_MCO_to_I($parsedStructure, $EMD, $this->transaction);
			$item = $this->getRemarksFromPassenger($parsedStructure, $index, $item, $I, $indexType);
		}

		$this->CACHE[__METHOD__] = $item;
		return $item;
	}


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

		$item = null;
		$EMD = $parsedStructure['EMD'][$index->EMD[0]];
		if($EMD){
			$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
			$item->reasonCode = 		$EMD['REASON'];	// translated in RFICDESC
			$item->reasonCode_text = 	$EMD['RFICDESC'];
			$item->reasonSubCode = 		$EMD['RFISC'];	// translated in RFISCDESC
			$item->reasonSubCode_text = $EMD['RFISCDESC'];

			$item = $this->extractCommonServiceDetails($parsedStructure, $item);

			return $item;
		}

		return $item;
	}

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

		$EMD = $parsedStructure['EMD'][$index->EMD[0]];
		$relations = new \App\Amadeus\StructureParser\Types\Relations();
		if ($EMD && $EMD['ICW']) {
			// use an intermediar list, indexed by docNumber, to eliminate duplicate references
			$documentRelations = [];
			foreach ($EMD['ICW'] as $ICW) {
				foreach ($ICW['ICW'] as $subICW) {
					if ($subICW['ICWTKT1_']['tktNo']) {
						$r = new \App\Amadeus\StructureParser\Types\Relation();
						$r->docNumber = 	$subICW['ICWTKT1_']['tktNo'];
						$r->relationType = "reference";

						$documentRelations[$r->docNumber] = $r;
					}

					if ($subICW['ICWTKT2_']['tktNo']) {
						$r = new \App\Amadeus\StructureParser\Types\Relation();
						$r->docNumber = 	$subICW['ICWTKT2_']['tktNo'];
						$r->relationType = "reference";

						$documentRelations[$r->docNumber] = $r;
					}
				}
			}

			// add the gathered relations to the final list
			foreach ($documentRelations as $r) {
				$relations->relations[] = $r;
			}
		}

		return $relations;
	}

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

		$item = null;
		$U = $parsedStructure['U'][$index->segment];
		if($U){
			$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
			$item->reasonCode = 		$U['REASON'];	// translated in RFICDESC
			$item->reasonCode_text = 	$U['RFICDESC'];
			$item->reasonSubCode = 		$U['RFISC'];	// translated in RFISCDESC
			$item->reasonSubCode_text = $U['RFISCDESC'];

			$item = $this->extractCommonServiceDetails($parsedStructure, $item);

			return $item;
		}

		return $item;
	}

	/**
	 * Get data from specific line
	 * @return \App\Amadeus\StructureParser\Types\Relations
	 */
	protected function getRelations_U_19()
	{
		return parent::getRelations();
	}
}
