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

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=='H') {
			$details = $this->getDetails_H();
			if ($details) {
				$this->CACHE[__METHOD__] = $details;
				return $details;
			}
		}

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

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

		return null;
	}

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

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

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

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

		if ($indexType=='U:ticketing') {
			$details = $this->getRelations_U();
			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_H()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

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

		$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
		$this->extractTourCode($parsedStructure, $index, $item);

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

		return $item;
	}

	/**
	 * Get data from a specific line
	 * @return \App\Amadeus\StructureParser\Types\Relations
	 */
	protected function getRelations_H()
	{
		return parent::getRelations();
	}

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

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

		$item = new \App\Amadeus\StructureParser\Types\ServiceDetails();
		$this->extractTourCode($parsedStructure, $index, $item);

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

		return $item;
	}

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

		$relations = new \App\Amadeus\StructureParser\Types\Relations();
		if ($I['FO']) {
			$previousTickets = $I['FO'][0]['previousTickets'];

			/**
			 * Ideally we should report everything from FO line in here, but we need to keep compatibility with the original code (before JIRA: TD-8640([Incomplete] US - Reissue values are not interpreted correctly, for bookings imported from Amadeus))
			 * So I'm doing lots of filtering and slicing in order to try and report relations for compatibility (as was before 2023-04-xx, JIRA: TD-8640)
			 * I'm also trying to prioritize segments where the conjunctions are defined (the same ticket might be reported twice, once with conjunctions, once without)
			 */
			$previousTicketNumbersWithCnj = [];
			foreach ($previousTickets as $pt) {
				if (count($pt['tktNoArr'])>1) {
					$previousTicketNumbersWithCnj[$pt['tktNo']]++;
				}
			}
			$prevTkt = reset(array_reverse(array_slice(array_filter($previousTickets, function ($pt) use ($previousTicketNumbersWithCnj) {
				if ($previousTicketNumbersWithCnj[$pt['tktNo']] && count($pt['tktNoArr'])==1) {
					return false;
				}
				return !$pt['involuntary_reissue'];
			}), 0, 2)));

			foreach ($prevTkt['tktNoArr'] as $tktNo) {
				$r = new \App\Amadeus\StructureParser\Types\Relation();
				$r->docNumber = 	$tktNo;
				$r->relationType = 'reissueOf';

				$relations->relations[] = $r;
			}

		}

		return $relations;
	}

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

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

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

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

		return $item;
	}

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

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

		$relations = new \App\Amadeus\StructureParser\Types\Relations();
		$relations = $this->extractReferencesData($parsedStructure, $index, $relations);

		if ($I['FH']) {
			foreach ($I['FH'] as $FH) {
				if ($FH['HANDTKT_']['tktNo']) {
					$r = new \App\Amadeus\StructureParser\Types\Relation();
					$r->docNumber = 	$FH['HANDTKT_']['tktNo'];
					$r->relationType = 'reference';

					$relations->relations[] = $r;
				}
			}
		}

		return $relations;
	}
}
