<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Page;
use AppBundle\Entity\TravelCountry;
use AppBundle\Form\SearchRequestType;
use AppBundle\Util;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * Controller for re-usable view components. They are normally included by a render twig-expression. Example:
 *
 * <code>{{ render(controller('AppBundle:Component:travelGuideSidebarWidget', {country: page.country})) }}</code>
 */
class ComponentController extends Controller
{
    /**
     * @return EntityManager
     */
    public function getEntityManager()
    {
        return $this->getDoctrine()->getManager();
    }

    public function headerAction()
    {
        $navPages = $this->getEntityManager()->getRepository('AppBundle:Page')->findTopCountryNavPages();
        return $this->render('default/components/header.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'nav_pages' => $navPages,
        ]);
    }

    public function breadcrumbAction(Page $page)
    {
        return $this->render('default/components/breadcrumb.html.twig', [
          'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
          'breadcrumb_entries' => Util::createBreadcrumb($page),
        ]);
    }

    public function navSidebarWidgetAction(Page $page)
    {
        $pageRepo = $this->getEntityManager()->getRepository('AppBundle:Page');
        $view = [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'page' => $page,
        ];

        if ($page->getHasChildren())
        {
            if ($page->getLvl() == 0)
            {
                $view['nav_pages'] = $page->getChildren();
                $view['nav_child_pages'] = [];
            }
            else
            {
                $view['nav_pages'] = $pageRepo->getSiblings($page);
                $view['nav_child_pages'] = $page->getChildren();
            }
            $view['nav_open_node'] = $page;
        }
        else
        {
            $parent = $page->getParent();
            if ($parent)
            {
                $view['nav_pages'] = $pageRepo->getSiblings($parent);
                if (empty($view['nav_pages']))
                {
                    $view['nav_pages'] = $pageRepo->getSiblings($page);
                }
                else
                {
                    $view['nav_child_pages'] = $pageRepo->getSiblings($page);
                }
                $view['nav_open_node'] = $parent;
            }
            else
            {
                $view['nav_pages'] = $pageRepo->getSiblings($page);
                $view['nav_child_pages'] = [];
                $view['nav_open_node'] = null;
            }
        }
        return $this->render('default/components/sidebar/navSidebarWidget.html.twig', $view);
    }

    public function searchSidebarWidgetAction(Page $page)
    {
        $combinedDestination = null;
        if ($page->getTravelProgram())
        {
            $countries = $page->getTravelProgram()->getCountries();
            $destination = $countries->first();
            if (count($countries) > 1)
            {
                $combinedDestination = $countries[1];
            }
        }
        else
        {
            $destination = $page->getCountry();
        }
        return $this->render('default/components/sidebar/searchSidebarWidget.html.twig', [
            'search_form' => $this->createForm(SearchRequestType::class, [
                'c' => $destination,
                'c2' => $combinedDestination,
            ])->createView()
        ]);
    }

    public function travelGuideSidebarWidgetAction(TravelCountry $country)
    {
        $repo = $this->getEntityManager()->getRepository('AppBundle:Page');
        $rootPage = $repo->find(13);
        $pages = $repo->getChildrenQueryBuilder($rootPage)
            ->andWhere('IDENTITY(node.country) = '. $country->getId())
            ->andWhere('node.status > 0')
            ->getQuery()
            ->execute()
        ;
        return $this->render('default/components/sidebar/pageSliderSidebarWidget.html.twig', [
            'slider_title' => 'Reiseführer',
            'pages' => $pages
        ]);
    }

    public function travelMagazineSidebarWidgetAction(TravelCountry $country)
    {
        $repo = $this->getEntityManager()->getRepository('AppBundle:Page');
        $rootPage = $repo->find(2803);
        $pages = $repo->getChildrenQueryBuilder($rootPage)
            ->andWhere('IDENTITY(node.country) = '. $country->getId())
            ->andWhere('node.status > 0')
            ->getQuery()
            ->execute()
        ;
        return $this->render('default/components/sidebar/pageSliderSidebarWidget.html.twig', [
            'slider_title' => 'Reisemagazin',
            'pages' => $pages
        ]);
    }

    public function offersSidebarWidgetAction(TravelCountry $country = null)
    {
        return $this->render('default/components/sidebar/pageSliderSidebarWidget.html.twig', [
            'slider_title' => 'Angebote',
            'pages' => $this->getOffersByCountry($country),
        ]);
    }

    public function offersCarouselAction(TravelCountry $country = null)
    {
        return $this->render('default/components/multiPageBoxCarousel.html.twig', [
            'pages' => $this->getOffersByCountry($country),
        ]);
    }

    private function getOffersByCountry(TravelCountry $country = null)
    {
        $repo = $this->getDoctrine()->getRepository('AppBundle:Page');
        return $country === null
            ? $repo->findOffers()
            : $repo->findWithTravelProgramsOfCountry($country)
        ;
    }

    public function feedbacksSidebarWidgetAction(TravelCountry $country)
    {
        return $this->render(':default/components/sidebar:textSliderSidebarWidget.html.twig', [
            'slider_title' => 'Kundenfeedback',
            'slides' => $this->getDoctrine()->getRepository('AppBundle:Page')->findFeedbacks(
                $country->getFeedbackPage()->getId()),
            'theme' => 'gray-box',
        ]);
    }
}
