<?php

namespace NPS\Factory;

use NPS\Model\User;
use NPS\Entity\UserEntity;
use NPS\Entity\IEntity;
use Doctrine\ORM\EntityManagerInterface;
use NPS\Etc\Strings;
use NPS\DependencyInjection\Mailer;
use Symfony\Component\HttpFoundation\RequestStack;
use NPS\Entity\ErrorReportingQueryEntity;
use NPS\Model\ErrorReportingQuery;
use NPS\Model\UserQuestionQuery;
use NPS\Model\AbstractUserQuery;
use NPS\Model\File;
use NPS\Entity\UserQuestionQueryEntity;


/**
 * User query factory 
 * @author Lukas Korinek
 */
class UserQueryFactory implements IObjectFactory
{
    use TMemorizedFactory;

    const USER_QUERY_ERROR_REPORTING = 'error_reporting';
    const USER_QUERY_QUESTION = 'question';

    /** @var EntityManagerInterface $em */
    protected $em;

    /** @var UserFactory $userFactory */
    protected $userFactory;

    /** @var FileFactory $fileFactory */
    protected $fileFactory;

    /** @var Mailer $mailer */
    protected $mailer;

    /** @var string $emailAdmin */
    protected $emailAdmin;

    public function __construct(EntityManagerInterface $em, UserFactory $userFactory, FileFactory $fileFactory, Mailer $mailer, $emailAdmin)
    {
        $this->em = $em;
        $this->userFactory = $userFactory;
        $this->fileFactory = $fileFactory;
        $this->mailer = $mailer;
        $this->emailAdmin = $emailAdmin;
    }

    /**
     * @inheritdoc
     */
    public function make(IEntity $entity)
    {
        return $this->load($entity, function($entity)
        {
            if(!$entity->getId())
            {
                $this->em->persist($entity);
            }

            if($entity instanceof ErrorReportingQueryEntity)
            {
                $user = $entity->user ? $this->userFactory->make($entity->user) : null;
                $attachment = $entity->attachment ? $this->fileFactory->make($entity->attachment) : null;
                return new ErrorReportingQuery($entity, $user, $attachment);
            }
            else if($entity instanceof UserQuestionQueryEntity)
            {
                return new UserQuestionQuery($entity);
            }
            
            throw new \InvalidArgumentException('The entity ' . get_class($entity) . ' is not a valid entity for UserQueryFactory');
        });
    }

    
    /**
     * Creates a user query
     * @param int $type Target user query type
     * @param string $text
     * @param string $email
     * @param User $user
     * @param File $attachment
     * @param array $history
     * @return AbstractUserQuery
     */
    public function create($type, $text, $email = null, User $user = null, File $attachment = null, array $history = null)
    {
        switch($type)
        {
            case self::USER_QUERY_ERROR_REPORTING:
            {
                $entity = ErrorReportingQueryEntity::create([
                    'text' => $text,
                    'user' => $user ? $user->getEntity() : null,
                    'email' => $email,
                    'attachment' => $attachment ? $attachment->getEntity() : null,
                    'history' => implode(';', $history)
                ]);
                
                // Create an email
                $this->mailer->mail(
                    $this->emailAdmin,
                    'error_reporting',
                    'error.html.twig',
                    [
                        'email' => $user ? $user->getEmail() : $email,
                        'entity' => $entity,
                        'history' => $history
                    ]
                );

                break;
            }
            case self::USER_QUERY_QUESTION:
            {
                $entity = UserQuestionQueryEntity::create([
                    'email' => $email,
                    'text' => $text
                ]);

                // Create an email to admin
                $this->mailer->mail(
                    $this->emailAdmin,
                    'user_question',
                    'question.html.twig',
                    [
                        'email' => $email,
                        'entity' => $entity,
                    ]
                );

                break;
            }
            default: 
            {
                throw new \InvalidArgumentException('Invalid user query type passed');
            }
        }

        return $this->make($entity);
    }


}

