<?php

namespace AppBundle\Controller;

use AppBundle\Form\AdSearchType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function homepageAction()
    {
        $em = $this->getDoctrine()->getManager();
        $ads = $em->getRepository('AppBundle:Ad')->findBy(array("status" => "PUBLISHED", "emphasis" => true, "isPromoted" => false), array('emphasisOrder' => 'DESC'));
        if (!$ads) {
            $ads = $em->getRepository('AppBundle:Ad')->findBy(array("status" => "PUBLISHED", "isPromoted" => false), array('id' => 'DESC'));
        }
        $talents = $em->getRepository('AppBundle:Company')->findBy(array("type" => "TALENT", "emphasis" => true), array('emphasisOrder' => 'DESC'));
        $companies = $em->getRepository('AppBundle:Company')->findBy(array("type" => "COMPANY", "emphasis" => true), array('emphasisOrder' => 'ASC'));
        $territories = $em->getRepository('AppBundle:Company')->findBy(array("type" => "TERRITORY", "emphasis" => true), array('emphasisOrder' => 'ASC'));

        $arrayTalents = array();
        foreach ($talents as $talent) {
            if ($talent->getPresentationVideo()) {
                $arrayTalents[] = $talent;
            }
        }

        $arrayCompany = array();
        foreach ($companies as $company) {
            if ($company->getPresentationVideo()) {
                $arrayCompany[] = $company;
            }
        }

        $mediaManager = $this->get('media_manager');
        foreach ($ads as $ad) {
            $mediaManager->getThumbnails($ad);
        }

        foreach ($companies as $company) {
            $mediaManager->getThumbnails($company);
        }

        foreach ($territories as $t) {
            $mediaManager->getThumbnails($t);
        }

        $form = $this->createForm(AdSearchType::class);

        return $this->render(
            'default/index.html.twig', [
                'ads' => $ads,
                'companies' => $arrayCompany,
                'talents' => $arrayTalents,
                'territories' => $territories,
                'searchform' => $form->createView(),
            ]
        );
    }

    /**
     * @Route("/redirect_dashboard", name="redirect_dashboard")
     */
    public function redirect_dashboard()
    {
        $user = $this->getUser();
        if ($user) {
            if ($user->hasRole('ROLE_TALENT')) {

                return $this->redirectToRoute('talent_dashboard');
            } elseif ($user->hasRole('ROLE_COMPANY')) {

                return $this->redirectToRoute('company_dashboard');
            }
        } else {

            return $this->redirectToRoute('homepage');
        }

    }

    /**
     * @Route("/test", name="test")
     */
    public function testAction()
    {
        //return $this->render('page_static/marque_employeur.html.twig');
        //return $this->render('page_static/offre_emploi.html.twig');
        //return $this->render('page_static/accompagnement_video_company.html.twig');
        //return $this->render('page_static/reussir_video_company.html.twig');
        //return $this->render('page_static/accompagnement_video_talent.html.twig');
        //return $this->render('page_static/cvvideo.html.twig');
        //return $this->render('page_static/reussir_video_talent.html.twig');
        //return $this->render('page_static/video_territoire.html.twig');
        return $this->render('page_static/accompagnement_video_territoire.html.twig');
    }
}
