<?php

namespace App\ImportedOrder;

/**
 * The class stores info about the passenger title (Mr, Mrs, Ms, etc).
 * @author Gabriel
 */
class Passenger_Title extends EntityAbstract
{

	/**
	 * The passenger title code in TINA.
	 * @var String
	 */
	public $codeTina;

	/**
	 * The passenger title in the external system.
	 * @var String
	 */
	public $codeSystem;

	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		// check tina code
		if ($this->codeTina and !in_array($this->codeTina, $CFG['generalData']['personTitles'])) {
			$messagesCollector->addInfo('The passenger title TINA code is not valid');
		}
	}


	/**
	 * Gets the TINA matching passenger title.
	 * @param Int $idExternalSystem
	 * @return String - the matching TINA psg title code or blank if nothing was matched
	 */
	public function getMatchedId($idExternalSystem)
	{
		// get global cfgs
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// collect matched codes for psg type
		$matchedCodes = [];

		// check tina code
		if ($this->codeTina and in_array($this->codeTina, $CFG['generalData']['personTitles'])) {
			$matchedCodes[$this->codeTina] = true;
		}

		// check matched passenger type
		if ($this->codeSystem and $idExternalSystem) {
			$matchedTitle = \App\Config\ImportedOrders_Matches::getInstance()->getCustomerTitle($idExternalSystem, $this->codeSystem);
			if ($matchedTitle) {
				$matchedCodes[$matchedTitle] = true;
			}
		}

		// return the value if we have exactly one record
		if (count($matchedCodes)==1) {
			return key($matchedCodes);
		} else {
			return false;
		}

	}

}