<?php

namespace Entity;

/**
 * The airports/stations management class
 */
class Station extends \App\Entity\GeneralAbstract
{

	/**
	 * Indicate if the operations available for the current entity must be done inside a transaction or not.
	 * @var Boolean
	 */
	protected $saveInsideTransaction = true;

	protected $addAndUpdateDateTimeFormat = 'timestamp'; // TODO: to be eliminated after the table columns are renamed and replaced with datetime

	protected $tablePrimaryKey='code';
	protected $primaryKeyType='string';

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::setTableName()
	 */
	protected function setTableName(): void
	{
		global $DBTStations;
		$this->tableName=$DBTStations;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::getAttribute()
	 */
	protected function getAttribute($var)
	{

		// CHECK EVERY POSSIBLE VALUES THAT $var MAY HAVE -----------------------------------------
		switch ($var) {

			// country
			case 'country': {
				return \Entity\Country::select($this->fields['countryCode'], $this->selectForUpdate);
			}

			// city
			case 'city': {
				return \Entity\City::select($this->fields['cityId'], $this->selectForUpdate);
			}

			// set the 'nameStandard'
			case 'nameStandard': {
				if (!$this->fields['nameStandard']) {
					$this->fields['nameStandard'] = $this->fields['name'];
				}

				return $this->fields['nameStandard'];
			}

			// the airport add user
			case 'addUser': {
				return \Entity\User::select($this->fields['idAddUser'], $this->selectForUpdate);
			}

			// the airport lastUpdate user
			case 'updateUser': {
				return \Entity\User::select($this->fields['idUpdateUser'], $this->selectForUpdate);
			}

			// the rest of the fields (most of them are just copied)
			default: {
				return parent::getAttribute($var);
			}

		}
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralAbstract::setAttribute()
	 */
	protected function setAttribute($var, $value)
	{

		// check that operation is okay
		if ($this->operation != 'insert' and $this->operation != 'update') {
			throw new \Exception('You can use __set magic method only with insert/update operation in `'.__CLASS__.'` class.');
		}

		// CHECK EACH POSSIBLE FIELD VALUE --------------------------------------------------------
		switch ($var) {

			// 'code' can be set only for 'insert' operation
			case 'code': {
				if ($this->operation == 'insert') {
					$this->fields[$var] = $value;
				}
				else {
					throw new \Exception('You can use __set magic method with `code` parameter only with insert operation in `'.__CLASS__.'` class.');
				}

				break;
			}

			// the airport type
			case 'type': {
				if ($this->operation == 'insert') {
					 $this->fields[$var] = $value;
				}
				else {
					throw new \Exception('You can use __set magic method with `type` parameter only with insert operation in `'.__CLASS__.'` class.');
				}

				break;
			}

			// simple fields
			case 'name': case 'nameLocal': case 'codeLocal': case 'cityId': case 'timezone': {
				$this->fields[$var] = $value;
				break;
			}

			// country
			case 'countryCode': {
				$this->fields[$var] = $value;
				if (!$this->fields['cityId']) {
					$this->fields['cityId'] = 0;
				}

				break;
			}

			// the field is not valid
			default: {
				throw new \Exception("The field '$var' is not valid for the `".__METHOD__."` class/method.");
			}

		}

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralAbstract::save_executeDelete()
	 */
	protected function save_executeDelete(Array $saveData)
	{

		// EXECUTE SQL ----------------------------------------------------------------------------
		$this->save_executeSql_delete($this->tableName, $this->tablePrimaryKey, $this->id);

		$this->messages->addSuccessCode('delete_ok');

	}
}

