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

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']=='17'){	// U-MISC
			$doc = $this->document_U_17();
			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__];
		$indexType = $this->transaction->getIndexType();

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

		return null;
	}


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

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

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

		if($parsedStructure['D']['AIRDTE']){
			$item->docDate = 		$parsedStructure['D']['AIRDTE'];
		}else{
			$item->docDate = 		$U['DATE'];
		}

		$item->docType = 		$U['_type'];

		$item->transactionCode = $this->createTransactionCode($parsedStructure['AMD']['MUC']['RECLOC']['RECLOC'], $I['GCPSGR'], [$U['TTONBR']]);

		if ($U['FREE_']['_type']=='CABFORCE') {
			$item->docNumber = $U['FREE_']['confNumber'];
			$item->docNumbers[] = $item->docNumber;
			$item->docSubType = $U['FREE_']['_type'];
		}
		elseif ($U['FREE_']['_type']=='visa') {
			$item->docNumber = $U['FREE_']['confirmationNumber'];
			$item->docNumbers[] = $item->docNumber;
			$item->docSubType = $U['FREE_']['_type'];
		}
		elseif ($U['FREE_']['_type']=='meetAndAssist') {
			$item->docNumber = $U['FREE_']['confirmationNumber'];
			$item->docNumbers[] = $item->docNumber;
			$item->docSubType = $U['FREE_']['_type'];
		}

		return $item;
	}

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

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

		$item = new \App\Amadeus\StructureParser\Types\Supplier();

		$item->codeAirline = 	$U['SUPPCDE'];
		if ($U['FREE_']['_type']=='CABFORCE') {
			$item->name = 			'CABFORCE';
			$item->codeAirline = 	null;
			$item->productCode = 	'cab';
			$item->productName = 	'Cab';
		}
		elseif ($U['FREE_']['_type']=='visa') {
			$item->codeAirline = null;
			$item->productCode = $U['FREE_']['productType'];
			$item->productName = $U['FREE_']['productType'];
		}
		elseif ($U['FREE_']['_type']=='meetAndAssist') {
			$item->codeAirline = null;
		}

		return $item;
	}
}
