<?php
namespace App\Amadeus\CompatibilityWithImportedOrders;

class TaxManipulationsCached extends TaxManipulations
{
	private $CACHE = [];


	/**
	 * Get the parent entity for this service
	 * @param \Entity\AmadeusService $entity
	 * @return \Entity\AmadeusService|NULL
	 */
	public function getParentEntity($entity)
	{
		$cacheKey = sprintf("%s:%s", __METHOD__, $entity->getId());


		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = parent::getParentEntity($entity);
		}

		return $this->CACHE[$cacheKey];
	}

	/**
	 * Get the service itinerary, or the service's parent itinerary, if this is a refund cached
	 * @param \Entity\AmadeusService $entity
	 * @param \Entity\AmadeusService $parentEntity
	 * @return Array
	 */
	public function TINA_getServiceItinerary($entity, $parentEntity=null)
	{
		$cacheKey = sprintf("%s:%s,%s", __METHOD__, $entity->getId(), ($parentEntity?$parentEntity->getId():null));

		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = $entity->transactionSegmentData->getSegments();
		}
		return $this->CACHE[$cacheKey];
	}

	/**
	 * Get TINA offer location cached
	 * This would be better moved in the CompatibilityWithImportedOrders layer instead, as these calculations are "simulated" the same way TINA handles them, and it seems to impact the layer closer to TINA than the GDS layer
	 * @param array $segments
	 */
	public function TINA_getOfferLocation(array $segments)
	{
		$cacheKey = sprintf("%s:%s", __METHOD__, md5(serialize($segments)));

		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = parent::TINA_getOfferLocation($segments);
		}
		return $this->CACHE[$cacheKey];
	}

	/**
	 * Get TINA VAT configuration
	 * This would be better moved in the CompatibilityWithImportedOrders layer instead, as these calculations are "simulated" the same way TINA handles them, and it seems to impact the layer closer to TINA than the GDS layer
	 *
	 * @param array $segments
	 * @param String $transactionType
	 * @return []
	 */
	public function TINA_getVatDetails(array $segments, $transactionType)
	{
		$cacheKey = sprintf("%s:%s,%s", __METHOD__, md5(serialize($segments)), $transactionType);

		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = parent::TINA_getVatDetails($segments, $transactionType);
		}
		return $this->CACHE[$cacheKey];
	}
}
