<?php
namespace Webkul\AdvanceSeoManager\Observer;

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Mail\Template\TransportBuilder;
use \Magento\Framework\Translate\Inline\StateInterface;
use \Magento\Store\Model\StoreManagerInterface;

class Pageobserver implements ObserverInterface
{
    const XML_PATH_EMAIL_RECIPIENT = 'seomanager/emails/recipient_email';
    
     /**
     * Email template config path
     */
    const XML_PATH_EMAIL_TEMPLATE = 'seomanager/emails/email_template';
    
    /**
     * Sender email config path
     */
    const XML_PATH_EMAIL_SENDER = 'seomanager/emails/sender_email_identity';

    /**
     * @param ScopeConfigInterface $scopeConfig
     * @param StoreManagerInterface $storeManage
     * @param TransportBuilder $transportBuilder
     * @param StateInterface $inlineTranslation
     * @param \Magento\Framework\UrlInterface $urlInterface
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManage,
        TransportBuilder $transportBuilder,
        StateInterface $inlineTranslation,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->storeManage = $storeManage;
        $this->transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->urlInterface = $urlInterface;
        $this->messageManager = $messageManager;
    }

    public function execute(Observer $observer)
    {
        $id = $observer->getEvent()->getPage()->getIdentifier();
        $gloabal = $this->scopeConfig->getValue(
            \Magento\Cms\Helper\Page::XML_PATH_NO_ROUTE_PAGE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        if ($id == $gloabal) {
            try {
                $req = $observer->getEvent()->getControllerAction()->getRequest();
                $clientIp = $req->getClientIp();
                $currentUrl = $this->urlInterface->getCurrentUrl();
                $storeid = $this->storeManage->getStore()->getId();
    
                $this->inlineTranslation->suspend();
                $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
                $transport = $this->transportBuilder
                   ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                   ->setTemplateOptions(
                       [
                            'area' => 'frontend',
                            'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                        ]
                   )
                   ->setTemplateVars(['url' => $currentUrl, 'clientip' => $clientIp])
                   ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
                   ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                   ->getTransport();

                $transport->sendMessage();
            } catch (\Exception $e) {
                $this->messageManager->addError($e->getMessage());
            }
            $this->inlineTranslation->resume();
        }
    }
}
