<?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, $contentParentId = 'configuration_generale_minisite')
    {
        $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);
        $contentParent = $homeContent;
        if($contentParentId !== 'accueil_mini_site'){
            $configurationGenerale = $helperContent->contentList(
                $rootLocation->id,
                array($contentParentId),
                array(),
                1
            );
            $contentParent = !empty($configurationGenerale) ? $configurationGenerale[0] : null;
        }

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

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

            $rootContentId = $contentParent->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' => $contentParent,
                'homeContent' => $homeContent
            )
        );

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

        return $response;
    }

    /**
     * @param $template
     * @param $contentParentId
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getReseauSociauxFromFolderAction($template,$contentParentId){
        $listReseauSociaux = array();
        $helperContent = $this->get('ayaline_tools.tools.content');
        $listReseauSociaux = $helperContent->contentTree(
            $contentParentId,
            array('reseau_social')
        );
        $response = $this->render(
            $template,
            array(
                'contentList' => $listReseauSociaux
            )
        );

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

        return $response;
    }
}
