<?php
/**
 * Copied from \Magebees\Ajaxaddtocart\Controller\Cart\Add
 * to show success message on product detail page 
 */
namespace MNM\Spitzner\Controller\Cart;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magebees\Ajaxaddtocart\Controller\Cart\Add
{
    public function execute()
    {
        $popup_block= $this->_objectManager->create('Magebees\Ajaxaddtocart\Block\Popup');
        $config=$popup_block->getConfig();
        
        if ($config['enable']==1) {
            if (!$this->_formKeyValidator->validate($this->getRequest())) {
                return $this->resultRedirectFactory->create()->setPath('*/*/');
            }

            $params = $this->getRequest()->getParams();
        
            try {
                if (isset($params['qty'])) {
                    $filter = new \Zend_Filter_LocalizedToNormalized(
                        ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
                    );
                    $params['qty'] = $filter->filter($params['qty']);
                }
        
                $product = $this->_initProduct();
                /** check whether display popup or not*/
                $productId = (int)$this->getRequest()->getParam('product');
            
                $related = $this->getRequest()->getParam('related_product');
                $upsell=$this->getRequest()->getParam('upsell_product');
                $crosssell=$this->getRequest()->getParam('crosssell_product');
            
                /**
             * Check product availability
             */
                if (!$product) {
                    return $this->goBack();
                }

                $this->cart->addProduct($product, $params);
            
                if (!empty($related)) {
                    $this->cart->addProductsByIds(explode(',', $related));
                }
            
                $this->cart->save();

                /**
             * dispatch event [events.xml] after add to cart product and display confirmation content in popup
             */
                $this->_eventManager->dispatch(
                    'ajaxcheckout_cart_add_product_complete',
                    ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
                );
//MNM - copy (and modified) from \Magento\Checkout\Controller\Cart\Add to have message on product detail page
                if (!$this->_checkoutSession->getNoCartRedirect(true)) {
                    if (!$this->cart->getQuote()->getHasError()) {
                        if ($this->_scopeConfig->isSetFlag(
                                'checkout/cart/redirect_to_cart',
                                \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
                            $message = __(
                                'You added %1 to your shopping cart.',
                                $product->getName()
                                );
                            $this->messageManager->addSuccessMessage($message);
                        } else {
                            $this->messageManager->addComplexSuccessMessage(
                                'addCartSuccessMessage',
                                [
                                    'product_name' => $product->getName(),
                                    'cart_url' => $this->_url->getUrl('checkout/cart', ['_secure' => true]),
                                ]
                                );
                        }
                    }
//                   return $this->goBack(null, $product);
                }
//MNM
                
                if ($this->getRequest()->isAjax()) {
                    return;
                }
            

                if (!$this->_checkoutSession->getNoCartRedirect(true)) {
                    if (!$this->cart->getQuote()->getHasError()) {
                       /* $message = __(
                        'You added %1 to your shopping cart.',
                        $product->getName()
                        );
                        $this->messageManager->addSuccessMessage($message);*/
                    }
                        return $this->goBack(null, $product);
                }
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                if (preg_match('/requested/', $e->getMessage())) {
                    $result['popup_content'] = $e->getMessage();
                    $result['error'] = true;
                    return $this->getResponse()->representJson($this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result));
                }
                $url = $this->_checkoutSession->getRedirectUrl(true);

                /* set below condition if in response product page url then display product content in popup */
                if ($url) {
                    $this->getResponse()->setRedirect($url.'?options=ajax');
                }
                if (!$url) {
                    $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl();
                    $url = $this->_redirect->getRedirectUrl($cartUrl);
                }

                return $this->goBack($url);
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.'));
                $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
                return $this->goBack();
            }
        } else {
             return parent::execute();
        }
    }
}
