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

use App\Entity\EntProduction;
use App\Entity\EntProductionContact;
use App\Entity\EntProductionNote;
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 ProductionEditController
 * @package App\Controller
 * @IsGranted({"ROLE_PRODUCTION", "ROLE_TECHNIQUE", "ROLE_SERVICE", "ROLE_ADMIN"})
 */
class ProductionEditController extends AbstractController
{
    /**
     * @Route("/create-production", name="create_production")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createProduction(Request $request)
    {
        $productionClient = json_decode($request->request->get('production'));
        $em = $this->getDoctrine()->getManager();
        $entProduction = new EntProduction($em, $productionClient->name);
        $entProduction->setClientData($em, $productionClient);
        $em->persist($entProduction);
        $em->flush();

        $fullMappedProduction = $entProduction->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_PRODUCTIONS, ServiceData::ACTION_ADD, $fullMappedProduction);
        return Reply::getResponse($fullMappedProduction, Message::SUCCESS_PRODUCTION_CREATE, 0, $serviceData);
    }


    /**
     * @Route("/edit-production", name="edit_production")
     * @param Request $request
     * @return JsonResponse
     */
    public function editProduction(Request $request)
    {
        $productionClient = json_decode($request->request->get('production'));
        $em = $this->getDoctrine()->getManager();
        $entProduction = $em->getRepository(EntProduction::class)->find($productionClient->id);
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }
        $entProduction->setClientData($em, $productionClient);
        $em->persist($entProduction);
        $em->flush();

        $fullMappedProduction = $entProduction->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_PRODUCTIONS, ServiceData::ACTION_EDIT, $fullMappedProduction);
        return Reply::getResponse($fullMappedProduction, Message::SUCCESS_PRODUCTION_EDIT, 0, $serviceData);
    }

    /**
     * @Route("/create-production-contact", name="create_production_contact")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createProductionContact(Request $request)
    {
        $productionContactClient = json_decode($request->request->get('productionContact'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($productionContactClient->production_id);
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entProductionContact = new EntProductionContact($em, $entProduction, $productionContactClient->lastname);
        $entProductionContact->setClientData($em, $productionContactClient);
        $em->persist($entProductionContact);
        $em->flush();

        $fullMappedProductionContact = $entProductionContact->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_PRODUCTION_CONTACTS, ServiceData::ACTION_ADD, $fullMappedProductionContact);
        return Reply::getResponse($entProduction->clientMapper($em, true), Message::SUCCESS_PRODUCTION_CONTACT_CREATE, 0, $serviceData);
    }

    /**
     * @Route("/edit-production-contact", name="edit_production_contact")
     * @param Request $request
     * @return JsonResponse
     */
    public function editProductionContact(Request $request)
    {
        $productionContactClient = json_decode($request->request->get('productionContact'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntProductionContact $entProductionContact */
        $entProductionContact = $em->getRepository(EntProductionContact::class)->find($productionContactClient->id);
        if (is_null($entProductionContact)) {
            return Reply::getErrorResponse(Message::ERROR_NON_EXISTING_DATA);
        }

        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($entProductionContact->getProductionId());
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entProductionContact->setClientData($em, $productionContactClient);
        $em->persist($entProductionContact);
        $em->flush();

        $fullMappedProductionContact = $entProductionContact->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_PRODUCTION_CONTACTS, ServiceData::ACTION_EDIT, $fullMappedProductionContact);
        return Reply::getResponse($entProduction->clientMapper($em, true), Message::SUCCESS_PRODUCTION_CONTACT_EDIT, 0, $serviceData);
    }

    /**
     * @Route("/delete-production-contact", name="delete_production_contact")
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteProductionContact(Request $request)
    {
        $productionContactIdClient = json_decode($request->request->get('productionContactId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntProductionContact $entProductionContact */
        $entProductionContact = $em->getRepository(EntProductionContact::class)->find($productionContactIdClient);
        if (is_null($entProductionContact)) {
            return Reply::getErrorResponse(Message::ERROR_NON_EXISTING_DATA);
        }

        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($entProductionContact->getProductionId());
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }
        $fullMappedProductionContact = $entProductionContact->clientMapper($em, true);
        $em->remove($entProductionContact);
        $em->flush();

        $sql = "UPDATE `production_meeting` SET `production_contact_id` = NULL WHERE `production_contact_id` = ". $productionContactIdClient;
        $statement = $em->getConnection()->prepare($sql);
        $statement->execute();

        $sql = "UPDATE `production_note` SET `production_contact_id` = NULL WHERE `production_contact_id` = ". $productionContactIdClient;
        $statement = $em->getConnection()->prepare($sql);
        $statement->execute();

        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_PRODUCTION_CONTACTS, ServiceData::ACTION_DELETE, $fullMappedProductionContact);
        return Reply::getResponse($entProduction->clientMapper($em, true), Message::SUCCESS_PRODUCTION_CONTACT_DELETE, 0, $serviceData);
    }

    /**
     * @Route("/create-production-note", name="create_production_note")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createProductionNote(Request $request)
    {
        $productionNoteClient = json_decode($request->request->get('productionNote'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();
        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($productionNoteClient->production_id);
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entProductionNote = new EntProductionNote($em, $entProduction, $user, $productionNoteClient->title);
        $entProductionNote->setClientData($em, $productionNoteClient);
        $em->persist($entProductionNote);
        $em->flush();

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

    /**
     * @Route("/edit-production-note", name="edit_production_note")
     * @param Request $request
     * @return JsonResponse
     */
    public function editProductionNote(Request $request)
    {
        $productionNoteClient = json_decode($request->request->get('productionNote'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();
        /** @var EntProductionNote $entProductionNote */
        $entProductionNote = $em->getRepository(EntProductionNote::class)->find($productionNoteClient->id);
        if (is_null($entProductionNote) || $entProductionNote->getCreationUserId() != $user->getId()) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($entProductionNote->getProductionId());
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entProductionNote->setClientData($em, $productionNoteClient);
        $em->persist($entProductionNote);
        $em->flush();

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

    /**
     * @Route("/delete-production-note", name="delete_production_note")
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteProductionNote(Request $request)
    {
        $productionNoteIdClient = json_decode($request->request->get('productionNoteId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();

        /** @var EntProductionNote $entProductionNote */
        $entProductionNote = $em->getRepository(EntProductionNote::class)->find($productionNoteIdClient);
        $bHasRightsToDelete = $entProductionNote->getCreationUserId() == $user->getId() || $user->isAdmin();
        if (is_null($entProductionNote) || !$bHasRightsToDelete) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($entProductionNote->getProductionId());
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

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

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