<?php

namespace Internetgalerie\IgDatapoolFe\ViewHelpers\Form;

use TYPO3\CMS\Extbase\Utility\DebuggerUtility;

/**
 * Provides accessors for use with the tabs / fieldwarpper engine.
 */
trait FieldTrait
{
    /**
     * the fields id
     *
     * @var $fieldId string
     */
    protected $fieldId = '';

    /**
     * Debug
     */
    public static $forceError = false;

    /**
     * Inject the ID Argument. has to be done this way because of the caching stuff...
     */
    public function setArguments(array $arguments)
    {

        //doesnt really fit here, but it works
        \Internetgalerie\IgDatapoolFe\ViewHelpers\FieldWrapperViewHelper::setField($this);

        if (null === $arguments['id']) {
            if ($this->isSearchForm()) {
                $this->fieldId = $this->generateSearchId($arguments);
            } else {
                $this->fieldId = $this->generateId($arguments);
            }
        }
        if ($this->fieldId) {
            $arguments['id'] = $this->fieldId;
        }

        /*
         * Override defaults
         * we REALLY want our own error class
         */
        if (!isset($arguments['errorClass']) || $arguments['errorClass'] == 'f3-form-error') {
            $arguments['errorClass'] = 'dp-field-error';
        }
        parent::setArguments($arguments);
    }

    /**
     * Check if field has a validation error
     *
     * @return boolean
     */
    public function hasError()
    {
        if (self::$forceError) {
            return true;
        }
        return $this->getMappingResultsForProperty()->hasErrors();
    }

    protected function setErrorClassAttribute()
    {
        parent::setErrorClassAttribute();
        if (self::$forceError) {
            $class = $this->tag->getAttribute('class');
            $this->tag->addAttribute('class', $class . ' ' . $this->arguments['errorClass']);
        }
    }

    /**
     * get all validation errors
     *
     * @return TYPO3\CMS\Extbase\Error\Result
     */
    public function getErrors()
    {
        if (self::$forceError) {
            $k = new \TYPO3\CMS\Extbase\Error\Result();
            $k->addError(new \TYPO3\CMS\Extbase\Error\Error('Demo Fehlermeldung'));
            return $k;
        }
        return $this->getMappingResultsForProperty();
    }

    /**
     * Return the whole path according to the model.field scheme
     *
     * @param  string $separator How to separate both parts
     * @return string
     */
    public function getNamePath($separator = '.')
    {
        if ($this->isSearchForm()) {
            return 'search' . $separator . $this->arguments['name'];
        }
        $out = '';
        if (null != \Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm) {
            $out = strtolower(\Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm->_getFormName()) . $separator;
        }
        return $out . $this->arguments['property'];
    }

    /**
     * Returns the property that the formfield is bound to
     */
    public function getProperty()
    {
        return $this->arguments['property'];
    }

    /**
     * get the fields Id as written in the HTML
     *
     * @return string
     */
    public function getId()
    {
        return $this->fieldId;
    }

    /**
     * Generates a HTML Id
     */
    protected function generateId($arguments)
    {
        if (null == \Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm) {
            throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception('This field MUST be used inside DataPool Form!');
        }
        //form name.
        $fieldId = 'dp-field-' . str_replace('.', '-', strtolower(\Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm->_getFormName()) . '-' . $arguments['property']);
        if ($this->isMultipleValuedField()) {
            $fieldId .= '-' . $arguments['value'];
        }
        return $fieldId;
    }

    /**
     * Generates a HTML Id
     */
    protected function generateSearchId($arguments)
    {
        if (null == \Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm) {
            throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception('This field MUST be used inside DataPool Form!');
        }
        //form name.
        $fieldId = 'dp-search-' . $arguments['name'];
        if ($this->isMultipleValuedField()) {
            $fieldId .= '-' . $arguments['value'];
        }
        return $fieldId;
    }

    /**
     * is this a multivalued field like checkboxes or radio boxes?
     */
    protected function isMultipleValuedField()
    {
        $all = array('Internetgalerie\IgDatapoolFe\ViewHelpers\Form\RadioViewHelper', 'Internetgalerie\IgDatapoolFe\ViewHelpers\Form\CheckboxViewHelper');
        return in_array(get_class($this), $all);
    }

