<?php

namespace App\ImportedOrder;

/**
 * The class contains info about a supplier product (hotel, a package, a certain flight, etc).
 * @author Gabriel
 */
class SupplierProduct extends EntityAbstract
{

	/**
	 * The supplier product id from TINA.
	 * @var Int
	 */
	public $idTina;

	/**
	 * The supplier product id from the external system.
	 * @var String
	 */
	public $idSystem;

	/**
	 * The supplier product service type.
	 * @var \App\ImportedOrder\ServiceType
	 */
	public ?\App\ImportedOrder\ServiceType $serviceType = null;

	/**
	 * The supplier product name.
	 * @var String
	 */
	public $name;

	/**
	 * The supplier product group name (eg. chain code)
	 * @var String
	 */
	public $groupName;

	/**
	 * The supplier product category code.
	 * @var String
	 */
	public $categoryCode;

	/**
	 * Stores the supplier product place.
	 * @var \App\ImportedOrder\Place
	 */
	public ?\App\ImportedOrder\Place $place = null;

	/**
	 * The supplier product address.
	 * @var String
	 */
	public $address;

	/**
	 * The supplier product telephone.
	 * @var String
	 */
	public $telephone;

	/**
	 * The supplier product fax.
	 * @var String
	 */
	public $fax;

	/**
	 * The supplier product email
	 * @var String
	 */
	public $email;

	/**
	 * An URL of an image representing the hotel.
	 * @var String
	 */
	public $imageUrl;

	/**
	 * Returns a list of internal class attributes that can be copied when merging orders.
	 * @return Array
	 */
	protected function getMergingFields() :array
	{
		return [ 'idTina', 'idSystem', 'serviceType', 'name', 'categoryCode', 'groupName', 'place', 'address', 'telephone', 'fax', 'email', 'imageUrl' ];
	}

	/**
	 * Gets the entity match object.
	 * @param String $idExternalSystem
	 * @return \App\ImportedOrder\EntityMatch\SupplierProduct
	 */
	public function getPopulatedMatchObject($idExternalSystem)
	{

		$matchObj = new \App\ImportedOrder\EntityMatch\SupplierProduct('intersect');
		if ($this->idTina) {
			$matchObj->setId($this->idTina);
		}
		if ($idExternalSystem and $this->idSystem) {
			$matchObj->setIdInExtSys($this->idSystem, $idExternalSystem);
		}
		if ($this->name) {
			$matchObj->setProductName($this->name, true);
		}
		// filter by service code
		if ($this->serviceType) {
			$matchedServiceType = $this->serviceType->getMatchedId($idExternalSystem);
			if ($matchedServiceType) {
				$matchObj->filterByServiceCode($matchedServiceType);
			}
		}
		// filter by city and country
		if ($this->place and $this->place->station and $this->place->station->city) {
			$cityObj = $this->place->station->city;
			$matchedCityId = $cityObj->getMatchedId($idExternalSystem);
			if ($matchedCityId) {
				$matchObj->filterByCityId($matchedCityId);
			}
			if ($cityObj->country) {
				$matchedCountryCode = $cityObj->country->getMatchedId($idExternalSystem);
				if ($matchedCountryCode) {
					$matchObj->filterByCountryCode($matchedCountryCode);
				}
			}
		}

		return $matchObj;

	}


	/**
	 * Matches the entity by its fields - can also check for matches in the external
	 * systems associations (if $idExternalSystem is set).
	 * @param Int $idExternalSystem - if set, it is attempted to match the user by the manual associations;
	 * @return Int - the entity id, if matched; 0/blank if no entity was matched
	 */
	public function getMatchedId($idExternalSystem)
	{

		$matchObj = $this->getPopulatedMatchObject($idExternalSystem);

		$matchObj->search();

		if ($matchObj->getResultsCount()==1) {
			return $matchObj->getFirstMatchId();
		}

		return 0;

	}

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

		// checks idTina
		if ( $this->idTina and (!is_numeric($this->idTina) or $this->idTina<0) ) {
			$messagesCollector->addInfo('The TINA id is invalid - it should be a positive integer.');
		}

		// append place validators
		if ($this->place) {
			$messagesCollector->appendMessages($this->place->getValidatingMessagesCollector(), 'Place: %s');
		}

		// append serviceType validators
		if ($this->serviceType) {
			$messagesCollector->appendMessages($this->serviceType->getValidatingMessagesCollector(), 'Service Type: %s');
		}

	}

}
