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

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__];

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

		if($indexType=='U' && $U['_type']=='08'){
			$details = $this->getDetails_U_08();
			if($details){
				$this->CACHE[__METHOD__] = $details;
				return $details;
			}
		}

		if($indexType=='U' && $U['_type']=='09'){
			$details = $this->getDetails_U_09();
			if($details){
				$this->CACHE[__METHOD__] = $details;
				return $details;
			}
		}

		return null;
	}

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

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

		$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
		$item->car_carCode = 		$U['CAR'];
		$item->car_carCode_text = 	trim($U['CARTRANS']);
		$item->car_numberOfCars = 	(int)($U['NBRCARS']);

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

		return $item;
	}

	/**
	 * Get data from a specific line
	 * @return \App\Amadeus\StructureParser\Types\ServiceDetails
	 */
	protected function getDetails_U_09()
	{
		$item = $this->getDetails_U_08();

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

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

		$item->car_carMake_text = trim($U['VEHINFO']['VEHMAKE']);

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

		return $item;
	}
}
