<?php
namespace App\Dashboard\Widget;

class CurrencyCalculator extends \Engine\Dashboard\Widgets\WidgetAbstract
{
	/**
	 * The default options associated when the widget is added.
	 * @var array
	 */
	protected function getDefaultOptions()
	{
		return [];
	}


	/**
	 * Sets the currency exchange rates details.
	 */
	protected function getData()
	{

		$calledClass = get_called_class();

		global $CFG, $_LANG;

		// form default values
		$formValues = [];
		$formValues['initialCurrency']=$CFG['currencies']['workCurrency'];
		$formValues['finalCurrency']=$CFG['currencies']['defaultCurrency'];
		$formValues['currencyDate']=TODAY_ISO;
		$formValues['currencyType']=$CFG['currencies']['defaultCurrencyType'];

		$id = $this->getId();

		// define the form
		$form = new \Engine\Form\General();
		$form->setId('formCurrencyCalculator');
		$form->addContainer(\Engine\Form\Container\Table::create());
		$form->setStyle('width', 150);

		// original price
		$fld=new \App\Form\Input\Price($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['initialPrice'], "price_{$id}", ['value'=>$formValues['initialPrice'], 'currency'=>$formValues['initialCurrency']]);
		$fld->setMandatory();
		$form->addElement($fld);

		// final currency
		$fld=new \App\Form\Input\Select_Currency($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['finalCurr'], "finalCurrency_{$id}", $formValues['finalCurrency']);
		$fld->setMandatory();
		$form->addElement($fld);

		// currency date
		$fld=new \Engine\Form\Input\Date($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['currDate'], "currencyDate_{$id}", $formValues['currencyDate']);
		$form->addElement($fld);

		// currency type
		$fld=new \App\Form\Input\Select_CurrencyType($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['currType'], "currencyType_{$id}", $formValues['currencyType']);
		$form->addElement($fld);

		// response
		$fld = new \Engine\Form\Input\Text($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['response'], "response_{$id}");
		$fld->setStyle('CSSClass', 'response');
		$fld->setDisabled(true);
		$form->addElement($fld);

		// submit button
		$fldButton = new \Engine\Form\Button\Button($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['submitBtn']);

		$dashboard = $this->getDashboard();
		$idEntity = '';
		if ($dashboard->getOption('idCorporate')) {
			$idEntity = $dashboard->getOption('idCorporate');
		}
		elseif ($dashboard->getOption('idCustomer')) {
			$idEntity = $dashboard->getOption('idCustomer');
		}



		$fldButton->addEvent('onClick', "window.dashboardController.dashboard.request(MVC.appUrl(MVC.CURRENT_MODULE, 'editDashboardElement', { 'idEntity' : '$idEntity' } ),
				{
					operation: 'methodCall',
					method: 'convert',
					src: '$id',
					price: \$F('price_{$id}_value_'),
					currency: \$F('price_{$id}_currency_'),
					currencyDate: \$F('currencyDate_{$id}'),
					currencyType: \$F('currencyType_{$id}'),
					finalCurrency: \$F('finalCurrency_{$id}')
				},

				window.currencyCalculator_callMethod_callMethod
			);
		");
		$form->addButton($fldButton);

		return ['form'=>$form];
	}


	/**
	 * Generate the options form
	 */
	protected function populateLocalOptionsForm(\Engine\Form\Container\GeneralAbstract $form)
	{
		return $form;
	}


	/**
	 * Extract data from the form return($R) and sets internal configs($this->options) accordingly
	 */
	protected function extractFormOptions($formData)
	{
	}


	/**
	 * Used for AJAX-ex to call a certain method of the current widget.
	 */
	public function __callMethod($method, $params, $JSONResponse) {
		if ($method=='convert') {
			return $this->convert($params, $JSONResponse);
		}
		return parent::__callMethod($method, $params, $JSONResponse);
	}

	/**
	 * Calculates the converted price.
	 * @param Array $params (the $R array)
	 */
	private function convert($params, $JSONResponse)
	{
		$JSONResponse->status = 'OK';
		$JSONResponse->response = \App\CurrencyExchange\Rates::getInstance($params['currencyDate'], $params['currencyType'])->convert($params['price'], $params['currency'], $params['finalCurrency']);
	}
}