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

class Price extends \App\Amadeus\StructureParser\Parser\Price{
	/**
	 * Can be calculated from the TOP tag, or by scanning individual U tags, or by loading the IVD line
	 * @see \App\Amadeus\StructureParser\Parser\Price::passenger()
	 */
	public function passenger(){
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];

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

		if($indexType=='IVD'){
			$price = $this->passenger_IVD();
			if($price){
				$price->checkIntegrity($this->transaction);
				$this->CACHE[__METHOD__] = $price;
				return $price;
			}
		}

		if($indexType=='U'){
			$price = $this->passenger_U();
			if($price){
				$price->checkIntegrity($this->transaction);
				$this->CACHE[__METHOD__] = $price;
				return $price;
			}
		}

		// worst-case, used both by IVD & U
		$price = $this->passenger_TOP();
		if($price){
			$price->checkIntegrity($this->transaction);
			$this->CACHE[__METHOD__] = $price;
			return $price;
		}

		return null;
	}

	/*----------------------------------------------------------------------------------------------*/

	/**
	 * Get the price from the U line
	 * @return \App\Amadeus\StructureParser\Types\Price|NULL
	 */
	protected function passenger_U(){
		$parsedStructure = 	$this->transaction->getParsedStructure();
		$index = 			$this->transaction->getIndex();

		$U = $parsedStructure['U'][$index->segment];
		if($U['TOTPAMNT']){
			$item = new \App\Amadeus\StructureParser\Types\Price();

			$I = $parsedStructure['I'][$index->passenger];
			$item = $this->internal_getFormOfPayment($parsedStructure, $index, $item, $I);

			// extract supplier price
			$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
				'total',
				'U:TOTPAMNT',
				$U['TOTPAMNT'],
				null,
				$U['TOTPCURR']
			));

			// calculate the supplier price
			$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
				'supplier',
				'total',
				$item->total,
				null,
				$item->totalCurrency
			));

			// extract commission
			if ($U['COMAMNT']) {
				$item->componentAdd(new \App\Amadeus\StructureParser\Types\PriceComponent(
					'commission',
					'U:COMAMNT',
					$U['COMAMNT'],
					null,
					$U['COMCURR']
				));
				$item->commissionType = 	'value';
			}

			if($item->commissionCurrency && $item->commissionCurrency!=$item->supplierCurrency){
				throw new \App\Amadeus\StructureParser\StructureException("We were expecting that commission currency and supplier currency match");
			}

			if ($item->commission) {
				$item->componentAdd(new \App\Amadeus\StructureParser\Types\PriceComponent(
					'supplier',
					'commission',
					-1*$item->commission,
					null,
					$item->commissionCurrency
				));
			}

			$item = $this->internal_extractVAT($parsedStructure, $index, $item, null, []);

			return $item;
		}

		return null;
	}

	/**
	 * Get the price from the TOP line
	 * @return \App\Amadeus\StructureParser\Types\Price|NULL
	 */
	protected function passenger_TOP(){
		$parsedStructure = 	$this->transaction->getParsedStructure();
		$index = 			$this->transaction->getIndex();

		$TOP = $parsedStructure['TOP'];
		if ($TOP) {
			$item = new \App\Amadeus\StructureParser\Types\Price();

			foreach ($TOP as $totalPrice) {
				$thisPrice = new \App\Amadeus\StructureParser\Types\Price();

				// extract supplier price
				$thisPrice->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
					'total',
					'TPRICE_amount',
					$totalPrice['TPRICE_amount'],
					null,
					$totalPrice['TPRICE_currency']
				));

				if($item->totalCurrency && $item->totalCurrency!=$thisPrice->totalCurrency){
					throw new \App\Amadeus\StructureParser\StructureException("We were expecting the same currencies");
				}

				$item->add($thisPrice);
			}

			$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
				'total',
				'total/transactionsCount',
				$item->total/$this->transaction->getProcessor()->getTransactionsCount(),
				null,
				$item->totalCurrency
			));

			$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
				'supplier',
				'total',
				$item->total,
				null,
				$item->totalCurrency
			));

			// extract serviceFee
			if (in_array(\App\Amadeus\Config::getInstance()->get("importServiceFeeForService"), ['all', ])) {
				$item = $this->internal_extractServiceFee($parsedStructure, $index, $item, [], [$index->TSA], ['ignore_TFD_line'=>true]);
			}

			// extract commission
			if (in_array(\App\Amadeus\Config::getInstance()->get("importCommissionForService"), ['all',  ])) {
				$item = $this->internal_extractCommission($item, $parsedStructure, $index, []);
			}

			$item = $this->internal_extractVAT($parsedStructure, $index, $item, null, []);

			$item = $this->sanitizePrice($item);

			return $item;
		}

		return null;
	}

	/**
	 * Get the price from the U line
	 * @return \App\Amadeus\StructureParser\Types\Price|NULL
	 */
	protected function passenger_IVD() {
		$parsedStructure = 	$this->transaction->getParsedStructure();
		$index = 			$this->transaction->getIndex();

		$IVD = $parsedStructure['IVD'];
		if($IVD['TOTPAMNT']){
			$item = new \App\Amadeus\StructureParser\Types\Price();

			$I = $parsedStructure['I'][ $parsedStructure['assoc:IVD:segment-pax'][$index->segment][0]];
			if (!$item->formOfPayment->hasData()) {
				$item = $this->internal_getFormOfPaymentFromRIU($parsedStructure, $index, $item);
			}
			if (!$item->formOfPayment->hasData()) {
				$item = $this->internal_getFormOfPayment($parsedStructure, $index, $item, $I);
			}

			// extract supplier price
			$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
				'total',
				'IVD:TOTPAMNT',
				$IVD['TOTPAMNT'],
				null,
				$IVD['TOTPCURR']
			));

			// calculate the supplier price
			$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
				'supplier',
				'total',
				$item->total,
				null,
				$item->totalCurrency
			));

			// extract commission
			if ($IVD['COMAMNT']) {
				$item->componentAdd(new \App\Amadeus\StructureParser\Types\PriceComponent(
					'commission',
					'IVD:COMAMNT',
					(float)$IVD['COMAMNT'],
					null,
					$IVD['COMCURR']
				));
				$item->commissionType = 	'value';
			}

			if ($item->commissionCurrency && $item->commissionCurrency!=$item->supplierCurrency) {
				throw new \App\Amadeus\StructureParser\StructureException("We were expecting that commission currency and supplier currency match");
			}

			if ($item->commission) {
				$item->componentAdd(new \App\Amadeus\StructureParser\Types\PriceComponent(
					'supplier',
					'commission',
					-1*$item->commission,
					null,
					$item->commissionCurrency
				));
			}

			// extract serviceFee
			if (in_array(\App\Amadeus\Config::getInstance()->get("importServiceFeeForService"), ['all', ])) {
				$item = $this->internal_extractServiceFee($parsedStructure, $index, $item, $I, [$index->TSA], ['ignore_TFD_line'=>true]);
			}

			// extract commission
			if (in_array(\App\Amadeus\Config::getInstance()->get("importCommissionForService"), ['all',  ])) {
				$item = $this->internal_extractCommission($item, $parsedStructure, $index, $I);
			}

			$item = $this->internal_extractVAT($parsedStructure, $index, $item, null, []);

			$item = $this->sanitizePrice($item);

			return $item;
		}

		return null;
	}

}
