<?php

namespace Ayaline\Bundle\EzMiniSitesCollectivitesBundle\Controller;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;

/**
 * Class HomeController
 * @package Ayaline\Bundle\EzMiniSitesCollectivitesBundle\Controller
 */
class HomeController extends Controller
{
    /**
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getBlocHomeAction($type, $template)
    {
        $repository = $this->getRepository();
        $locationService = $repository->getLocationService();
        $contentService = $repository->getContentService();

        $helperContent = $this->get('ayaline_tools.tools.content');
        $rootLocation = $this->getRootLocation();
        $homeContent = $contentService->loadContent($rootLocation->contentInfo->id);
        $configGeneralIdentifier = $this->container->getParameter('identifier.config_general.'.$this->getRequest()->get('siteaccess')->name);


        $configurationGenerale = $helperContent->contentList(
            $rootLocation->id,
            array($configGeneralIdentifier),
            array(),
            1
        );

        $contentList = array();
        $rootLocationId = null;
        $configurationGenerale = $configurationGenerale[0];

        if (!empty($configurationGenerale) && $configurationGenerale->getFieldValue($type)->destinationContentId) {

            $rootContentId = $configurationGenerale->getFieldValue($type)->destinationContentId;
            $contentRoot = $contentService->loadContent($rootContentId);
            $rootLocationId = $contentRoot->contentInfo->mainLocationId;
            $locationRoot = $locationService->loadLocation($rootLocationId);
            $locationsList = $locationService->loadLocationChildren($locationRoot);

            if ($locationsList->totalCount) {
                foreach ($locationsList->locations as $location) {
                    if (!$location->hidden) {

                        $locationId = $location->contentInfo->mainLocationId;
                        $locationInfo = $locationService->loadLocation($locationId);
                        $locationsChildrenList = $locationService->loadLocationChildren($locationInfo, 0, 3);

                        foreach ($locationsChildrenList->locations as $locationsChildren) {
                            if (!$locationsChildren->hidden) {
                                $childrenContent = $contentService->loadContent($locationsChildren->contentInfo->id);
                                $contentList[$locationId][] = $childrenContent;
                            }
                        }
                    }
                }

            }
        }

        $response = $this->render(
            $template,
            array(
                'contentList' => $contentList,
                'rootLocationId' => $rootLocationId,
                'configurationGenerale' => $configurationGenerale,
                'homeContent' => $homeContent
            )
        );

        $response->setSharedMaxAge($this->container->getParameter("content.default_ttl"));

        return $response;
    }
}
