<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Observer\Frontend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class BeforeGoToCart implements ObserverInterface
{
    protected $httpContext;
    /**
     * @var \Magento\Framework\App\ResponseFactory
     */
    private $responseFactory;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;
    private $redirect;
    protected $messageManager;
    protected $customerSession;
    public function __construct(
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->httpContext = $httpContext;
        $this->responseFactory = $responseFactory;
        $this->url = $url;
        $this->redirect = $redirect;
        $this->messageManager = $messageManager;
        $this->customerSession = $customerSession;
    }

    public function execute(Observer $observer)
    {

       if($this->checkIsLogin()) {

       }
       else {
         $controller = $observer->getControllerAction();
           $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
       }
        $isPlaceOrderDesign = $this->customerSession->getPlacePreviewOrderSuccess();
        if($isPlaceOrderDesign == 1) {
            $this->messageManager->addSuccessMessage(__('Order placed successfully'));
            $this->customerSession->setPlacePreviewOrderSuccess(0);
        }
        return $this;
    }
    protected function checkIsLogin() {
        $isLogin = false;
        if($this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)) {
            $isLogin = true;
        }
        return $isLogin;
    }
}
