<?php
namespace App\Worldspan\StructureParser\Structures;

class Transaction extends \App\Utils\BaseParser\Structure\StructureParser\Structures\Transaction
{
	/**
	 * The tarnsaction document data
	 * @var \App\Worldspan\StructureParser\Types\Document
	 */
	public $documents = 	null;
	public $itinerary = 	null;
	public $passengers = 	null;
	public $price = 		null;
	public $supplier = 		null;

	/**
	 * The agent data
	 * @var \App\Worldspan\StructureParser\Types\Agent
	 */
	public $agent = 		null;

	/**
	 * The relations
	 * @var \App\Worldspan\StructureParser\Types\Relations
	 */
	public $relations = 	null;
	public $remarks = 		null; // no examples found
	public $extraData = 	null; // not available

	/**
	 * {@inheritDoc}
	 * @see \App\Utils\BaseParser\Structure\StructureParser\Structures\Transaction::getStorageObjInstance()
	 */
	public function getStorageObjInstance()
	{
		return \App\Worldspan\Storage\StorageHandler::getInstance();
	}

	public function getData()
	{
		switch ($this->transactionType) {
			case 'ticketing':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Documents");
				$this->passengers = $this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Passengers");
				$this->itinerary = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Itinerary");
				$this->supplier = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Supplier");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Agent");
				$this->remarks = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Remarks");
				$this->price = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Ticketing\Price");

				if ($this->price) {
					$this->price->checkIntegrity([], $this->messages, $this);
				}
			break;

			case 'reissue':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Documents");
				$this->passengers = $this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Passengers");
				$this->itinerary = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Itinerary");
				$this->supplier = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Supplier");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Agent");
				$this->remarks = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Remarks");
				$this->price = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Price");
				$this->relations = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Reissue\Relations");

				if ($this->price) {
					$this->price->checkIntegrity([], $this->messages, $this);
				}
			break;

			case 'EMD':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Documents");
				$this->passengers = $this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Passengers");
				$this->itinerary = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Itinerary");
				$this->supplier = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Supplier");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Agent");
				$this->remarks = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Remarks");
				$this->price = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Price");
				$this->extraData= 	$this->extractAs("\App\Worldspan\StructureParser\Structures\EMD\Extradata");

				if ($this->price) {
					$this->price->checkIntegrity([], $this->messages, $this);
				}
			break;

			case 'MCO':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Documents");
				$this->passengers = $this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Passengers");
				$this->itinerary = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Itinerary");
				$this->supplier = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Supplier");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Agent");
				$this->remarks = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Remarks");
				$this->price = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Price");
				$this->extraData= 	$this->extractAs("\App\Worldspan\StructureParser\Structures\MCO\Extradata");

				if ($this->price) {
					$this->price->checkIntegrity([], $this->messages, $this);
				}
			break;

			case 'void':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Void\Documents");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Void\Agent");
			break;

			case 'spoil':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Spoil\Documents");
			break;

			case 'refundRaw':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\RefundRaw\Documents");
				$this->passengers = $this->extractAs("\App\Worldspan\StructureParser\Structures\RefundRaw\Passengers");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\RefundRaw\Agent");
				$this->remarks = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\RefundRaw\Remarks");
				$this->price = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\RefundRaw\Price");

				if ($this->price) {
					$this->price->checkIntegrity(['negativeTotal'=>true, 'negativeSupplier'=>true], $this->messages, $this);
				}
			break;

			case 'refund':
				$this->documents = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Refund\Documents");
				$this->passengers = $this->extractAs("\App\Worldspan\StructureParser\Structures\Refund\Passengers");
				$this->agent = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Refund\Agent");
				$this->remarks = 	$this->extractAs("\App\Worldspan\StructureParser\Structures\Refund\Remarks");
				$this->price = 		$this->extractAs("\App\Worldspan\StructureParser\Structures\Refund\Price");

				if ($this->price) {
					$this->price->checkIntegrity(['negativeTotal'=>true, 'negativeSupplier'=>true], $this->messages, $this);
				}
			break;

			default:
				throw new \Exception("Invalid transaction type: {$this->transactionType}");
		}

		// post-processing
		$this->internal_extract_company($this->passengers, $this->remarks, $this->processor->getParsedStructure());


		return $this;
	}

	protected function internal_extract_company(?\App\Worldspan\StructureParser\Types\Passengers $passengers, ?\App\Worldspan\StructureParser\Types\Remarks $remarks, $parsedStructure)
	{
		if ($passengers) {
			foreach ($passengers->passengers as $pax) {
				if (!$pax->companyId && $remarks) {
					$processedRemarks = $remarks->getProcessedRemarks();
					if ($processedRemarks['corporateCode']) {
						$pax->companyId = $processedRemarks['corporateCode'];
					}
				}
			}

			$R2 = $parsedStructure['R2'];
			foreach ($passengers->passengers as $pax) {
				if (!$pax->companyId && $R2['ClientAccountNumber']) {
					$pax->companyId = $R2['ClientAccountNumber'];
				}
			}
		}
	}
}