<?php

namespace Lc\ShopBundle\Services ;

use App\Entity\TicketMessage;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderShopInterface;
use Lc\ShopBundle\Context\TicketInterface;
use Lc\ShopBundle\Context\TicketMessageInterface;
use Lc\ShopBundle\Model\Ticket;

class TicketUtils
{
        protected $em ;
        protected $merchantUtils ;
        protected $mailUtils ;

        public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, MailUtils $mailUtils)
        {
                $this->em = $em ;
                $this->merchantUtils = $merchantUtils ;
                $this->mailUtils = $mailUtils ;
        }

        public function createTicket($params): TicketInterface
        {
                $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName() ;
                $ticket = new $classTicket ;
                $ticket->setMerchant($this->merchantUtils->getMerchantCurrent()) ;
                $ticket->setStatus(1) ;

                if(isset($params['user'])) {
                        $ticket->setUser($params['user']) ;

                        $email = $params['user']->getEmail() ;
                        $firstname = $params['user']->getFirstname() ;
                }
                else {
                        $ticket->setVisitorFirstname($params['visitorFirstname']) ;
                        $ticket->setVisitorLastname($params['visitorLastname']) ;
                        $ticket->setVisitorEmail($params['visitorEmail']) ;
                        $ticket->setVisitorToken(uniqid()) ;

                        $email = $params['visitorEmail'] ;
                        $firstname = $params['visitorFirstname'] ;
                }

                $ticket->setStatus(Ticket::TICKET_STATUS_OPEN) ;
                $ticket->setType($params['type']) ;
                if(isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
                        $ticket->setOrderShop($params['orderShop']) ;
                }
                $ticket->setSubject($params['subject']) ;
                $this->em->persist($ticket);

                $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
                $ticketMessage = new $classTicketMessage ;
                $ticketMessage->setStatus(1) ;
                $ticketMessage->setTicket($ticket) ;
                $ticketMessage->setMessage($params['message']) ;
                $this->em->persist($ticketMessage);

                $this->em->flush() ;

                // envoi email au client
                $this->mailUtils->send([
                        MailUtils::SUBJECT => 'Nouvelle demande',
                        MailUtils::TO_EMAIL => $email,
                        MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
                        MailUtils::CONTENT_DATA =>  [
                                'firstname' => $firstname,
                                'ticket' => $ticket
                        ],
                ]) ;

                return $ticket ;
        }

        public function createTicketMessage($params)
        {
                $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
                $ticketMessage = new $classTicketMessage ;

                $ticket = $params['ticket'] ;

                $ticketMessage->setStatus(1) ;
                $ticketMessage->setTicket($ticket) ;
                $ticketMessage->setMessage($params['message']) ;
                if(isset($params['answerByAdmin']) && $params['answerByAdmin']) {
                        $ticketMessage->setAnswerByAdmin($params['answerByAdmin']) ;

                        // envoi email au client
                        $this->mailUtils->send([
                                MailUtils::SUBJECT => 'Réponse à votre demande',
                                MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail() : $ticket->getVisitorEmail(),
                                MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
                                MailUtils::CONTENT_DATA =>  [
                                        'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname() : $ticket->getVisitorFirstname(),
                                        'ticket' => $ticket
                                ],
                        ]) ;
                }
                $this->em->persist($ticketMessage);

                if(isset($params['closeTicket']) && $params['closeTicket']) {
                        $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED) ;
                }

                $ticket->setUpdatedAt(new \DateTime()) ;
                $this->em->persist($ticket);

                $this->em->flush() ;

                return $ticketMessage ;
        }
}

