<?php
namespace App\Amadeus\StructureParser\Parser;

/**
 * TODO: we should mark somehow open segments, so TINA would know about them
 */


/**
 * Base class, used by individual transaction classes
 * @author Lucian
 */
abstract class Itinerary
{
	/**
	 * The calling parent class
	 * @var \App\Amadeus\Storage\Operation
	 */
	protected $transaction = null;

	/**
	 * The basic itinerary class
	 * @var \App\Amadeus\StructureParser\Types\Itinerary
	 */
	protected $itinerary = null;

	/**
	 * Private cache for call_itineraryAnalizer()
	 * @var array
	 */
	private $cached_tripData = null;

	public function __construct($transaction)
	{
		$this->transaction = 	$transaction;

		$this->itinerary = new \App\Amadeus\StructureParser\Types\Itinerary();

		$this->initialize();
	}

	abstract protected function updateInternalSegmentData();

	/**
	 * Initialize the object
	 */
	public function initialize()
	{
		$this->updateInternalSegmentData();
		$this->itinerary->setDeparture($this->getDeparture());
		$this->itinerary->setDestination($this->getDestination());
	}

	/**
	 * Cleanup any memory references
	 */
	public function destroy()
	{
		$this->transaction = 	null;
		$this->itinerary->destroy();
		$this->itinerary = null;
	}

	/**
	 * Check if this is a valid transaction
	 * @return boolean|default:true
	 */
	public function isValid()
	{
		return true;
	}

	/**
	 * Get the itinerary object
	 * @throws \Exception
	 */
	public function getItineraryObj()
	{
		return $this->itinerary;
	}


	/**
	 * Get the itinerary segments
	 * @return Array
	 */
	public function getSegments()
	{
		return $this->itinerary->getSegments();
	}


	/**
	 * Get a list of segment statuses
	 * @return Array
	 */
	public function getSegmentsStatuses()
	{
		$ret = [];
		foreach($this->itinerary->getSegments() as $segment) {
			$ret[] = $segment->segmentStatus;
		}

		return $ret;
	}

	/**
	 * Get the departure location and time
	 * @return \App\Amadeus\StructureParser\Types\ItineraryLocation
	 */
	public function getDeparture()
	{
		// return $this->itinerary->getDeparture();
		return $this->internal_getDeparture();
	}


	/**
	 * Get the departure location and time
	 * @return \App\Amadeus\StructureParser\Types\ItineraryLocation
	 */
	public function getDestination()
	{
		// return $this->itinerary->getDestination();
		return $this->internal_getDestination();
	}


	/**
	 * Get the trip type (OW, RT, SJ, DJ)
	 * @return string
	 */
	public function getTripType()
	{
		return $this->itinerary->getTripType();
	}

	/**
	 * Return the departure data
	 * @param string $segments
	 * @return NULL
	 */
	protected function internal_getDeparture($segments = null)
	{
		if($segments===null){
			$segments = $this->getSegments();
		}
		$tripData = $this->itinerary->getItineraryAnalizer();

		if(count($segments)==1){
			return $segments[0]->getDeparture();
		}

		if(!$tripData->getType() && $segments[0]) {
			return $segments[0]->getDeparture();
		}
		elseif($tripData->getDeparture()) {
			return $segments[$tripData->getDepartureIndex()->index]->getDeparture();
		}

		return null;
	}


	/**
	 * Return destination data
	 * @param string $segments
	 */
	protected function internal_getDestination($segments = null)
	{
		if($segments===null){
			$segments = $this->getSegments();
		}

		if(count($segments)==1){
			return $segments[0]->getDestination();
		}

		$tripData = $this->itinerary->getItineraryAnalizer();

		$keys = array_keys($segments);
		$lastSegmentIdx = end($keys);

		$ret = null;
		if ($segments) {
			if (!$tripData->getType()) {
				$ret =  $segments[0]->getDestination();
			}
			elseif ($segments[$tripData->getArrivalIndex()->index]) { // RT, SJ, DJ
				if ($tripData->getArrivalIndex()->node =='departure') {
					$ret =  $segments[$tripData->getArrivalIndex()->index]->getDeparture();
				}
				else {
					$ret =  $segments[$tripData->getArrivalIndex()->index]->getDestination();
				}
			}

			if ($ret) {
				$ret->date = $segments[$lastSegmentIdx]->destination->date;
				$ret->time = $segments[$lastSegmentIdx]->destination->time;

				return $ret;
			}
		}

		return null;
	}
}
