<?php

namespace Opentalent\OtTemplating\ViewHelpers\Donors;

use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
use Opentalent\OtCore\Domain\Repository\DonorRepository;
use Opentalent\OtCore\Exception\ApiRequestException;
use Opentalent\OtCore\Logging\OtLogger;
use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;

/**
 *   This view helper give access to an ApiPagedCollection named according to the 'as' variable
 *   and which contains all the donors of the given structure
 *
 *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
 *
 *     <ot:donors.getAll as="donors"
 *                       organizationId="{settings.organizationId}">
 *          <f:debug>{donors}</f:debug>
 *     </ot:donors.getAll>
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class GetAllViewHelper extends OtAbstractViewHelper {

    use TemplateVariableViewHelperTrait;
    /**
     * >> Required to prevent typo3 to escape the html output
     * @var boolean
     */
    protected $escapeOutput = false;

    /**
     * @var DonorRepository
     *
     */
    protected $donorRepository;

    /**
     * -- This method is expected by Fluid --
     * Declares the viewhelper's parameters
     */
    public function initializeArguments()
    {
        $this->registerArgument(
            'as',
            'string',
            'Name of the returned array',
            true
        );
        $this->registerArgument(
            'organizationId',
            'integer',
            'Id of the current structure',
            true
        );
        $this->registerArgument(
            'fromParents',
            'bool',
            'Get donors from parents instead',
            false,
            0
        );
    }

    /**
     * -- This method is expected by Fluid --
     * Renders the content as html
     *
     * @return string
     * @throws ApiRequestException
     */
    public function render()
    {
        // Get current settings
        $as = $this->arguments['as'];
        $organizationId = $this->arguments['organizationId'];
        $fromParents = $this->arguments['fromParents'];

        if ($fromParents) {
            // Get the donors of the parent structures
            try {
                $donors = $this->donorRepository->findParentsByOrganizationId($organizationId);
            } catch (ApiRequestException $e) {
                OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
                $donors = [];
            }
        } else {
            // Get donors of the structure
            try {
                $donors = $this->donorRepository->findByOrganizationId($organizationId);
            } catch (ApiRequestException $e) {
                OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
                $donors = [];
            }
        }

        $variables = [$as => $donors];
        return $this->renderChildrenWithVariables($variables);
    }

    /**
     * @param DonorRepository $donorRepository
     */
    public function injectDonorRepository(DonorRepository $donorRepository)
    {
        $this->donorRepository = $donorRepository;
    }
}
