<?php
namespace App\Sabre\StructureParser\Structures\Hotel;

class CancellationPolicy extends \App\Sabre\StructureParser\Structures\Base\CancellationPolicy
{
	protected function _getData()
	{
		list(, , $transactionIndexes, ) = $this->getTransactionDetails();

		if (in_array($transactionIndexes['source'], ['M3Hhl', 'M3Oths:HHL, M5->M3, M5->M1', 'M3Oths:HHL, M5->M3', ])) {
			return $this->_getData_M3Hhl();
		}
		elseif (in_array($transactionIndexes['source'], ['M3Oths:HHT', 'M3Oths:HHT, M5->M3', 'M3Oths:HHT, M5->M3, M5->M1',])) {
			return $this->_getData_M3Oths_HHL();
		}
		elseif (in_array($transactionIndexes['source'], ['M3Oths:HHX', 'M3Oths:HHX, M5->M3', 'M3Oths:HHX, M5->M3, M5->M1', ])) {
			return $this->_getData_M3Oths_HHX();
		}
		else {
			throw new \Exception(sprintf("Don't know how to interpret source '%s'", $transactionIndexes['source']));
		}
	}

	private function _getData_M3Hhl()
	{
		list($parsedStructure, , $transactionIndexes, ) = $this->getTransactionDetails();

		$item = new \App\Sabre\StructureParser\Types\CancellationPolicy();

		$M3 = $parsedStructure['M3'][$transactionIndexes['km3']];

		if ($M3['M3HhlrVar']['hotelCancellationPolicy']) {
			$component = new \App\Sabre\StructureParser\Types\CancellationPolicyComponent();
			$component->text = $M3['M3HhlrVar']['hotelCancellationPolicy'];

			$item->append($component);
		}

		return $item;
	}

	private function _getData_M3Oths_HHL()
	{
		$item = new \App\Sabre\StructureParser\Types\CancellationPolicy();
		return $item;
	}

	private function _getData_M3Oths_HHX()
	{
		list($parsedStructure, , $transactionIndexes, ) = $this->getTransactionDetails();

		$item = new \App\Sabre\StructureParser\Types\CancellationPolicy();

		$M3 = $parsedStructure['M3'][$transactionIndexes['km3']];

		if ($M3['M3Oths']['IU3VR4_']['CPC']) {
			$component = new \App\Sabre\StructureParser\Types\CancellationPolicyComponent();
			$component->text = $M3['M3Oths']['IU3VR4_']['CPC'];

			$item->append($component);
		}

		if ($M3['M3Oths']['IU3VR4_']['CP01']) {
			$component = new \App\Sabre\StructureParser\Types\CancellationPolicyComponent();
			$component->text = $M3['M3Oths']['IU3VR4_']['CP01'];
			$item->append($component);
		}

		if ($M3['M3Oths']['IU3VR4_']['CP02']) {
			$component = new \App\Sabre\StructureParser\Types\CancellationPolicyComponent();
			$component->text = $M3['M3Oths']['IU3VR4_']['CP02'];
			$item->append($component);
		}

		if ($M3['M3Oths']['IU3VR4_']['CP03']) {
			$component = new \App\Sabre\StructureParser\Types\CancellationPolicyComponent();
			$component->text = $M3['M3Oths']['IU3VR4_']['CP03'];
			$item->append($component);
		}

		if ($M3['M3Oths']['IU3VR4_']['CP04']) {
			$component = new \App\Sabre\StructureParser\Types\CancellationPolicyComponent();
			$component->text = $M3['M3Oths']['IU3VR4_']['CP04'];
			$item->append($component);
		}

		return $item;
	}
}
