<?php

namespace Internetgalerie\IgDatapoolFe\ViewHelpers\Form;

/**
 * View Helper which creates a text field (<input type="text">).
 *
 * = Examples =
 *
 * <code title="Example">
 * <f:form.textfield name="myTextBox" value="default value" />
 * </code>
 * <output>
 * <input type="text" name="myTextBox" value="default value" />
 * </output>
 *
 * @api
 * @author     Markus Baumgartner, Daniel Abplanalp, Internetgalerie AG
 * @package    IgDatapoolFE
 * @subpackage ViewHelpers
 */

use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper;

class TextfieldViewHelper extends \Internetgalerie\IgCoreViewhelpers\ViewHelpers\Form\TextfieldViewHelper implements FieldInterface
{
    use FormFieldTrait;

    // remove if v12 is dropped
    protected PageRenderer $pageRenderer;

    public function injectPageRenderer(PageRenderer $pageRenderer): void
    {
        $this->pageRenderer = $pageRenderer;
    }

    public function initializeArguments(): void
    {
        parent::initializeArguments();
        $this->registerArgument('default', 'string', 'Default Value');
    }


    public function setArguments(array $arguments): void
    {
        if ($this->viewHelperVariableContainer->exists(FormViewHelper::class, 'fieldWrapperFields')) {
            $fieldWrapperFields = $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldWrapperFields');

            $fieldWrapperFields[] = $this;
            $this->viewHelperVariableContainer->addOrUpdate(FormViewHelper::class, 'fieldWrapperFields', $fieldWrapperFields);
        }
        /*
        if (isset($arguments['default'])) {
            if ($this->isObjectAccessorMode()) {
                // @todo use $this->persistenceManager->isNewObject();
                $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
                if ($viewHelperVariableContainer->exists(
                    FormViewHelper::class,
                    'formObject'
                )
                ) {
                    $formObject = $viewHelperVariableContainer->get(
                        FormViewHelper::class,
                        'formObject'
                    );
                    $uid = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($formObject, 'uid');
                    if (!$uid)  {
                        $arguments['value'] = $arguments['default'];
                    }
                }
            }
        }
        */
        parent::setArguments($arguments);
        //doesnt really fit here, but it works
        //\Internetgalerie\IgDatapoolFe\ViewHelpers\FieldWrapperViewHelper::setField($this);
    }
}
