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

namespace Magediary\ProductQuestion\Model\Email;

use Magediary\ProductQuestion\Model\Data\Answer;
use Magediary\ProductQuestion\Model\Data\Question;

class MailAbuse extends Mail implements MailAbuseInterface
{
    /**
     * Send email to customer and admin on report abuse
     *
     * @param mixed $data
     * @return void
     */
    public function send($data)
    {
        $this->inlineTranslation->suspend();
        $variables = [];

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

            /** @var Question $question */
            $question = $this->questionFactory->create()->load(
                $answer->getQuestionId()
            );

            if ($data->getProductId()) {
                $product = $this->productRepository->getById($data->getProductId());
                $variables['product'] = $product;
                $variables['product_link'] = $this->backendHelper->getUrl(
                    'catalog/product/edit',
                    ['id' => $product->getId()]
                );
            }

            $variables['email'] = __('N/A');
            $customer_id = $data->getCustomerId();
            if ($customer_id) {
                $customer = $this->_customerFactory->create()->load($customer_id);
                $variables['email'] = $customer->getEmail();
                $variables['customer_url'] = $this->backendHelper->getUrl(
                    'customer/index/edit',
                    ['id' => $customer_id]
                );
            }
            $variables['answer'] = $answer;
            $variables['question'] = $question;
            $variables['answer_link'] = $this->backendHelper->getUrl(
                'magediary_productquestion/answer/edit',
                ['answer_id' => $answer->getAnswerId()]
            );
            $variables['question_link'] = $this->backendHelper->getUrl(
                'magediary_productquestion/question/edit',
                ['question_id' => $question->getQuestionId()]
            );

            $transport = $this->transportBuilder
                ->setTemplateIdentifier($this->_helperData->getConfig('question/report_abuse/email_template'))
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => $this->storeManager->getStore()->getId()
                    ]
                )
                ->setTemplateVars($variables)
                ->setFrom($this->_helperData->getConfig('question/report_abuse/sender_email_identity'))
                ->addTo($this->_helperData->getConfig('question/report_abuse/recipient_email'))
                ->getTransport();

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