<?php

namespace Entity;

/**
 * The bank office management class
 * @author oana
 */
class BankOffice 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


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

	/**
	 * (non-PHPdoc)
	 * @see App\Entity.SelectAbstract::getAttribute()
	 */
	protected function getAttribute($var)
	{
		// CHECK EVERY POSSIBLE VALUES THAT $var MAY HAVE -----------------------------------------
		switch ($var) {

			// bank
			case 'bank': {
				return Bank::select($this->fields['idBank'], $this->selectForUpdate);
			}

			// nameStandard
			case 'nameStandard': {
				if (!isset($this->fields['nameStandard'])) {
					if ($this->fields['nameAlphabet'] == 'local'){
						$this->fields['nameStandard']=$this->fields['name2'];
					}
					else {
						$this->fields['nameStandard']=$this->fields['name'];
					}
				}
				return $this->fields['nameStandard'];
			}

			// nameLocal
			case 'nameLocal': {
				if (!isset($this->fields['nameLocal'])) {
					if ($this->fields['nameAlphabet']=='local') $this->fields['nameLocal']=$this->fields['name'];
					else $this->fields['nameLocal']=$this->fields['name2'];
				}
				return $this->fields['nameLocal'];
			}

			// addressStandard
			case 'addressStandard': {
				if (!isset($this->fields['addressStandard'])) {
					if ($this->fields['addressAlphabet']=='local') $this->fields['addressStandard']=$this->fields['address2'];
					else $this->fields['addressStandard']=$this->fields['address'];
				}
				return $this->fields['addressStandard'];
			}

			// addressLocal
			case 'addressLocal': {
				if (!isset($this->fields['addressLocal'])) {
					if ($this->fields['addressAlphabet']=='local') $this->fields['addressLocal']=$this->fields['address'];
					else $this->fields['addressLocal']=$this->fields['address2'];
				}
				return $this->fields['addressLocal'];
			}

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

			// updateUser
			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) {

			// simple fields
			case 'nameStandard':
			case 'nameLocal':
			case 'mfo':
			case 'swiftCode':
			case 'code':
			case 'addressStandard':
			case 'addressLocal': {
				$this->fields[$var]=$value;
				break;
			}

			// allow idBank only for insert operations
			case 'idBank': {
				if ($this->operation=='update') throw new \Exception('You cannot change the idBank of a bank office.');
				$this->fields[$var]=$value;
				break;
			}

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

	/**
	 * (non-PHPdoc)
	 * @see App\Entity.GeneralAbstract::save_preCheck()
	 */
	protected function save_preCheck()
	{
		// prepare alternative names for checking
		if ($this->operation == 'insert' or $this->operation == 'update') {
			if (array_key_exists('nameStandard', $this->fields) or array_key_exists('nameLocal', $this->fields) ) {
				$this->fields['name'] = max($this->fields['nameStandard'], $this->fields['nameLocal']);
			}

			if (array_key_exists('addressStandard', $this->fields) or array_key_exists('addressLocal', $this->fields)) {
				$this->fields['address'] = max($this->fields['addressStandard'], $this->fields['addressLocal']);
			}
		}
	}

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

		// POPULATE THE FIELDS: `name`, `name2`, `nameAlphabet` -----------------------------------
		if (array_key_exists('nameStandard', $saveData) or array_key_exists('nameLocal', $saveData)) {
			if ($saveData['nameStandard']) {
				$saveData['name'] =			$saveData['nameStandard'];
				$saveData['name2'] =		(String)$saveData['nameLocal'];
				$saveData['nameAlphabet'] =	'standard';
			} else {
				$saveData['name'] =			(String)$saveData['nameLocal'];
				$saveData['name2'] =		(String)$saveData['nameStandard'];
				$saveData['nameAlphabet'] =	'local';
			}
			unset($saveData['nameStandard'], $saveData['nameLocal']);
		}

		// POPULATE THE FIELDS: `address`, `address2`, `addressAlphabet` --------------------------
		if (array_key_exists('addressStandard', $saveData) or array_key_exists('addressLocal', $saveData)) {
			if ($saveData['addressStandard']) {
				$saveData['address'] =			$saveData['addressStandard'];
				$saveData['address2'] =		(String)$saveData['addressLocal'];
				$saveData['addressAlphabet'] =	'standard';
			} else {
				$saveData['address'] =			(String)$saveData['addressLocal'];
				$saveData['address2'] =		(String)$saveData['addressStandard'];
				$saveData['addressAlphabet'] =	'local';
			}
			unset($saveData['addressStandard'], $saveData['addressLocal']);
		}

		return $saveData;

	}
}