<?php


namespace Magetop\Advancepreview\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;

class Email extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var StateInterface
     */
    protected $inlineTranslation;
    /**
     * @var Escaper
     */
    protected $escaper;
    /**
     * @var TransportBuilder
     */
    protected $transportBuilder;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    protected $emailSettingFactory;
    protected $storeManager;

    public function __construct(
        Context $context,
        StateInterface $inlineTranslation,
        Escaper $escaper,
        TransportBuilder $transportBuilder,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magetop\Advancepreview\Model\ResourceModel\EmaiSetting\CollectionFactory $emailSettingFactory
    ) {
        parent::__construct($context);
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->logger = $context->getLogger();
        $this->emailSettingFactory = $emailSettingFactory;
        $this->storeManager = $storeManager;
    }

    /**
     * @param string $emailId
     * @param array $tempalteVars
     * @param string $name
     * @param string $fromEmail
     * @param string $toEmail
     * @return array
     */
    public function sendEmail($emailId, $tempalteVars, $name, $fromEmail, $toEmail)
    {
        $response = array(
          'status'=>'error',
          'message'=>'error'
        );
        try {
            $this->inlineTranslation->suspend();
            $sender = [
                'name' => $this->escaper->escapeHtml('Myfitmix'),
                'email' => $this->escaper->escapeHtml($fromEmail),
            ];
            $transport = $this->transportBuilder
                ->setTemplateIdentifier($emailId)
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DISTRO_STORE_ID,
                    ]
                )
                ->setTemplateVars($tempalteVars)
                ->setFrom($sender)
                ->addTo($toEmail)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
            $response['status'] = 'success';
            $response['message'] = 'send success';
        } catch (\Exception $e) {
            $response['message'] = $e->getMessage();
            $this->logger->debug($e->getMessage());

        }
        return  $response;
    }
    public function sendAdvanceEmail($emailId, $tempalteVars, $name, $fromEmail, $toEmail, $storeId = null)
    {

        $response = array(
            'status'=>'error',
            'message'=>'error'
        );
        try {
			if($storeId == null) {
				  $storeId = $this->storeManager->getStore()->getId();
			}

            $this->inlineTranslation->suspend();
            $sender = [
                'name' => $this->escaper->escapeHtml($name),
                'email' => $this->escaper->escapeHtml($fromEmail),
            ];
            $transport = $this->transportBuilder
                ->setTemplateIdentifier($emailId)
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => $storeId,
                    ]
                )
                ->setTemplateVars($tempalteVars)
                ->setFrom($sender)
                ->addTo($toEmail)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
            $response['status'] = 'success';
            $response['message'] = 'send success';
        } catch (\Exception $e) {
            $response['message'] = $e->getMessage();
            $this->logger->debug($e->getMessage());

        }
        return  $response;
    }
    public function sendEmailAdmin($emailId, $tempalteVars, $fromEmail, $toEmail)
    {
        $response = array(
          'status'=>'error',
          'message'=>'error'
        );
        try {
            $this->inlineTranslation->suspend();
            $sender = [
                'name' => $this->escaper->escapeHtml('Myfitmix'),
                'email' => $this->escaper->escapeHtml($fromEmail),
            ];
            $transport = $this->transportBuilder
                ->setTemplateIdentifier($emailId)
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_ADMINHTML,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars($tempalteVars)
                ->setFrom($sender)
                ->addTo($toEmail)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
            $response['status'] = 'success';
            $response['message'] = 'send success';
        } catch (\Exception $e) {
            $response['message'] = $e->getMessage();
            $this->logger->debug($e->getMessage());

        }
        return  $response;
    }
    public function checkIsSendEmail($status,$customerId = 0) {
        $collection = $this->emailSettingFactory->create()
            ->addFieldToFilter('customer_id',$customerId);
      /*  $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom_email.log');
        $logger = new \Zend_Log();
        $logger->addWriter($writer);
        $sql = (string)$collection->getSelect();
        $logger->info($status. ' -- '. $sql);*/
        $item = $collection && $collection->getSize() > 0 ? $collection->getFirstItem()->getData() : [];
//        $isOK = false;
        return isset($item[$status]) && $item[$status] == 1 ? true : false;
    }
}
