<?php

namespace Sibs\Sibs\Helper;

/*
 * Custom Module Email helper
 */

use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Sibs\Sibs\Utils\SibsLog;

class Email extends AbstractHelper
{
    /* Here section and group refer to name of section and group where you create this field in configuration*/

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * Store manager.
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $_inlineTranslation;

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

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

    /**
     * @param Magento\Framework\App\Helper\Context $context
     * @param Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param Magento\Store\Model\StoreManagerInterface $storeManager
     * @param Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
     * @param Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param string $template
     */
    public function __construct(
        Context $context,
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager,
        StateInterface $inlineTranslation,
        TransportBuilder $transportBuilder,
        string $template
    ) {
        $this->_scopeConfig       = $scopeConfig;
        $this->_storeManager      = $storeManager;
        $this->_inlineTranslation = $inlineTranslation;
        $this->_transportBuilder  = $transportBuilder;
        $this->setTemplateId($template);

        parent::__construct($context);
    }

    /**
     * Return store configuration value of your template field that which id you set for template.
     *
     * @param string $path
     * @param int $storeId
     * @return mixed
     */
    protected function getConfigValue($path, $storeId)
    {
        return $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
    }

    /**
     * Return store.
     *
     * @return int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }

    /**
     * Return template id according to store.
     *
     * @return mixed
     */
    public function setTemplateId($xmlPath)
    {
        $this->temp_id = $this->getConfigValue($xmlPath, $this->getStoreId());
    }

    /**
     * Return template id according to store.
     *
     * @return mixed
     */
    public function getTemplateId()
    {
        return $this->temp_id;
    }

    /**
     * [generateTemplate description]  with template file and tempaltes variables values.
     * @param mixed $emailVariables
     * @param mixed $senderInfo
     * @param mixed $receiverInfo
     * @return object
     */
    public function generateTemplate($emailVariables, $senderInfo, $receiverInfo)
    {
        return $this->_transportBuilder->setTemplateIdentifier($this->getTemplateId(), ScopeInterface::SCOPE_STORE)
            ->setTemplateOptions([
                'area'  => Area::AREA_FRONTEND,
                'store' => $this->getStoreId(),
            ])
            ->setTemplateVars($emailVariables)
            ->setFrom($senderInfo)
            ->addTo($receiverInfo['email'], $receiverInfo['name']);
    }

    /**
     * [sendInvoicedOrderEmail description].
     * @param mixed $emailVariables
     * @param mixed $senderInfo
     * @param mixed $receiverInfo
     * @return void
     */
    public function customMailSendMethod($emailVariables, $senderInfo, $receiverInfo)
    {
        try {
            $this->_inlineTranslation->suspend();

            $this->generateTemplate($emailVariables, $senderInfo, $receiverInfo)
                ->getTransport()
                ->sendMessage();

            $this->_inlineTranslation->resume();
        } catch (\Exception $e) {
            SibsLog::warn($e->getMessage());
        }
    }
}
