<?php

namespace RxConnexionBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @Route("")
 */
class MapController extends Controller
{
    /**
     * @Route("/map", name="map")
     */
    public function indexAction()
    {
        $authService = $this->get('rx_connexion.authService');
        if(!$authService->isLoggedIn()) return $authService->redirectToLogin();

        return $this->render('@RxConnexion/Map/map.html.twig', array(
            'title' => 'Map',
            'page_title' => 'Map',
            'googleMapApiKey' => $this->container->getParameter('google_map_api_key'),
            'stylesheets' => array('rxconnexion/admin/css/map/map.css'),
            'javascripts' => array('rxconnexion/admin/js/map/map.js')
        ));
    }

    /**
     * @Route("/health_facility_for_map", name="health_facility_for_map_ajax")
     */
    public function health_facility_for_map_ajax(Request $request) {

        $authService = $this->get('rx_connexion.authService');
        if($authService->isLoggedIn()) {
            $em = $this->getDoctrine()->getManager();

            $healthFacilities = $em->getRepository('RxConnexionBundle:HealthFacilityEntity')->healthFacilityForMap();

            $result['success'] = true;
            $result['healthFacilities'] = $healthFacilities;

        }else{
            $result['success'] = false;
        }

        return new JsonResponse($result);
    }
}