    protected function isSearchForm()
    {
        return \Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm  !==null  && \Internetgalerie\IgDatapoolFe\ViewHelpers\FormViewHelper::$curForm->isSearchForm();
    }

    /**
     * Add support for default value. must be registred by the specific fieldtype!!
     *
     * @see TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
     *
     * Get the value of this form element.
     * Either returns arguments['value'], or the correct value for Object Access.
     *
     * @param  boolean $convertObjects whether or not to convert objects to identifiers
     * @return mixed Value
     */

    protected function getValueAttribute($convertObjects = true)
    {
        // EDIT MB
        // Searchform Value
        //if ($this->isSearchForm()) {
        if ($this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formClass')) {
            $formClass = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formClass');

            if ($formClass==\Internetgalerie\IgDatapoolFe\ViewHelpers\SearchFormViewHelper::class) {
                $requestArguments = array();
                if ($this->renderingContext->getRequest()->hasArgument('@search')) {
                    $requestArguments = $this->renderingContext->getRequest()->getArgument('@search');
                    if (isset($requestArguments[$this->arguments['name']])) {
                        return $requestArguments[$this->arguments['name']];
                    }
                }
            }
        }
        // end edit mb

        $value = null;
        if ($this->hasArgument('value')) {
            $value = $this->arguments['value'];
        } elseif ($this->hasMappingErrorOccurred()) {
            $value = $this->getLastSubmittedFormData();

        // here we have to add additionalidedentiyfield!!!!
        } elseif ($this->isObjectAccessorMode()) {
            $value = $this->getPropertyValue();

            // XXX: Is this line correct here? only works with ObjectAccessorMode

            if (null === $value && isset($this->arguments['default'])) {
                $value = $this->arguments['default'];
            }
        }
        if ($convertObjects === true && is_object($value)) {
            $identifier = $this->persistenceManager->getIdentifierByObject($value);
            if ($identifier !== null) {
                $value = $identifier;
            }
        }
        if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject')) {
            $this->addAdditionalIdentityPropertiesIfNeeded();
        }
        return $value;
    }













    /**
     * Checks if a property mapping error has occurred in the last request.
     *
     * @return bool TRUE if a mapping error occurred, FALSE otherwise
     */
    protected function hasMappingErrorOccurred()
    {
        $ret = $this->renderingContext->getRequest()->getOriginalRequest() !== null;
        return $ret;
    }

    /**
     * adjusted for searchform
     * use _ to avoid autowrapping inside a searchform
     */
    protected function getNameWithoutPrefix()
    {
        if ($this->isSearchForm()) {
            if (strpos($this->arguments['name'], '_') === 0) {
                return trim($this->arguments['name'], '_');
            }
            return '@search[' . $this->arguments['name'] . ']';
        }

        return parent::getNameWithoutPrefix();
    }

    /**
     * Automagically add the reuired flag if property has an accroding validator
     */
    protected function isRequiredByAnnotation()
    {
        return \Internetgalerie\IgDatapoolFe\Utility\NeedfulThings::isRequiredByAnnotation($this->getProperty());
    }

    /**
     * Renders a hidden form field containing the technical identity of the given object.
     *
     * @param  object $object Object to create the identity field for
     * @param  string $name   Name
     * @return string A hidden field containing the Identity (UID in TYPO3 Flow, uid in Extbase) of the given object or NULL if the object is unknown to the persistence framework
     * @see    \TYPO3\CMS\Extbase\Mvc\Controller\Argument::setValue()
     */
    protected function renderHiddenIdentityField($object, $name)
    {
        if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
            $object = $object->_loadRealInstance();
        }
        if (!is_object($object) || !($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject) || ($object->_isNew() && !$object->_isClone())) {
            return '';
        }
        // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
        // Using that one breaks re-submission of data in forms in case of an error.
        $identifier = $object->getUid();
        if ($identifier === null) {
            return chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
        }
        $newName = $this->prefixFieldName($name) . '[__identity]';
        $this->registerFieldNameForFormTokenGeneration($name);

        //EDIT MB: add a class
        return chr(10) . '<input type="hidden" name="' . $newName . '" value="' . $identifier . '" class="' . trim(str_replace(array('][', '['), '-', $name), '[]') . '" />' . chr(10);
        //END EDIT MB.
    }
}
