<?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\EntOperatorNote;
use App\Entity\EntUser;
use App\EntityVirtual\ServiceData;
use App\Utils\Message;
use App\Utils\Reply;
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 OperatorEditController
 * @package App\Controller
 * @IsGranted({"ROLE_ACCOUNTING", "ROLE_TECHNIQUE", "ROLE_ADMIN"})
 */
class OperatorEditController extends AbstractController
{
    /**
     * @Route("/create-operator", name="create_operator")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createOperator(Request $request)
    {
        $operatorClient = json_decode($request->request->get('operator'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntOperator $entOperator */
        $entOperator = new EntOperator($em, $operatorClient->name);
        $entOperator->setClientData($em, $operatorClient);
        $em->persist($entOperator);
        $em->flush();

        $fullMappedOperator = $entOperator->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_OPERATORS, ServiceData::ACTION_ADD, $fullMappedOperator);
        return Reply::getResponse($fullMappedOperator, Message::SUCCESS_OPERATOR_CREATE, 0, $serviceData);
    }


    /**
     * @Route("/edit-operator", name="edit_operator")
     * @param Request $request
     * @return JsonResponse
     */
    public function editOperator(Request $request)
    {
        $operatorClient = json_decode($request->request->get('operator'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($operatorClient->id);
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }
        $entOperator->setClientData($em, $operatorClient);
        $em->persist($entOperator);
        $em->flush();

        $fullMappedOperator = $entOperator->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_OPERATORS, ServiceData::ACTION_EDIT, $fullMappedOperator);
        return Reply::getResponse($fullMappedOperator, Message::SUCCESS_OPERATOR_EDIT, 0, $serviceData);
    }

    /**
     * @Route("/create-operator-contact", name="create_operator_contact")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createOperatorContact(Request $request)
    {
        $operatorContactClient = json_decode($request->request->get('operatorContact'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($operatorContactClient->operator_id);
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntOperatorContact $entOperatorContact */
        $entOperatorContact = new EntOperatorContact($em, $entOperator, $operatorContactClient->lastname);
        $entOperatorContact->setClientData($em, $operatorContactClient);
        $em->persist($entOperatorContact);
        $em->flush();

        $fullMappedOperatorContact = $entOperatorContact->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_OPERATOR_CONTACTS, ServiceData::ACTION_ADD, $fullMappedOperatorContact);
        return Reply::getResponse($entOperator->clientMapper($em, true), Message::SUCCESS_OPERATOR_CONTACT_CREATE, 0, $serviceData);
    }

    /**
     * @Route("/edit-operator-contact", name="edit_operator_contact")
     * @param Request $request
     * @return JsonResponse
     */
    public function editOperatorContact(Request $request)
    {
        $operatorContactClient = json_decode($request->request->get('operatorContact'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntOperatorContact $entOperatorContact */
        $entOperatorContact = $em->getRepository('App:EntOperatorContact')->find($operatorContactClient->id);
        if (is_null($entOperatorContact)) {
            return Reply::getErrorResponse(Message::ERROR_NON_EXISTING_DATA);
        }

        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($entOperatorContact->getOperatorId());
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entOperatorContact->setClientData($em, $operatorContactClient);
        $em->persist($entOperatorContact);
        $em->flush();

        $fullMappedOperatorContact = $entOperatorContact->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_OPERATOR_CONTACTS, ServiceData::ACTION_EDIT, $fullMappedOperatorContact);
        return Reply::getResponse($entOperator->clientMapper($em, true), Message::SUCCESS_OPERATOR_CONTACT_EDIT, 0, $serviceData);
    }

    /**
     * @Route("/delete-operator-contact", name="delete_operator_contact")
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteOperatorContact(Request $request)
    {
        $operatorContactIdClient = json_decode($request->request->get('operatorContactId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntOperatorContact $entOperatorContact */
        $entOperatorContact = $em->getRepository('App:EntOperatorContact')->find($operatorContactIdClient);
        if (is_null($entOperatorContact)) {
            return Reply::getErrorResponse(Message::ERROR_NON_EXISTING_DATA);
        }

        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($entOperatorContact->getOperatorId());
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }
        $fullMappedOperatorContact = $entOperatorContact->clientMapper($em, true);
        $em->remove($entOperatorContact);
        $em->flush();

        $sql = "UPDATE `operator_meeting` SET `operator_contact_id` = NULL WHERE `operator_contact_id` = ". $operatorContactIdClient;
        $statement = $em->getConnection()->prepare($sql);
        $statement->execute();

        $sql = "UPDATE `operator_note` SET `operator_contact_id` = NULL WHERE `operator_contact_id` = ". $operatorContactIdClient;
        $statement = $em->getConnection()->prepare($sql);
        $statement->execute();

        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_OPERATOR_CONTACTS, ServiceData::ACTION_DELETE, $fullMappedOperatorContact);
        return Reply::getResponse($entOperator->clientMapper($em, true), Message::SUCCESS_OPERATOR_CONTACT_DELETE, 0, $serviceData);
    }

    /**
     * @Route("/create-operator-note", name="create_operator_note")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createOperatorNote(Request $request)
    {
        $operatorNoteClient = json_decode($request->request->get('operatorNote'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();
        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($operatorNoteClient->operator_id);
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntOperatorNote $entOperatorNote */
        $entOperatorNote = new EntOperatorNote($em, $entOperator, $user, $operatorNoteClient->title);
        $entOperatorNote->setClientData($em, $operatorNoteClient);
        $em->persist($entOperatorNote);
        $em->flush();

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

    /**
     * @Route("/edit-operator-note", name="edit_operator_note")
     * @param Request $request
     * @return JsonResponse
     */
    public function editOperatorNote(Request $request)
    {
        $operatorNoteClient = json_decode($request->request->get('operatorNote'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();
        /** @var EntOperatorNote $entOperatorNote */
        $entOperatorNote = $em->getRepository('App:EntOperatorNote')->find($operatorNoteClient->id);
        if (is_null($entOperatorNote) || $entOperatorNote->getCreationUserId() != $user->getId()) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($entOperatorNote->getOperatorId());
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entOperatorNote->setClientData($em, $operatorNoteClient);
        $em->persist($entOperatorNote);
        $em->flush();

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

    /**
     * @Route("/delete-operator-note", name="delete_operator_note")
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteOperatorNote(Request $request)
    {
        $operatorNoteIdClient = json_decode($request->request->get('operatorNoteId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();

        /** @var EntOperatorNote $entOperatorNote */
        $entOperatorNote = $em->getRepository('App:EntOperatorNote')->find($operatorNoteIdClient);
        $bHasRightsToDelete = $entOperatorNote->getCreationUserId() == $user->getId() || $user->isAdmin();
        if (is_null($entOperatorNote) || !$bHasRightsToDelete) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntOperator $entOperator */
        $entOperator = $em->getRepository('App:EntOperator')->find($entOperatorNote->getOperatorId());
        if (is_null($entOperator)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $em->remove($entOperatorNote);
        $em->flush();

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