<?php

namespace App\EntityMatch;

/**
 * The class is used for matching cities.
 * @author Gabriel
 */
class City extends \App\EntityMatch\BaseAbstract
{

	/**
	 * The export fields for countries.
	 * @var array
	 */
	protected $exportFields = ['name', 'code3', 'countryCode'];

	/**
	 * Extra filters - deleted countries are allwed.
	 * @var String
	 */
	protected $matchingFilteringSQL = ' AND deleted=0';

	/**
	 * The class name which will instantiate the Entity - used for primaryKey search.
	 * @var String
	 */
	protected $entityClassName = '\Entity\City';

	/**
	 * The country code for the matching airports.
	 * @var String
	 */
	private $countryCodeFilter;


	/**
	 * (non-PHPdoc)
	 * @see \App\EntityMatch\BaseAbstract::setTableName()
	 */
	protected function setTableName()
	{
		global $DBTCities;
		$this->tableName = $DBTCities;
	}


	/**
	 * Sets a search by city name.
	 * @param String $value - the city name.
	 * @param Boolean $useWildcard - uses wildcard for this search or not.
	 */
	public function setName($value, $useWildcard=true)
	{

		global $CFG;
		// if the city name can be duplicated
		if ($CFG['useAlternativeNames']['cities'])
		{
			$this->setSearchCriterion('name', $value, $useWildcard, ['duplicateColumn'=>'name2']);
		}
		// if the city name can't be duplicated
		else
		{
			$this->setSearchCriterion('name', $value, $useWildcard);
		}

	}


	/**
	 * Sets a search by city code3.
	 * @param String $value - the city code3.
	 */
	public function setCode3($value)
	{
		$this->setSearchCriterion('code3', $value, false, ['duplicateColumn'=>'codeLocal']);
	}


	/**
	 * Sets a search by station code (airport/train station).
	 * @param String $value - the station code.
	 */
	public function setStationCode($value)
	{
		$this->setSearchCriterion('stationCode', $value, false);
	}


	/**
	 * Sets the filter by country code.
	 * @param String $value - the country code.
	 */
	public function filterByCountryCode($value)
	{
		$this->countryCodeFilter = $value;
	}



	/**
	 * This method does a special search for station code (for the rest of the criteria, it uses the parent call).
	 * @see \App\EntityMatch\BaseAbstract::searchRecords()
	 */
	protected function dispatchSearchRecords($field, $value, $useWildcard, Array $options=[])
	{

		switch ($field)
		{
			case 'stationCode':
				return $this->searchRecords_stationCode($value);
			break;

			default:
				return parent::dispatchSearchRecords($field, $value, $useWildcard, $options);

		}

	}

	/**
	 * Searches a city by the station code.
	 * @param String $value
	 */
	private function searchRecords_stationCode($value)
	{

		// get station details and check it
		$locObj = \Entity\Station::select($value);
		if (!$locObj->recordExists()) {
			return [];
		}

		// get the country by it's primary key
		if ($locObj->cityId) {
			return $this->searchRecord_primaryKey($locObj->cityId);
		}

		return [];

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\EntityMatch\BaseAbstract::cleanMatches()
	 */
	protected function cleanMatches()
	{

		// FILTER BULK MATCHES (by country code) --------------------------------------------------
		if ($this->countryCodeFilter) {
			foreach($this->bulkMatches as $idx=>$matchesList) {
				foreach($matchesList as $entCode=>$entDet) {
					if ($entDet['countryCode']!=$this->countryCodeFilter) {
						unset($this->bulkMatches[$idx][$entCode]);
					}
				}
			}

			// eliminate empty values
			$this->bulkMatches = array_filter($this->bulkMatches);

		}

	}


}

