<?php

namespace Modules\Admin\Config\Remarks\AssistedInputs;

class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\PopupAbstract::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] = [ 'checkSection' => 'Modules/Admin/Config/Remarks', 'checkOption' => false ];
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$formData = [];

		// get the remark
		list($category, $key) = explode('|',$R['id']);
		$remark = \App\Config\Remarks::getAdmin()->setHandledCategory($category)->get($key);

		$formData['format'] = $remark['format'];
		$formData['vars'] = $remark['vars'];

		// retrieve the service types assigned to the remark's visibility conditions
		$services = [];
		if($remark['visibilityConditions']) {
			foreach ($remark['visibilityConditions'] as $condition) {
				if ($condition['field'] == 'serviceType') {
					$services[] = $condition['value'];
				}
			}
		}
		$formData['services'] = $services;

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		list($category, $key) = explode('|',$R['id']);
		$remark = \App\Config\Remarks::getAdmin()->setHandledCategory($category)->get($key);

		// information about names, type, optional, default, select option, minLength ... remain unchanged
		$varsListToSet = $remark['vars'];

		// rewrite assisted input and assisted input details with vars from request
		foreach ($remark['vars'] as $indexVar => $varDetails) {

			$varsListToSet[$indexVar]['assistedInput'] = $R['vars'][$indexVar]['assistedInput'];
			$varsListToSet[$indexVar]['assistedInputDetails'] = $R['vars'][$indexVar]['assistedInputDetails'];
		}

		//set up visibility conditions
		if($remark['visibilityConditions']) {
			//first remove those that have 'serviceType' as field
			foreach($remark['visibilityConditions'] as $idxCond => $condition) {
				if($condition['field'] == 'serviceType') {
					unset($remark['visibilityConditions'][$idxCond]);
				}
			}
		}
		// then append the services selected
		$services = $R['services'];
		if($services) {
			foreach($services as $serviceCode) {
				$remark['visibilityConditions'][] = [
					'field' => 'serviceType',
					'value' => $serviceCode
				];
			}
		}

		// edit the remark
		try {
			\App\Config\Remarks::getAdmin()->itemUpdate($key, [
				'format' => $remark['format'],
				'vars' => $varsListToSet,
				'comments' => $remark['comments'],
				'visibilityConditions' => $remark['visibilityConditions']
				]
			);
			$msgs->addSuccess($this->lang['successMessage']['remarkUpdated']);
		}
		catch (\App\Config\MessageException $e) {
			$msgs->addError(sprintf($this->lang['errorMessage']['cannotUpdateRemark'], $e->getMessage()));
		}

		return !$msgs->hasErrorOrWarning();
	}

}