<?php

namespace App\ImportedOrder;

class Itinerary extends EntityAbstract
{

	/**
	 * An array of itineray segments.
	 * @var \App\ImportedOrder\Itinerary_Segment[]
	 */
	public ?array $segments = null;

	/**
	 * Adds a new segment to the itinerary.
	 * @param \App\ImportedOrder\Itinerary_Segment $segment
	 */
	public function addSegment(Itinerary_Segment $segment)
	{
		if (!$this->segments) {
			$this->segments = [];
		}
		$this->segments[] = $segment;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{

		$cnt=0;
		if ($this->segments) {
			foreach ($this->segments as $segm) {
				$cnt++;
				$messagesCollector->appendMessages($segm->getValidatingMessagesCollector(), "Segment #{$cnt}: %s");
			}
		}

	}

	/**
	 * Mathes every element of each segment of the itinerary and returns a TINA itinerary object.
	 * @return \App\Itinerary
	 */
	public function getMatchedItinerary($idExternalSystem)
	{

		// if no segments - return null
		if (!$this->segments) {
			return null;
		}

		// create a new itinerary object
		$tinaItinerary = new \App\Itinerary();

		// loop through each itinerary
		foreach($this->segments as $importedSegment) {

			/* @var $importedSegment \App\ImportedOrder\Itinerary_Segment */

			// start a new itinerary segment
			$tinaSegment = new \App\ItinerarySegment();

			// departure place
			if ($importedSegment->departurePlace and $importedSegment->departurePlace->station) {
				$stationObj = $importedSegment->departurePlace->station;
				$matchedStationCode = $stationObj->getMatchedId($idExternalSystem);
				if ($matchedStationCode) { // station is matched
					$tinaSegment->setDeparturePlace(\Entity\Station::select($matchedStationCode)->city->countryCode, \Entity\Station::select($matchedStationCode)->cityId, $matchedStationCode);
				} else {
					if ($stationObj->city) {
						$cityObj = $stationObj->city;
						$matchedCityId = $cityObj->getMatchedId($idExternalSystem);
						if ($matchedCityId) { // city is matched
							$tinaSegment->setDeparturePlace(\Entity\Station::select($matchedCityId)->countryCode, $matchedCityId, '');
						} else {
							if ($cityObj->country) {
								$matchedCountryCode = $cityObj->country->getMatchedId($idExternalSystem);
								if ($matchedCountryCode) {
									$tinaSegment->setDeparturePlace($matchedCountryCode, '', '');
								}
							}
						}
					}
				}
			}

			// destination place
			if ($importedSegment->arrivalPlace and $importedSegment->arrivalPlace->station) {
				$stationObj = $importedSegment->arrivalPlace->station;
				$matchedStationCode = $stationObj->getMatchedId($idExternalSystem);
				if ($matchedStationCode) { // station is matched
					$tinaSegment->setArrivalPlace(\Entity\Station::select($matchedStationCode)->city->countryCode, \Entity\Station::select($matchedStationCode)->cityId, $matchedStationCode);
				} else {
					if ($stationObj->city) {
						$cityObj = $stationObj->city;
						$matchedCityId = $cityObj->getMatchedId($idExternalSystem);
						if ($matchedCityId) { // city is matched
							$tinaSegment->setArrivalPlace(\Entity\Station::select($matchedCityId)->countryCode, $matchedCityId, '');
						} else {
							if ($cityObj->country) {
								$matchedCountryCode = $cityObj->country->getMatchedId($idExternalSystem);
								if ($matchedCountryCode) {
									$tinaSegment->setArrivalPlace($matchedCountryCode, '', '');
								}
							}
						}
					}
				}
			}

			// departure / destination date and time
			if ($importedSegment->departureDateTime) {
				$tinaSegment->setDepartureDateTime($importedSegment->departureDateTime);
			}
			if ($importedSegment->arrivalDateTime) {
				$tinaSegment->setArrivalDateTime($importedSegment->arrivalDateTime);
			}

			// class code/name
			if ($importedSegment->classCode!==null or $importedSegment->className!==null) {
				$tinaSegment->setServiceClass((String)$importedSegment->classCode, (String)$importedSegment->className);
			}

			// set the distance
			if ($importedSegment->distance!==null) {
				$tinaSegment->setDistance($importedSegment->distance, $importedSegment->distanceUnit);
			}

			// set the duration
			if ($importedSegment->duration!==null) {
				$tinaSegment->setDuration($importedSegment->duration);
			}

			// set the transport number
			if ($importedSegment->transportNumber!==null) {
				$tinaSegment->setTransportNumber($importedSegment->transportNumber);
			}

			// set the transport unit
			if ($importedSegment->transportUnit!==null) {
				$tinaSegment->setTransportUnit($importedSegment->transportUnit);
			}

			// set the seat number
			if ($importedSegment->seatNumber!==null) {
				$tinaSegment->setSeatNumber($importedSegment->seatNumber);
			}

			// add supplier on this segment
			if ($importedSegment->supplier) {
				$idMatchedSupplier = $importedSegment->supplier->getMatchedId($idExternalSystem);
				if ($idMatchedSupplier) {
					$tinaSegment->setIdSupplier($idMatchedSupplier);
				}
			}

			// set the fare basis code
			if ($importedSegment->fareBasis!==null) {
				$tinaSegment->setFareBasis($importedSegment->fareBasis);
			}

			// stop-over allowed
			if ($importedSegment->stopOverAllowed!==null) {
				$tinaSegment->setStopOverAllowed($importedSegment->stopOverAllowed);
			}

			// set the baggage code
			if ($importedSegment->baggageCode!==null) {
				$tinaSegment->setBaggageCode($importedSegment->baggageCode);
			}

			// set the meal details
			if ($importedSegment->mealCode!==null) {
				$tinaSegment->setMealCode($importedSegment->mealCode);
			}
			// set the baggage code
			if ($importedSegment->mealDescription!==null) {
				$tinaSegment->setMealDescription($importedSegment->mealDescription);
			}

			// set the terminal check io/out
			if ($importedSegment->terminalCheckIn!==null) {
				$tinaSegment->setTerminalCheckIn($importedSegment->terminalCheckIn);
			}
			if ($importedSegment->terminalCheckOut!==null) {
				$tinaSegment->setTerminalCheckOut($importedSegment->terminalCheckOut);
			}

			// add the new segment
			$tinaItinerary->addSegment($tinaSegment);

		}

		return $tinaItinerary;

	}

}
