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

class Document extends \App\Amadeus\StructureParser\Parser\Document
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Document::document()
	 */
	public function document()
	{
		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']=='10'){
			$doc = $this->document_U_10();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		return null;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Document::supplier()
	 */
	public function supplier()
	{
		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']=='10'){
			$doc = $this->supplier_U_10();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		return null;
	}


	/**
	 * Extract data from a specific line
	 * @return \App\Amadeus\StructureParser\Types\Document
	 */
	protected function document_U_10()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();	// $index is the segment number
//		$U = $parsedStructure['U'][$index->segment];

		// initialize the return result
		$item = new \App\Amadeus\StructureParser\Types\Document();
		$item->createDate= 		$parsedStructure['D']['PNRDTE'];
		$item->issueDate= 		$parsedStructure['D']['AIRDTE'];

		$item->docDate = 		$parsedStructure['D']['AIRDTE'];
		$I = $parsedStructure['I'][$index->passenger];
		$item->transactionCode = $this->createTransactionCodeFromAssoc($parsedStructure['AMD']['MUC']['RECLOC']['RECLOC'], $I['GCPSGR'], [$index->segment], $parsedStructure['U'], 'TTONBR');

		return $item;
	}

	/**
	 * Extract data from a specific line
	 * @return \App\Amadeus\StructureParser\Types\Supplier
	 */
	protected function supplier_U_10()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();
		$U = $parsedStructure['U'][$index->segment];

		// initialize the return result
		$item = new \App\Amadeus\StructureParser\Types\Supplier();
		$item->codeAirline =$U['AIRCDE'];

		$item->productCode = $U['GRNDCDE'];
		$item->productName = $U['GRNDCDE_'];

		return $item;
	}
}
