<?php
/**
 * @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
 * @date 02/22/2017
 */

namespace AppBundle\Controller;


use AppBundle\Entity\Page;
use AppBundle\Form\TtSearchRequestType;
use AppBundle\Listener\KernelControllerListener;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * Controller for CMS pages. CMS pages are represented by Page instances (i.e. entries of the page database table).
 * The template property of a Page object defines the action method below. If a travel program is assigned then the
 * travelProgramAction is used. If no template is specified, then the defaultAction is used.
 * If there is no action method, the defaultAction is called and the template name is passed as an argument. A twig
 * file with this template will be rendered in this case.
 *
 * The view templates can be found pages/cms.
 *
 * Also see {@link KernelControllerListener}, which handles routing.
 *
 * @package AppBundle\Controller
 */
class CmsController extends Controller
{
    /**
     * @return EntityManager
     */
    private function getEntityManager()
    {
        return $this->getDoctrine()->getManager();
    }

    public function defaultAction(Page $page, $template = 'default')
    {
        return $this->render('default/pages/cms/'. $template .'.html.twig', [
          'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
          'page' => $page,
        ]);
    }

    public function overviewAction(Page $page)
    {
        return $this->render('default/pages/cms/overview.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'page' => $page,
        ]);
    }

    public function travelProgramOverviewAction(Page $page)
    {
        $childPages =
          $this->getEntityManager()->getRepository('AppBundle:Page')->getChildrenWithTravelProgramsAndDates($page);

        $nonMediated = [];
        $mediated = [];
        foreach ($childPages as $childPage)
        {
            if ($childPage->getStatus() == 1 && $childPage->getTravelProgram() &&
                $childPage->getTravelProgram()->getIsMediated())
            {
                $mediated[] = $childPage;
            }
            else
            {
                $nonMediated[] = $childPage;
            }
        }
        // We only need a separation if there are mediated AND non mediated travel programs
        if (empty($nonMediated) && !empty($mediated))
        {
            $childPages = $mediated;
        }
        else
        {
            $childPages = $nonMediated;
        }

        return $this->render('default/pages/cms/travelProgramOverview.html.twig', [
            'base_dir'    => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'page'        => $page,
            'child_pages' => $childPages,
            'mediated_child_pages' => $mediated,
        ]);
    }

    public function traveltainmentAction(Page $page)
    {
        $form = $this->createForm(TtSearchRequestType::class, null, [
            'country' => $page->getCountry(),
        ]);

        return $this->render('default/pages/cms/traveltainment.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'page' => $page,
            'tt_search_form' => $form->createView(),
        ]);
    }

    public function travelProgramAction(Page $page)
    {
        $this->getDoctrine()->getRepository('AppBundle:TravelPeriod')->getTrueTravelPeriods($page->getTravelProgram());

        // replace this example code with whatever you need
        return $this->render('default/pages/cms/travelProgram.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'page' => $page,
            'travel_program' => $page->getTravelProgram()
        ]);
    }
}