<?php
namespace App\Form\Input;

class Address extends \Engine\Form\Input\ElementAbstract
{
	protected $class = "address";

	protected $internalElements = [
		'streetPrefix' => 		NULL,
		'streetName' => 		NULL,
		'streetNumber' => 		NULL,
		'buildingNumber' => 	NULL,
		'buildingEntrance' => 	NULL,
		'floor' => 				NULL,
		'apartment' => 			NULL,
		'countryCity' => 		NULL,
		'district' =>			NULL,
		'zipCode' => 			NULL,
	];

	public function __construct($label=NULL, $name=NULL, $value=[], $possibleValues=[])
	{
		parent::__construct($label, NULL, NULL);

		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		$streetPrefixPossibleValues = [
			'alley' => 			$_LANG['addressDetails']['streetPrefixPossibleValues']['alley'],
			'avenue' => 		$_LANG['addressDetails']['streetPrefixPossibleValues']['avenue'],
			'route' =>  		$_LANG['addressDetails']['streetPrefixPossibleValues']['route'],
			'square' => 		$_LANG['addressDetails']['streetPrefixPossibleValues']['square'],
			'road' => 			$_LANG['addressDetails']['streetPrefixPossibleValues']['road'],
			'embankment' => 	$_LANG['addressDetails']['streetPrefixPossibleValues']['embankment'],
			'street'	=>		$_LANG['addressDetails']['streetPrefixPossibleValues']['street'],
			'area'	=> 			$_LANG['addressDetails']['streetPrefixPossibleValues']['area'],
		];


		// setup the internal date objects
		$this->internalElements["streetPrefix"] = new \Engine\Form\Input\Select('', "{$name}[streetPrefix]", NULL, $streetPrefixPossibleValues);
		$this->internalElements["streetPrefix"]->setWidth(66.5)->showDefaultOption();

		$this->internalElements['streetName'] =	new \Engine\Form\Input\Text('', "{$name}[streetName]", NULL);

		$this->internalElements['streetNumber'] = new \Engine\Form\Input\Text($_LANG['addressDetails']['streetNumber'], "{$name}[streetNumber]", NULL);
		$this->internalElements['streetNumber']->setWidth(22);

		$this->internalElements['buildingNumber'] = new \Engine\Form\Input\Text($_LANG['addressDetails']['buildingNumber'], "{$name}[buildingNumber]", NULL);
		$this->internalElements['buildingNumber']->setWidth(22);

		$this->internalElements['buildingEntrance'] = new \Engine\Form\Input\Text($_LANG['addressDetails']['buildingEntrance'], "{$name}[buildingEntrance]", NULL);
		$this->internalElements['buildingEntrance']->setWidth(22);

		$this->internalElements['floor'] = new \Engine\Form\Input\Text($_LANG['addressDetails']['floor'], "{$name}[floor]", NULL);
		$this->internalElements['floor']->setWidth(22);

		$this->internalElements['apartment'] = new \Engine\Form\Input\Text($_LANG['addressDetails']['apartment'], "{$name}[apartment]", NULL);
		$this->internalElements['apartment']->setWidth(22);

		$this->internalElements['countryCity'] = new \App\Form\Input\Autocomplete_CountryCity($_LANG['addressDetails']['countryCity'], "{$name}[countryCity]", NULL);
		$this->internalElements['countryCity']->setWidth(113);
		$this->internalElements['countryCity']->setStyle('simple', true);

		$this->internalElements['district'] = new \Engine\Form\Input\Text($_LANG['addressDetails']['district'], "{$name}[district]", NULL);
		$this->internalElements['district']->setWidth(40);

		$this->internalElements["zipCode"] = new \Engine\Form\Input\Text($_LANG['addressDetails']['zipCode'], "{$name}[zipCode]", NULL);
		$this->internalElements["zipCode"]->setWidth(60);

		$this->name = $name;
	}

	/**
	 * Set the element name
	 * @param String $name
	 */
	public function setName($name)
	{
		$this->name = $name;

		foreach ($this->internalElements as $element => $input) {
			$this->internalElements[$element]->setName("{$name}[{$element}]");
		}
		return $this;
	}


	/**
	 * Set value for each input(non-PHPdoc)
	 * @see Engine\Form\Input.ElementAbstract::setValue()
	 */
	public function setValue($value)
	{
		if ($value) {
			foreach ($value as $element => $inputValue) {
				$this->internalElements[$element]->setValue($inputValue);
			}
		}
		return $this;
	}

	/**
	 * Get value of elements
	 * @return Array
	 */
	public function getValue()
	{
		$value = [];
		foreach ($this->internalElements as $element => $input) {
			$value[$element] = $input->getValue();
		}

		return $value;
	}

	public function setDisabled($disabled=true)
	{
		$this->disabled = true;
		foreach ($this->internalElements as $element => $input) {
			$input->setDisabled($disabled);
		}

		return $this;
	}

	/**
	 * Check if this element is disabled
	 * @return Bool
	 */
	public function getDisabled()
	{
		return $this->disabled;
	}

	/**
	 * Disable element rendering routines (compatibility with the old formRow 'invisibile' tag name)
	 * @param Bool $disabled
	 */
	public function setInvisible($dontRender=true)
	{
		$this->dontRender = $dontRender;
		foreach ($this->internalElements as $element => $input) {
			$input->setInvisible($dontRender);
		}

		return $this;
	}

	/**
	 * Detect whenever the input should be rendered or not in the interface (compatibility with the old formRow 'invisibile' tag name)
	 * @return Bool
	 */
	public function getInvisible()
	{
		return $this->dontRender;
	}

	public function getInput($key)
	{
		return $this->internalElements[$key];
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\ElementAbstract::getTemplateParams()
	 */
	protected function getTemplateParams($templateClass = null, $parentElement = null){
		$params = parent::getTemplateParams($templateClass, $parentElement );
		$this->internalElements['streetName']->setWidth($params['style']['width'] - 66.5 - 80);
		$params['internalElements'] = $this->internalElements;
		return $params;
	}

}