<?php

namespace Modules\RemoteData\InfoBox;

class View_GalileoService extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{
	protected function getEntityRemoteDetails(\App\Entity\SelectAbstract $entity)
	{
		// shorthand notation
		$lang = $this->lang['amadeusService']['fields'];

		// add the header
		$output = \Engine\RemoteData\InfoBox\Container_Main::create( sprintf($lang['title'], $entity->docNumber))
			->setShortTitle( $entity->docNumber )
			->setId(sprintf("%s.%s", __CLASS__, $entity->getId()));

		// add basic info
		$c1 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['general']);
		$output->addContainer($c1);

		$c1->addField( \Engine\RemoteData\InfoBox\Field_String::create( $lang['serviceCode'], 	$entity->serviceCode ))
			->addField( \Engine\RemoteData\InfoBox\Field_String::create( $lang['getId'], 		$entity->getId() ))
			->addField( \Engine\RemoteData\InfoBox\Field_String::create( $lang['RECLOC'], 		$entity->transactionData->documents->RECLOC ))
			->addField( \Engine\RemoteData\InfoBox\Field_Date::create(	 $lang['docDate'], 		\Engine\DateTime::createFromIsoDate($entity->docDate) ))
			->addField( \Engine\RemoteData\InfoBox\Field_String::create( $lang['file'], 		\Engine\File::getName($entity->file->filePath ) ));


		// add service details
		$departureArr = [];
		if ($entity->transactionData->itinerary) {
			if ($entity->transactionData->itinerary->getDeparture()->airportCode) {
				$departureArr[] = $entity->transactionData->itinerary->getDeparture()->getAirport()->name;
			}
			if ($entity->transactionData->itinerary->getDeparture()->cityId) {
				$departureArr[] = $entity->transactionData->itinerary->getDeparture()->getCity()->name;
			}
			if ($entity->transactionData->itinerary->getDeparture()->countryCode) {
				$departureArr[] = $entity->transactionData->itinerary->getDeparture()->getCountry()->name;
			}
		}

		$destinationArr = [];
		if ($entity->transactionData->itinerary) {
			if ($entity->transactionData->itinerary->getDestination()->airportCode) {
				$destinationArr[] = $entity->transactionData->itinerary->getDestination()->getAirport()->name;
			}
			if ($entity->transactionData->itinerary->getDestination()->cityId) {
				$destinationArr[] = $entity->transactionData->itinerary->getDestination()->getCity()->name;
			}
			if ($entity->transactionData->itinerary->getDestination()->countryCode) {
				$destinationArr[] = $entity->transactionData->itinerary->getDestination()->getCountry()->name;
			}
		}

		$c2 = \Engine\RemoteData\InfoBox\Container_Simple::create($lang['service']);
		$output->addContainer($c2);
		$c2->addField(  \Engine\RemoteData\InfoBox\Field_String::create( $lang['passenger'], 	sprintf("%s %s", $entity->transactionData->passengers->passengers[0]->lastName, $entity->transactionData->passengers->passengers[0]->firstName ) ))
			->addField( \Engine\RemoteData\InfoBox\Field_String::create( $lang['departure'], 	implode(", ", $departureArr) ))
			->addField( \Engine\RemoteData\InfoBox\Field_String::create( $lang['destination'], 	implode(", ", $destinationArr) ));

		if ($entity->transactionData->itinerary) {
			if($entity->transactionData->itinerary->getDeparture()->date && $entity->transactionData->itinerary->getDestination()->date) {
				$c2->addField(  \Engine\RemoteData\InfoBox\Field_DateInterval::create( $lang['serviceInterval'], 	[
					'start' => 	\Engine\DateTime::createFromIsoDate($entity->transactionData->itinerary->getDeparture()->date),
					'end' => 	\Engine\DateTime::createFromIsoDate($entity->transactionData->itinerary->getDestination()->date),
					'showDifference'=> \Engine\RemoteData\InfoBox\Field_DateInterval::DIFF_NIGHTS_OR_HOURS,
				] ));
			}
			elseif($entity->transactionData->itinerary->getDeparture()->date) {
				if($entity->transactionData->itinerary->getDeparture()->time){
					$c2->addField(  \Engine\RemoteData\InfoBox\Field_DateTime::create( $lang['serviceDate'], 	\Engine\DateTime::createFromIsoDatetime($entity->transactionData->itinerary->getDeparture()->getDateTime()) ));
				}
				else {
					$c2->addField(  \Engine\RemoteData\InfoBox\Field_Date::create( $lang['serviceDate'], 	\Engine\DateTime::createFromIsoDate($entity->transactionData->itinerary->getDeparture()->date) ));
				}
			}
		}

