<?php
namespace Modules\Admin\CurrencyExchange\HistoryRates;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$form = \Engine\Form\General::create();

		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['index']['currExchDate']);
		$form->addContainer($fieldset);

		$fld = \Engine\Form\Input\Date::create($this->lang['index']['currExchDate'], 'currencyExchDate', $this->values['currencyExchDate']);
		$fld->onChange("$('{$form->getId()}').submit();");

		$fieldset->addElement($fld);

		if ($this->values['currencyExchDate']) {

			$fieldsetCurrExch = \Engine\Form\Container\Fieldset::create($this->lang['index']['currExchRates']);
			$form->addContainer($fieldsetCurrExch);

			$allRates = \App\CurrencyExchange\Date::getInstance($this->values['currencyExchDate'])->getRates();

			$currencyRates = [];
			foreach ($allRates as $currencyTypeCode => $currencyDetails) {
				foreach ($currencyDetails as $currency => $currencyRate) {
					$currencyRates[$currency][$currencyTypeCode] = $currencyRate;
				}
			}

			$currencyTypes = \App\Config\CurrencyTypes::getInstance();
			$sortedCurrencyRates = [];
			$currencyTypesList = [];

			foreach(\App\Config\Currencies::getInstance()->getSimple() as $currencyCode) {
				foreach ($currencyTypes->getAll() as $currencyDetails) {

					$currencyTypesList[$currencyCode][$currencyDetails['code']] = $currencyDetails['name'];

					$sortedCurrencyRates[$currencyCode][$currencyDetails['code']] = $currencyRates[$currencyCode][$currencyDetails['code']] ?: 0;
				}
			}

			foreach(\App\Config\Currencies::getInstance()->getSimple() as $currencyCode) {
				if (\App\Config\Currencies::getInstance()->getDefaultCurrency() != $currencyCode) {
					$fld = new \App\Form\Input\MultipliedText($currencyCode, "currencyRates[".$currencyCode."]", $sortedCurrencyRates[$currencyCode]);
					$fld->setMultiplier(count($currencyTypesList[$currencyCode]));
					$fld->setMultiplierLabelKeys(array_keys($currencyTypesList[$currencyCode]));
					$fld->setMultiplierLabels(array_values($currencyTypesList[$currencyCode]));
					$fld->setMultiplierLabelVisibility(true);
					$fld->setStyle('CSSClass', 'input-multipliedElementLabelTop');
					$fld->setDisabled(true);
					$fieldsetCurrExch->addElement($fld);
				}
			}

			// Parity history
			$allParities = \App\CurrencyExchange\Date::getInstance($this->values['currencyExchDate'])->getParities(false);
			$groupedParities = [];
			foreach ($allParities as $fromCurrency => $parityDetails) {
				foreach ($parityDetails as $toCurrency => $parity) {
					if (!array_key_exists($toCurrency."&".$fromCurrency, $groupedParities)) {
						$groupedParities[$fromCurrency."&".$toCurrency][$fromCurrency."/".$toCurrency] = $parity;
					} else {
						$groupedParities[$toCurrency."&".$fromCurrency][$fromCurrency."/".$toCurrency] = $parity;
					}
				}
			}
			if ($allParities) {
				$fieldsetParity = \Engine\Form\Container\Fieldset::create($this->lang['index']['currExchParities']);
				$form->addContainer($fieldsetParity);

				foreach ($groupedParities as $key => $paritiesPair) {
					$fld = new \App\Form\Input\MultipliedText("Parity for ".$key, $key, $paritiesPair);
					$fld->setMultiplier(count($paritiesPair));
					$fld->setMultiplierLabelKeys(array_keys($paritiesPair));
					$fld->setMultiplierLabels(array_keys($paritiesPair));
					$fld->setMultiplierLabelVisibility(true);
					$fld->setStyle('CSSClass', 'input-multipliedElementLabelTop');
					$fld->setStyle('totalWidth', $form->getStyle('width'));
					$fld->setDisabled(true);
					$fieldsetParity->addElement($fld);
				}
			}

		}

		$form->addElement(\Engine\Form\Input\Hidden::create(null, $this->submitButtonName, 1));

		return $form;
	}

}