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

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

		$indexType = 		$this->transaction->getIndexType();

		if($indexType=='IVD'){
			$price = $this->getDetails_IVD();
			if($price){
				$this->CACHE[__METHOD__] = $price;
				return $price;
			}
		}

		if($indexType=='U'){
			$price = $this->getDetails_U();
			if($price){
				$this->CACHE[__METHOD__] = $price;
				return $price;
			}
		}

		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=='U') {
			// lookup passengers by name, it seems that thats the only way to correctly find the association between U and I segments in this case
			$U = $parsedStructure['U'][$index->segment];
			$I = \App\Amadeus\StructureParser\Helpers::internal_insurance_U_to_I($parsedStructure, $U);
			$item = $this->getRemarksFromPassenger($parsedStructure, $index, $item, $I);
		}

		if ($indexType=='IVD') {
			$I = $parsedStructure['I'][0];
			$item = $this->getRemarksFromPassenger($parsedStructure, $index, $item, $I);
		}

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


	/**
	 * Get the details from the IVD line
	 */
	public function getDetails_IVD()
	{
		$parsedStructure = 	$this->transaction->getParsedStructure();

		$item = null;
		$IVD = $parsedStructure['IVD'];
		if ($IVD['GEOZNDATA']) {
			$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
			$item->occupancy = 				count($parsedStructure['I']);	// valid for all passengers in the file?
			$item->insurance_validityArea = 		$IVD['GEOZNDATA'];

			$item->insurance_providerCode = 		$IVD['INPVCODE'];
			$item->insurance_providerCode_text = 	$IVD['INPVNAME'];
			$item->insurance_productCode = 			$IVD['INPDCODE'];
			$item->insurance_productCode_text = 	$IVD['INPDNAME'];

			$extensions = [];
			foreach($IVD['EXTN'] as $EXTN){
				$ext = new \App\Amadeus\StructureParser\Types\InsuranceExtension();
				$ext->code = $EXTN['EXTNCODE'];
				$ext->type = $EXTN['EXTTYPE'];
				$ext->name = $EXTN['EXTNNAME'];
				$extensions[] = $ext;
			}
			$item->insurance_extensions = 	$extensions;
		}

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

		return $item;
	}

	/**
	 * Get the details from the U line
	 */
	public function getDetails_U()
	{
		$parsedStructure = 	$this->transaction->getParsedStructure();
		$index = 			$this->transaction->getIndex();

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

		$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
		if ($U['GEOZNDATA']) {
			$item->occupancy = 				count($parsedStructure['I']);	// valid for all passengers in the file?
			$item->insurance_validityArea = $U['GEOZNDATA'];
		}

		$item->insurance_providerCode = 	$U['INPVCODE'];
		$item->insurance_providerCode_text =$U['INPVNAME'];
		$item->insurance_productCode = 		$U['INPDCODE'];
		$item->insurance_productCode_text = $U['INPDNAME'];


		if ($U['EXTN']) {
			$extensions = [];
			foreach($U['EXTN'] as $EXTN){
				$ext = new \App\Amadeus\StructureParser\Types\InsuranceExtension();
				$ext->code = $EXTN['EXTNCODE'];
				$ext->type = $EXTN['EXTTYPE'];
				$ext->name = $EXTN['EXTNNAME'];
				$extensions[] = $ext;
			}
			$item->insurance_extensions = 	$extensions;
		}

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

		return $item;
	}
}
