<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Model\Email;

use Magediary\ProductQuestion\Model\Data\Question;
use Magento\Framework\App\Area;

class MailQuestion extends Mail implements MailQuestionInterface
{
    /**
     * Send email on submit a new question
     *
     * If the product id assigned to the question to avoid sending an email on submit from the backend
     *
     * @param mixed $data
     * @return void
     */
    public function send($data)
    {
        if ($this->_helperData->getConfig('question/notification_question/enabled') && $data->getProductId()) {
            $this->inlineTranslation->suspend();
            try {
                /** @var Question $question */
                $question = $this->questionFactory->create()->load($data->getId());
                $product = $this->productRepository->getById($question->getProductId());

                $email = $question->getEmail();
                $variables['email'] = $email ? $email : __('Guest');
                $customer_id = $question->getCustomerId();
                if ($customer_id && !$email) {
                    $customer = $this->_customerFactory->create()->load($customer_id);
                    $variables['email'] = $customer->getEmail();
                }

                $variables['nickname'] = $question->getNickname();
                $variables['question'] = $question;
                $variables['product'] = $product;
                $variables['question_url'] = $this->backendHelper->getUrl(
                    'magediary_productquestion/question/edit',
                    ['question_id' => $question->getQuestionId()]
                );
                $variables['product_url'] = $this->backendHelper->getUrl(
                    'catalog/product/edit',
                    ['id' => $product->getId()]
                );
                if ($question->getCustomerId()) {
                    $variables['customer_url'] = $this->backendHelper->getUrl(
                        'customer/index/edit',
                        ['id' => $question->getCustomerId()]
                    );
                }

                $transport = $this->transportBuilder
                    ->setTemplateIdentifier(
                        $this->_helperData->getConfig('question/notification_question/admin_template')
                    )
                    ->setTemplateOptions(
                        [
                            'area' => Area::AREA_FRONTEND,
                            'store' => $this->storeManager->getStore()->getId()
                        ]
                    )
                    ->setTemplateVars($variables)
                    ->setFrom($this->_helperData->getConfig('question/notification_question/admin_email_identity'))
                    ->addTo($this->_helperData->getConfig('question/notification_question/recipient_email'))
                    ->getTransport();

                $transport->sendMessage();
            } finally {
                $this->inlineTranslation->resume();
            }
        }
    }
}
