<?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\Controller\Account;

use Magento\Framework\App\Action;
class CustomOrders extends Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $pageFactory;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $httpContext;
    protected $storeManager;


    public function __construct(
        Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        parent::__construct($context);
        $this->pageFactory = $pageFactory;
        $this->httpContext = $httpContext;
        $this->storeManager = $storeManager;
    }

    public function execute()
    {
        $resultPageFactory = $this->pageFactory->create();
        // Add page title
        $resultPageFactory->getConfig()->getTitle()->set(__('Orders'));
        // Add breadcrumb
        /** @var \Magento\Theme\Block\Html\Breadcrumbs */
        if($resultPageFactory->getLayout()->getBlock('breadcrumbs'))
        {
            $breadcrumbs = $resultPageFactory->getLayout()->getBlock('breadcrumbs');
            $breadcrumbs->addCrumb('home',
                [
                    'label' => __('Home'),
                    'title' => __('Home'),
                    'link' => $this->_url->getUrl('')
                ]
            );
            $breadcrumbs->addCrumb('preview_project_order',
                [
                    'label' => __('Orders'),
                    'title' => __('Orders')
                ]
            );
        }
        $isLoggedIn = $this->checkIsLogin();
        if(!$isLoggedIn) {
            $this->messageManager->addErrorMessage(__('Please login to see design'));
            $urlLogin = $this->getRefererUrl();
            return $this->resultRedirectFactory->create()->setUrl($urlLogin);
        }
        return $resultPageFactory;
    }
    public function getRefererUrl() {

        $url =  $this->storeManager->getStore()->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
        return  $this->storeManager->getStore()->getUrl('customer/account/login', array('referer' => base64_encode($url)));
    }
    protected function checkIsLogin() {
        $isLogin = false;
        if($this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)) {
            $isLogin = true;
        }
        return $isLogin;
    }
}
