<?php
/**
 * Created by PhpStorm.
 * User: danielknudsen
 * Date: 05.04.18
 * Time: 13:09
 */
namespace App\Controller;

use App\Entity\EntOperator;
use App\Entity\EntOperatorContact;
use App\Entity\EntOperatorMeeting;
use App\Utils\Message;
use App\Utils\Reply;
use App\Utils\Utils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class OperatorViewController
 * @package App\Controller
 * @IsGranted("ROLE_USER")
 */
class OperatorViewController extends AbstractController
{
    /**
     * @Route("/get-operator-data", name="get_operator_data")
     */
    public function getOperatorData()
    {
        $em = $this->getDoctrine()->getManager();
        return Reply::getResponse(
            [
                'operators' => Utils::clientMap($em, $em->getRepository(EntOperator::class)->findAll()),
                'operatorContacts' => Utils::clientMap($em, $em->getRepository(EntOperatorContact::class)->findAll()),
                'operatorMeetings' => Utils::clientMap($em, $em->getRepository(EntOperatorMeeting::class)->findAll()),
            ]
        );
    }

    /**
     * @Route("/get-operator-full", name="get_operator_full")
     * @param Request $request
     * @return JsonResponse
     */
    public function getOperatorFull(Request $request)
    {
        $operatorId = json_decode($request->request->get('operatorId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository(EntOperator::class)->find($operatorId);
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        return Reply::getResponse($entOperator->clientMapper($em, true));
    }

}