<?php

namespace Ayaline\Bundle\EzMeaLibreBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;

class MeaLibreController extends Controller
{
    public function listMeaChildrenAction($locationId = null)
    {
        /*
         * Si la liste est configuré sur le node de l'accueil
         */
        if ($locationId == null) $locationId = $this->container->getParameter( 'location_id.root' );

        /*
         * Initialisation de la repository / LocationService / ContentService
         */
        $repository = $this->getRepository();
        $locationService = $repository->getLocationService();
        $contentService = $repository->getContentService();
        $helperContent = $this->get('ayaline_tools.tools.content');
        $nbElementsByLigne = $this->container->getParameter( 'mea.liste.elements_par_colonne' );
        $childContentList = array();
        /*
         * Récupération de la liste des mises en avant à afficher
         */
        $locationContainer = $locationService->loadLocation($locationId);
        $contentContainer = $contentService->loadContent($locationContainer->contentId);
        $agendaPosition = $this->container->getParameter( 'mea.agenda.index' );
        if (
            $contentContainer->getFieldValue($this->container->getParameter( 'attribut_id.accueil.liste_mea' )) &&
            $contentContainer->getFieldValue($this->container->getParameter( 'attribut_id.accueil.liste_mea' ))->destinationContentId != null
        )
        {
            $listeMeaContentId = $contentContainer->getFieldValue($this->container->getParameter( 'attribut_id.accueil.liste_mea' ))->destinationContentId;
            $listeMeaContent = $contentService->loadContent($listeMeaContentId);
            $childMeaList = $helperContent->contentList(
                $listeMeaContent->contentInfo->mainLocationId,
                array(
                    $this->container->getParameter( 'class_identifier.mea_libre' ),
                    $this->container->getParameter( 'class_identifier.sit_mise_en_avant' )
                ),
                array(new SortClause\LocationPriority( Query::SORT_ASC ))
            );
            if (count($childMeaList))
            {
                $counter = 0;
                $index = 1;
                foreach ($childMeaList as $child)
                {
                    if ($nbElementsByLigne == $counter)
                    {
                        $counter = 0;
                        $index++;
                    }
                    if (!isset($childContentList[$index]))
                    {
                        $childContentList[$index] = array();
                    }
                    $childContentList[$index][] = $child->id;
                    $counter++;
                    if ($index == $agendaPosition) {
                        $counter++;
                    }
                }
            }
        }
        $response = $this->render(
            "AyalineEzMeaLibreBundle::liste_mea.html.twig"
            , array
            (
                'childContentList' => $childContentList,
                'nbElementsByLigne' => $nbElementsByLigne,
            )
        );
        $response->headers->set( 'X-Location-Id', $locationId);
        // 1jour
        $response->setSharedMaxAge(86400);
        return $response;
    }
}
