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

use App\Entity\EntCustomer;
use App\Entity\EntCustomerContact;
use App\Entity\EntCustomerMeeting;
use App\Entity\EntCustomerMeetingParticipant;
use App\Entity\EntCustomerNote;
use App\Entity\EntMeetingType;
use App\Entity\EntUser;
use App\EntityVirtual\ServiceData;
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\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class CustomerEditController
 * @package App\Controller
 * @IsGranted({"ROLE_SALES", "ROLE_ADMIN"})
 */
class CustomerEditController extends AbstractController
{
    /**
     * @Route("/create-customer", name="create_customer")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createCustomer(Request $request)
    {
        $customerClient = json_decode($request->request->get('customer'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntCustomer $entCustomer */
        $entCustomer = new EntCustomer($em, $customerClient->name);
        $entCustomer->setClientData($em, $customerClient);
        $em->persist($entCustomer);
        $em->flush();

        $fullMappedCustomer = $entCustomer->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_CUSTOMERS, ServiceData::ACTION_ADD, $fullMappedCustomer);
        return Reply::getResponse($fullMappedCustomer, Message::SUCCESS_CUSTOMER_CREATE, 0, $serviceData);
    }


    /**
     * @Route("/edit-customer", name="edit_customer")
     * @param Request $request
     * @return JsonResponse
     */
    public function editCustomer(Request $request)
    {
        $customerClient = json_decode($request->request->get('customer'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($customerClient->id);
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }
        $entCustomer->setClientData($em, $customerClient);
        $em->persist($entCustomer);
        $em->flush();

        $fullMappedCustomer = $entCustomer->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_CUSTOMERS, ServiceData::ACTION_EDIT, $fullMappedCustomer);
        return Reply::getResponse($fullMappedCustomer, Message::SUCCESS_CUSTOMER_EDIT, 0, $serviceData);
    }

    /**
     * @Route("/create-customer-contact", name="create_customer_contact")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createCustomerContact(Request $request)
    {
        $customerContactClient = json_decode($request->request->get('customerContact'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($customerContactClient->customer_id);
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntCustomerContact $entCustomerContact */
        $entCustomerContact = new EntCustomerContact($em, $entCustomer, $customerContactClient->lastname);
        $entCustomerContact->setClientData($em, $customerContactClient);
        $em->persist($entCustomerContact);
        $em->flush();

        $fullMappedCustomerContact = $entCustomerContact->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_CUSTOMER_CONTACTS, ServiceData::ACTION_ADD, $fullMappedCustomerContact);
        return Reply::getResponse($entCustomer->clientMapper($em, true), Message::SUCCESS_CUSTOMER_CONTACT_CREATE, 0, $serviceData);
    }

    /**
     * @Route("/edit-customer-contact", name="edit_customer_contact")
     * @param Request $request
     * @return JsonResponse
     */
    public function editCustomerContact(Request $request)
    {
        $customerContactClient = json_decode($request->request->get('customerContact'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntCustomerContact $entCustomerContact */
        $entCustomerContact = $em->getRepository('App:EntCustomerContact')->find($customerContactClient->id);
        if (is_null($entCustomerContact)) {
            return Reply::getErrorResponse(Message::ERROR_NON_EXISTING_DATA);
        }

        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($entCustomerContact->getCustomerId());
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entCustomerContact->setClientData($em, $customerContactClient);
        $em->persist($entCustomerContact);
        $em->flush();

        $fullMappedCustomerContact = $entCustomerContact->clientMapper($em, true);
        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_CUSTOMER_CONTACTS, ServiceData::ACTION_EDIT, $fullMappedCustomerContact);
        return Reply::getResponse($entCustomer->clientMapper($em, true), Message::SUCCESS_CUSTOMER_CONTACT_EDIT, 0, $serviceData);
    }

    /**
     * @Route("/delete-customer-contact", name="delete_customer_contact")
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteCustomerContact(Request $request)
    {
        $customerContactIdClient = json_decode($request->request->get('customerContactId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntCustomerContact $entCustomerContact */
        $entCustomerContact = $em->getRepository('App:EntCustomerContact')->find($customerContactIdClient);
        if (is_null($entCustomerContact)) {
            return Reply::getErrorResponse(Message::ERROR_NON_EXISTING_DATA);
        }

        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($entCustomerContact->getCustomerId());
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }
        $fullMappedCustomerContact = $entCustomerContact->clientMapper($em, true);
        $em->remove($entCustomerContact);
        $em->flush();

        $sql = "UPDATE `customer_meeting` SET `customer_contact_id` = NULL WHERE `customer_contact_id` = ". $customerContactIdClient;
        $statement = $em->getConnection()->prepare($sql);
        $statement->execute();

        $sql = "UPDATE `customer_note` SET `customer_contact_id` = NULL WHERE `customer_contact_id` = ". $customerContactIdClient;
        $statement = $em->getConnection()->prepare($sql);
        $statement->execute();

        $serviceData = new ServiceData();
        $serviceData->addServiceData(ServiceData::SERVICE_DATA_TYPE_CUSTOMER_CONTACTS, ServiceData::ACTION_DELETE, $fullMappedCustomerContact);
        return Reply::getResponse($entCustomer->clientMapper($em, true), Message::SUCCESS_CUSTOMER_CONTACT_DELETE, 0, $serviceData);
    }

    /**
     * @Route("/create-customer-note", name="create_customer_note")
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function createCustomerNote(Request $request)
    {
        $customerNoteClient = json_decode($request->request->get('customerNote'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();
        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($customerNoteClient->customer_id);
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntCustomerNote $entCustomerNote */
        $entCustomerNote = new EntCustomerNote($em, $entCustomer, $user, $customerNoteClient->title);
        $entCustomerNote->setClientData($em, $customerNoteClient);
        $em->persist($entCustomerNote);
        $em->flush();

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

    /**
     * @Route("/edit-customer-note", name="edit_customer_note")
     * @param Request $request
     * @return JsonResponse
     */
    public function editCustomerNote(Request $request)
    {
        $customerNoteClient = json_decode($request->request->get('customerNote'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();
        /** @var EntCustomerNote $entCustomerNote */
        $entCustomerNote = $em->getRepository(EntCustomerNote::class)->find($customerNoteClient->id);
        $bHasRightsToEdit = $entCustomerNote->getCreationUserId() == $user->getId() || $user->isAdmin();
        if (is_null($entCustomerNote) || !$bHasRightsToEdit) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository(EntCustomer::class)->find($entCustomerNote->getCustomerId());
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        $entCustomerNote->setClientData($em, $customerNoteClient);
        $em->persist($entCustomerNote);
        $em->flush();

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

    /**
     * @Route("/delete-customer-note", name="delete_customer_note")
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteCustomerNote(Request $request)
    {
        $customerNoteIdClient = json_decode($request->request->get('customerNoteId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntUser $user */
        $user = $this->getUser();

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

        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($entCustomerNote->getCustomerId());
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

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

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