		// add price data
		$c3 = \Engine\RemoteData\InfoBox\Container_Simple::create($lang['price']);
		$output->addContainer($c3);
		$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create( $lang['total'], [
			'amount' => $entity->transactionData->price->total,
			'currency'=>$entity->transactionData->price->totalCurrency,
		] ));

		$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create( $lang['supplier'], [
			'amount' => $entity->transactionData->price->supplier,
			'currency'=>$entity->transactionData->price->supplierCurrency,
		] ));

		$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create( $lang['tax'], [
			'amount' => ($entity->transactionData->price->tax?$entity->transactionData->price->tax->getAmount():0),
			'currency'=>($entity->transactionData->price->tax?$entity->transactionData->price->tax->getCurrency():''),
		] ));

		$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create( $lang['commission'], [
			'amount' => $entity->transactionData->price->commission,
			'currency'=>$entity->transactionData->price->commissionCurrency,
		] ));

		$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create( $lang['serviceFee'], [
			'amount' => $entity->transactionData->price->serviceFee,
			'currency'=>$entity->transactionData->price->serviceFeeCurrency,
		] ));

		$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create( $lang['discount'], [
			'amount' => $entity->transactionData->price->discount,
			'currency'=>$entity->transactionData->price->discountCurrency,
		] ));

		// add itinerary data
		$c4 = \Engine\RemoteData\InfoBox\Container_Simple::create(sprintf($lang['itinerary'], count((array)$entity->transactionData->itinerary->segments)));
		$c4->setHidden(true);
		$output->addContainer($c4);
		if($entity->transactionData->itinerary->segments){
			foreach($entity->transactionData->itinerary->segments as $idx=>$segment){
				$c5 = \Engine\RemoteData\InfoBox\Container_Simple::create(sprintf($lang['itinerarySegment'], $idx+1));
				$c4->addContainer($c5);

				$c5->setCollapsible(false);
				$c5->setType('section');

				// compose the departure tag
				$seg = $segment->departure;
				$components = [];
				if ($seg->getAirport()->recordExists()) {
					$components[] = $seg->getAirport()->name;
				}
				if ($seg->getCity()->recordExists()) {
					$components[] = $seg->getCity()->name;
				}
				if ($seg->getCountry()->recordExists()) {
					$components[] = $seg->getCountry()->name;
				}

				$c5->addField(  \Engine\RemoteData\InfoBox\Field_String::create( $lang['itinerarySegment_departure'], implode(", ", $components) ));

				//  compose the destination tag
				$seg = $segment->destination;
				$components = [];
				if ($seg->getAirport()->recordExists()) {
					$components[] = $seg->getAirport()->name;
				}
				if ($seg->getCity()->recordExists()) {
					$components[] = $seg->getCity()->name;
				}
				if ($seg->getCountry()->recordExists()) {
					$components[] = $seg->getCountry()->name;
				}

				$c5->addField(  \Engine\RemoteData\InfoBox\Field_String::create( $lang['itinerarySegment_destination'], implode(", ", $components) ));

				// add dates
				if($segment->destination->getDatetime()) {
					$c5->addField(  \Engine\RemoteData\InfoBox\Field_DateInterval::create( $lang['itinerarySegment_interval'], [
						'start'=> 	\Engine\DateTime::createFromIsoDatetime($segment->departure->getDatetime()),
						'end' => 	\Engine\DateTime::createFromIsoDatetime($segment->destination->getDatetime()),
						'showDifference'=> 'nights_or_hours',
					] ));
				}
				else {
					$c5->addField(  \Engine\RemoteData\InfoBox\Field_Date::create( $lang['itinerarySegment_interval'], \Engine\DateTime::createFromIsoDatetime($segment->departure->getDatetime())));
				}
			}
		}

		// add file list
		$c7 = \Engine\RemoteData\InfoBox\Container_Simple::create($lang['file']);
		$c7->setType('ol');
		$c7->setCollapsible(true);
		$c7->setHidden(true);
		$output->addContainer($c7);

		// get the "related" file list. put the current file on the top file list
		$files = $this->gatherFileList($entity, []);
		$files = array_reverse($files);

		foreach($files as $f) {
			$fld = \Engine\RemoteData\InfoBox\Field_Url::create( '',
				[
					'label' => \Engine\File::getName($f),
					'value' => \Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/Orders/Galileo/Collector', 'index', ['autoOpen'=>$f])
				]
			);

			if ($entity->file->relativeFilePath == $f) {
				$fld->setStyle('highlight');
			}

			$c7->addField($fld);
		}

		// add user data
		$c7 = \Engine\RemoteData\InfoBox\Container_Simple::create($lang['user']);
		$output->addContainer($c7);
		$c7->addField(  \Engine\RemoteData\InfoBox\Field_String::create( $lang['idTINAIssueUser'], $entity->transactionData->agent->issuingAgent->idSystemUser));
		$c7->addField(  \Engine\RemoteData\InfoBox\Field_String::create( $lang['idTINABookUser'], $entity->transactionData->agent->bookingAgent->idSystemUser));
		$c7->addField(  \Engine\RemoteData\InfoBox\Field_DateTime::create( $lang['addTime'], \Engine\DateTime::createFromIsoDateTime($entity->addTime) ));

		return $output;
	}

	private function gatherFileList($entity, $files)
	{
		foreach ($entity->PNRFiles as $file) {
			$files[] = $file->relativeFilePath;
		}
		return $files;
	}

}
