<?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;
use Symfony\Component\HttpFoundation\Response;

class MeaLibreController extends Controller
{
    public function listMeaChildrenAction($locationId = null, $imageClass='mea_simple' )
    {
        /*
         * 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->hasParameter( 'mea.agenda.index' ) ? $this->container->getParameter( 'mea.agenda.index' ) : null;
        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;
        }
        else{
            $listeMeaContentId = $contentContainer->id;
        }
            $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'),
                    $this->container->getParameter('class_identifier.rubrique')
                ),
                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 ($agendaPosition !== null && $index == $agendaPosition) {
                        $counter++;
                    }
                }
            }

        $response = $this->render(
            "AyalineEzMeaLibreBundle::liste_mea.html.twig",
            array
            (
                'childContentList' => $childContentList,
                'nbElementsByLigne' => $nbElementsByLigne,
                'imageClass' => $imageClass
            )
        );
        $response->headers->set('X-Location-Id', $locationId);
        // 1jour
        $response->setSharedMaxAge(86400);

        return $response;
    }

    public function getTitleAction($contentId)
    {
        $repository = $this->getRepository();
        $contentService = $repository->getContentService();
        $content = $contentService->loadContent($contentId);

        $response = new Response($content->contentInfo->name);

        $response->headers->set('X-Location-Id', $content->contentInfo->mainLocationId);
        // 1jour
        $response->setSharedMaxAge(86400);

        return $response;
    }

    public function getDescriptionAction($contentId = null)
    {
        $contentService = $this->getRepository()->getContentService();
        $content = $contentService->loadContent($contentId);
        $fieldDescription = null;
        $charLimit = $this->container->getParameter('charLimit_mea_description');
        $attributesDescription = $this->container->getParameter('fields_description');
        foreach ($attributesDescription as $attribute) {
            if (isset($content->fields[$attribute])) {
                $fieldDescription = $content->getFieldValue($attribute);
            }
        }
        $response = $this->render(
            "AyalineEzMeaLibreBundle:parts:description.html.twig",
            array
            (
                'description' => $fieldDescription,
                'charLimit_mea_description' => $charLimit,
            )
        );
        $response->headers->set('X-Location-Id', $content->contentInfo->mainLocationId);
        // 1jour
        $response->setSharedMaxAge(86400);

        return $response;
    }
}