<?php

namespace App\ImportedOrder;

/**
 * The class stores info about the passenger type (adult, child, infant, etc).
 * @author Gabriel
 */
class Passenger_Type extends EntityAbstract
{

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

	/**
	 * The passenger type 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']['personTypes'])) {
			$messagesCollector->addInfo('The passenger type TINA code is not valid');
		}
	}


	/**
	 * Gets the TINA matching passenger type.
	 * @param Int $idExternalSystem
	 * @return String - the matching TINA psg type 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']['personTypes'])) {
			$matchedCodes[$this->codeTina] = true;
		}

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

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

	}

}