<?php

namespace Modules\Utils\GdsProfileFill;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

		// load the configurations for this form
		$form = \Engine\Form\General::create();
		$form->setStyle('width', '300');

		// add corporate fields
		$this->addCorporateFields($form);

		// ad customer fields
		$this->addCustomerFields($form);

		// hidden fields
		$form->addElement( new \Engine\Form\Input\Hidden(null, 'idOrder', $this->values['idOrder']) );
		$form->addElement( new \Engine\Form\Input\Hidden(null, 'idCorporate', $this->values['idCorporate']) );
		$form->addElement( new \Engine\Form\Input\Hidden(null, 'idCustomer', $this->values['idCustomer']) );
		$form->addElement( new \Engine\Form\Input\Hidden(null, 'remarks', $this->values['remarks']) );
		$form->addElement( new \Engine\Form\Input\Hidden(null, 'remarksFieldId', $this->values['remarksFieldId']) );
		$form->addElement( new \Engine\Form\Input\Hidden(null, $this->submitButtonName, 1) );

		return $form;
	}


	private function addCorporateFields(\Engine\Form\General $form)
	{

		// if no corporate - stop
		if (!$this->values['idCorporate']) {
			return ;
		}

		// if no GDS profile for this corporate - stop
		$corporateEnt = \Entity\Corporate::select($this->values['idCorporate']);
		if (!$corporateEnt->recordExists() or !$corporateEnt->idGdsProfileTemplate) {
			return ;
		}

		$profileEnt = $corporateEnt->gdsProfileTemplate;

		// ADD DYNAMIC FIELDS /////////////////////////////////////////////////////////////////////
		if ($profileEnt->dynamicFieldsDetails) {
			$corpDynamicFieldsCont = \Engine\Form\Container\Fieldset::create($this->lang['index']['form']['grp_corpDin'].' - '.$corporateEnt->alias);
			$form->addContainer($corpDynamicFieldsCont);
			foreach ($profileEnt->dynamicFieldsDetails as $idField=>$fieldDet) {
				$corpDynamicFieldsCont->addElement( $this->getFormField($idField, $fieldDet, $corporateEnt->gdsProfileFieldsPossibleValues[$idField]?:[]) );
			}
		}

		// ADD STATIC FIELDS //////////////////////////////////////////////////////////////////////
		if ($profileEnt->staticFieldsDetails) {
			$corpStaticFieldsCont = \Engine\Form\Container\Fieldset::create($this->lang['index']['form']['grp_corpStt'].' - '.$corporateEnt->alias);
			$form->addContainer($corpStaticFieldsCont);
			$corpStaticFieldsCont->addElement( $this->getStaticFields($profileEnt->staticFieldsDetails, $corporateEnt->gdsProfileFieldsValues) );
		}

	}


	private function addCustomerFields(\Engine\Form\General $form)
	{

		// if no corporate - stop
		if (!$this->values['idCustomer']) {
			return ;
		}

		// if no GDS profile for this corporate - stop
		$customerEnt = \Entity\Customer::select($this->values['idCustomer']);
		if (!$customerEnt->recordExists() or !$customerEnt->idGdsProfileTemplate) {
			return ;
		}

		$profileEnt = $customerEnt->gdsProfileTemplate;

		// ADD DYNAMIC FIELDS /////////////////////////////////////////////////////////////////////
		if ($profileEnt->dynamicFieldsDetails) {
			$custDynamicFieldsCont = \Engine\Form\Container\Fieldset::create($this->lang['index']['form']['grp_custDin'].' - '.$customerEnt->name);
			$form->addContainer($custDynamicFieldsCont);
			foreach ($profileEnt->dynamicFieldsDetails as $idField=>$fieldDet) {
				$custDynamicFieldsCont->addElement( $this->getFormField($idField, $fieldDet, $customerEnt->gdsProfileFieldsPossibleValues[$idField]?:[]) );
			}
		}

		// ADD STATIC FIELDS //////////////////////////////////////////////////////////////////////
		if ($profileEnt->staticFieldsDetails) {
			$custStaticFieldsCont = \Engine\Form\Container\Fieldset::create($this->lang['index']['form']['grp_custStt'].' - '.$customerEnt->name);
			$form->addContainer($custStaticFieldsCont);
			$custStaticFieldsCont->addElement( $this->getStaticFields($profileEnt->staticFieldsDetails, $customerEnt->gdsProfileFieldsValues) );
		}

	}

	/**
	 * Adds a new dynamic field based on profile configs.
	 * @param int $idField
	 * @param array $fieldDetails
	 * @param array $possibleValues
	 * @throws \Exception
	 * @return \Engine\Form\Input\Select
	 */
	private function getFormField(int $idField, array $fieldDetails, array $possibleValues=[]) :\Engine\Form\Input\ElementAbstract
	{

		switch ($fieldDetails['inputType']) {
			case 'text':
				$inputField = new \Engine\Form\Input\Text();
			break;
			case 'textarea':
				$inputField = new \Engine\Form\Input\Textarea();
			break;
			case 'radio':
				$inputField = new \Engine\Form\Input\Radio();
				$values = [''=>$this->lang['index']['form']['na']] + $possibleValues;
				$inputField->setPossibleValues($values);
			break;
			case 'select':
				$inputField = new \Engine\Form\Input\Select();
				$inputField->setPossibleValues($possibleValues);
				$inputField->showDefaultOption();
			break;
			default :
				throw new \Exception("Invalid field type `{$fieldDetails['inputType']}` for GDS profile.");
		}

		$inputField->setLabel($fieldDetails['label']);
		if ($fieldDetails['remarkVar']) {
			$inputField->setName("profileFields[{$idField}]");
			$inputField->setValue($this->values['profileFields'][$idField]);
			$inputField->setDetails(sprintf($this->lang['index']['form']['rmVar'], $fieldDetails['remarkVar']));
		} else {
			$inputField->setDisabled();
			$inputField->setDetails($this->lang['index']['form']['noRm']);
		}
		$inputField->setMandatory($fieldDetails['mandatory']);
		if ($fieldDetails['description']) {
			$inputField->setTooltip($fieldDetails['description']);
		}
		if ($fieldDetails['validator']) {
			$inputField->setValidator($fieldDetails['validator']);
		}

		return $inputField;
	}

	/**
	 * Returns a new input label indicating the details of the static fields values.
	 * @param array $staticFieldsDetails
	 * @param array $profileFieldsValues
	 * @return \Engine\Form\Input\Label
	 */
	private function getStaticFields(array $staticFieldsDetails, array $profileFieldsValues) :\Engine\Form\Input\Label
	{

		$fieldsDescriptions = [];
		foreach ($staticFieldsDetails as $idField=>$fieldDet) {
			$description = $fieldDet['label'].': ';
			if ($profileFieldsValues[$idField]) {
				$description .= '<b>'.$profileFieldsValues[$idField].'</b>';
			} else {
				$description .= '['.$this->lang['index']['form']['noDefVal'].']';
			}
			if ($fieldDet['remarkVar']) {
				$description .= ' ['.sprintf($this->lang['index']['form']['rmVar'], $fieldDet['remarkVar']).']';
			} else {
				$description .= ' ['.$this->lang['index']['form']['noRmVar'].']';
			}
			if (!key_exists($idField, $profileFieldsValues) or !strlen($profileFieldsValues[$idField]) or !$fieldDet['remarkVar']) {
				$description .= ' - <b>'.$this->lang['index']['form']['skipped'].'</b>';
			}
			$fieldsDescriptions[] = $description;
		}

		$staticFieldsInput = new \Engine\Form\Input\Label('Static fields values');
		$staticFieldsInput->setTooltip($this->lang['index']['form']['staticFldTooltip']);
		$staticFieldsInput->setDetails(implode('<br />', $fieldsDescriptions));
		return $staticFieldsInput;
	}


}
