<?php

namespace Opentalent\OtTemplating\ViewHelpers\Organizations;

use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
use Opentalent\OtCore\Domain\Repository\FederationStructureRepository;
use Opentalent\OtCore\Exception\ApiRequestException;
use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;

/**
 *   This view helper returns all of the organization children structures as an ApiPagedCollection, without pagination
 *
 *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
 *
 *     <ot:organizations.getFederationStructures as="organization"
 *                               organizationId="1">
 *          <f:debug>{organization}</f:debug>
 *     </ot:organizations.getFederationStructures>
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class GetFederationStructuresViewHelper extends OtAbstractViewHelper {

    use TemplateVariableViewHelperTrait;

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

    /**
     * @var FederationStructureRepository
     *
     */
    protected $federationRepository;

    /**
     * -- 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(
            'parentId',
            'integer',
            'Id of the parent organization',
            true
        );
        $this->registerArgument(
            'itemsPerPage',
            'string',
            'Number of items per page',
            false,
            10
        );
        $this->registerArgument(
            'depth',
            'integer',
            'Max level of depth',
            false,
            5
        );
    }

    /**
     * -- This method is expected by Fluid --
     * Renders the content as html
     *
     * @return string
     * @throws ApiRequestException
     */
    public function render()
    {
        $as = $this->arguments['as'];
        $parentId = $this->arguments['parentId'];
        $itemsPerPage = $this->arguments['itemsPerPage'];
        $page = $_REQUEST['page'] ?? 1;
        $depth = $_REQUEST['depth'] ?? 5;

        if ($itemsPerPage == 'all') {
            $itemsPerPage = 99999; // the 'all' keyword raise an error during the api call
            $page = 1;
        }

        $searchParams = ['itemsPerPage' => $itemsPerPage];

//        // If a federation was selected in the filters, it is used as parent
//        if($_REQUEST['search-federation']) {
//            $parentId = (int)$_REQUEST['search-federation'];
//        }
//
//        // Search structures by name
//        if($_REQUEST['search-query']) {
//            $searchParams['what'] = $_REQUEST['search-query'];
//        }
//
//        // Filters
//        if($_REQUEST['search-category']) {
//            $searchParams['category'] = $_REQUEST['search-category'];
//        }
//        if($_REQUEST['search-province']) {
//            // A leading '_' may have been added to prevent the '0' from being stripped by fluid
//            $province = ltrim($_REQUEST['search-province'], '_');
//            $searchParams['province'] = $province;
//        }
//        if($_REQUEST['lat']) {
//            $radius = (int)$_REQUEST['search-radius'] ?? 0;
//
//            // radius is always increased of 10km to take into account the size of the city itself
//            $radius += 10;
//
//            $searchParams['lat'] = $_REQUEST['lat'];
//            $searchParams['long'] = $_REQUEST['long'];
//            $searchParams['radius'] = $radius;
//        }

        $organizations = $this->federationRepository->findChildrenById($parentId, $searchParams, $page, $depth);

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

    /**
     * @param FederationStructureRepository $federationRepository
     */
    public function injectFederationRepository(FederationStructureRepository $federationRepository)
    {
        $this->federationRepository = $federationRepository;
    }
}
