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

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

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

		if($indexType=='H'){
			$doc = $this->document_H();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		if($indexType=='H:reissue'){
			$doc = $this->document_H_reissue();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		if($indexType=='U:ticketing'){
			$doc = $this->document_U();
			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=='H'){
			$doc = $this->supplier_H();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		if($indexType=='H:reissue'){
			$doc = $this->supplier_H();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		if($indexType=='U:ticketing'){
			$doc = $this->supplier_U_1();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		if($indexType=='U:ticketing'){
			$doc = $this->supplier_U_2();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}

		if($indexType=='U:ticketing'){
			$doc = $this->supplier_U_3();
			if($doc){
				$this->CACHE[__METHOD__] = $doc;
				return $doc;
			}
		}
		return null;
	}


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

		$item = null;
		// this works for ticketed segments
		$I = $parsedStructure['I'][$index->passenger];
		$item = new \App\Amadeus\StructureParser\Types\Document();
		$item->createDate= 		$parsedStructure['D']['PNRDTE'];
		$item->issueDate= 		$parsedStructure['D']['AIRDTE'];

		$item->docDate = 		$parsedStructure['D']['AIRDTE'];
		$item->docNumber = 		$I['T']['TKTNBR'];
		$item->docNumbers = 	$I['T']['TKTNBR_'];
		$item->docType = 		$I['T']['_type'];

		if($I['TK'] && count($I['TK'])>1){
			$this->transaction->addLog(__METHOD__.": Found multiple TK lines. Using only the first one to extract data");
		}
		$item->docIssued= 		$I['TK'][0]['issued'];
		if($I['TK'] && $I['TK'][0]['timeLimit']){
			$item->docValidUntil= 	$I['TK'][0]['timeLimit'];
		}

		$item->transactionCode = $this->createTransactionCodeFromAssoc($parsedStructure['AMD']['MUC']['RECLOC']['RECLOC'], $I['GCPSGR'], array_keys($parsedStructure['H']), $parsedStructure['H'], 'TTONBR', ['01', '03']);

		$item->supplierRECLOC = $parsedStructure['AMD']['MUC']['nationalSystemPNR'][0]['NSRECLOC'];

		$item = $this->intern_extractTicketStock($item, $parsedStructure, $I);

		return $item;
	}

	/**
	 * Get data from a specific line
	 * @return \App\Amadeus\StructureParser\Types\Document
	 */
	protected function document_H_reissue()
	{
		$item = $this->document_H();
		$item->docSubType = 	'reissue';

		return $item;
	}

	/**
	 * Get data from a specific line
	 * @return \App\Amadeus\StructureParser\Types\Document
	 */
	protected function document_U()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();
		$I = $parsedStructure['I'][$index->passenger];

		$item = null;
		// this works for ticketed segments
		$item = new \App\Amadeus\StructureParser\Types\Document();
		$item->createDate= 		$parsedStructure['D']['PNRDTE'];
		$item->issueDate= 		$parsedStructure['D']['AIRDTE'];

		$item->docIssued= 		$I['TK'][0]['issued'];
		if($I['TK'][0]['timeLimit']){
			$item->docValidUntil= 	$I['TK'][0]['timeLimit'];
		}

		$item->docDate = 		$parsedStructure['D']['AIRDTE'];


		if ($I['T']) {
			$item->docNumber = 		$I['T']['TKTNBR'];
			$item->docNumbers = 	$I['T']['TKTNBR_'];
			$item->docType = 		$I['T']['_type'];
		}

		$segs = [];
		if (isset($index->TSA)) {
			foreach($parsedStructure['assoc:pax:TSA-segment'][$index->passenger][$index->TSA] as $segmentIdx){
				if($parsedStructure['U'][$segmentIdx]['_type']=='01'){	// filter only ticketing segments
					$segs[] = $parsedStructure['U'][$segmentIdx]['TTONBR'];
				}
			}
		}
		else {
			foreach($parsedStructure['assoc:U:pax-segment'][$index->passenger] as $segmentIdx){
				if($parsedStructure['U'][$segmentIdx]['_type']=='01'){	// filter only ticketing segments
					$segs[] = $parsedStructure['U'][$segmentIdx]['TTONBR'];
				}
			}
		}
		$item->transactionCode = $this->createUniqueTransactionCode($parsedStructure['AMD']['MUC']['RECLOC']['RECLOC'], $parsedStructure['I'][$index->passenger]['GCPSGR'], $segs, $parsedStructure, $I);

		return $item;
	}

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

		// initialize the return result
		$item = null;
		$I = $parsedStructure['I'][$index->passenger];

		if(	isset($I['T']['NUMAIR'])) {
			$item = new \App\Amadeus\StructureParser\Types\Supplier();
			$item->code3 = 			$I['T']['NUMAIR'];
			$item->codeAirline = 	$parsedStructure['A']['ALCDE'];
			$item->name = 			$parsedStructure['A']['TKTAIR'];
		}

		return $item;
	}

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

		// initialize the return result
		$item = null;
		$I = $parsedStructure['I'][0];

		if($I && $I['TMC']){
			foreach($I['TMC'] as $TMC){
				$item = new \App\Amadeus\StructureParser\Types\Supplier();
				$item->code3 = 		$TMC['NUMAIR'];	// airlineCode
				break;	// only pick the first one
			}
		}

		return $item;
	}

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

		// initialize the return result
		$item = null;
		$I = $parsedStructure['I'][0];

		if($I && $I['FV']){
			foreach($I['FV'] as $FV){
				$item = new \App\Amadeus\StructureParser\Types\Supplier();
				$item->codeAirline = 		$FV['TKTCARR'];	// Ticketing Carrier/Invoice/BP etc....
				break;	// only pick the first one
			}
		}

		return $item;
	}

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

		// initialize the return result
		$item = null;
		$U = $parsedStructure['U'][$index->segment];

		if($U && $U['AIRCDE']){
			$item = new \App\Amadeus\StructureParser\Types\Supplier();
			$item->codeAirline = 		$U['AIRCDE'];
		}

		return $item;
	}
}
