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

namespace Magediary\ProductQuestion\Model\Email;

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

class MailAnswer extends Mail implements MailAnswerInterface
{
    /**
     * Send notification email to the customer
     *
     * @param mixed $data
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\MailException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function sendCustomer($data)
    {
        if ($this->_helperData->getConfig('question/notification_answer/customer_enabled')) {
            $this->inlineTranslation->suspend();
            try {
                /** @var Question $question */
                $question = $this->questionFactory->create()->load(
                    $data->getQuestionId()
                );

                /** @var Answer $answer */
                $answer = $this->answerFactory->create()->load(
                    $data->getId()
                );

                if ($question->getProductId()) {
                    $product = $this->productRepository->getById($question->getProductId());
                    $product->setStoreId($question->getStoreId());

                    $variables['product'] = $product;
                    $variables['product_view'] = $product->getProductUrl();
                }

                $variables['answer'] = $answer;
                $variables['question'] = $question;

                if ($question->getEmail()) {
                    $transport = $this->transportBuilder
                        ->setTemplateIdentifier(
                            $this->_helperData->getConfig('question/notification_answer/customer_template')
                        )
                        ->setTemplateOptions(
                            [
                                'area' => Area::AREA_FRONTEND,
                                'store' => $this->storeManager->getStore()->getId()
                            ]
                        )
                        ->setTemplateVars($variables)
                        ->setFrom($this->_helperData->getConfig('question/notification_answer/customer_email_identity'))
                        ->addTo($question->getEmail())
                        ->getTransport();

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

    /**
     * Send email on submit a new answer
     *
     * @param mixed $data
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\MailException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function sendAdmin($data)
    {
        if ($this->_helperData->getConfig('question/notification_answer/admin_enabled')) {
            $this->inlineTranslation->suspend();
            try {
                /** @var Question $question */
                $question = $this->questionFactory->create()->load(
                    $data->getQuestionId()
                );

                /** @var Answer $answer */
                $answer = $this->answerFactory->create()->load(
                    $data->getId()
                );

                if ($question->getProductId()) {
                    $product = $this->productRepository->getById($question->getProductId());
                    $product->setStoreId($question->getStoreId());

                    $variables['product'] = $product;
                    $variables['product_url'] = $this->backendHelper->getUrl(
                        'catalog/product/edit',
                        ['id' => $product->getId()]
                    );
                    $variables['product_view'] = $product->getProductUrl();
                }

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

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

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

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